-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[core] Ensuring JDBC connection is not stale before using it #7470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
junmuz
wants to merge
1
commit into
apache:master
Choose a base branch
from
junmuz:jdbc_conn_validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
paimon-core/src/test/java/org/apache/paimon/jdbc/JdbcClientPoolTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.paimon.jdbc; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.Collections; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Tests for {@link JdbcClientPool} connection validation. */ | ||
| public class JdbcClientPoolTest { | ||
|
|
||
| private JdbcClientPool createPool(int poolSize) { | ||
| String dbUrl = | ||
| "jdbc:sqlite:file::memory:?ic" + UUID.randomUUID().toString().replace("-", ""); | ||
| return new JdbcClientPool(poolSize, dbUrl, Collections.emptyMap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidConnectionIsReused() throws SQLException, InterruptedException { | ||
| JdbcClientPool pool = createPool(1); | ||
| try { | ||
| AtomicReference<Connection> firstConn = new AtomicReference<>(); | ||
| AtomicReference<Connection> secondConn = new AtomicReference<>(); | ||
|
|
||
| pool.run( | ||
| connection -> { | ||
| firstConn.set(connection); | ||
| return null; | ||
| }); | ||
|
|
||
| pool.run( | ||
| connection -> { | ||
| secondConn.set(connection); | ||
| return null; | ||
| }); | ||
|
|
||
| assertThat(secondConn.get()).isSameAs(firstConn.get()); | ||
| } finally { | ||
| pool.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosedConnectionIsReplaced() throws SQLException, InterruptedException { | ||
| JdbcClientPool pool = createPool(1); | ||
| try { | ||
| AtomicReference<Connection> firstConn = new AtomicReference<>(); | ||
| AtomicReference<Connection> secondConn = new AtomicReference<>(); | ||
|
|
||
| // Get the connection and close it to simulate a stale connection | ||
| pool.run( | ||
| connection -> { | ||
| firstConn.set(connection); | ||
| connection.close(); | ||
| return null; | ||
| }); | ||
|
|
||
| // The pool should detect the closed connection and create a new one | ||
| pool.run( | ||
| connection -> { | ||
| secondConn.set(connection); | ||
| return null; | ||
| }); | ||
|
|
||
| assertThat(secondConn.get()).isNotSameAs(firstConn.get()); | ||
| assertThat(secondConn.get().isClosed()).isFalse(); | ||
| } finally { | ||
| pool.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplacedConnectionIsReturnedToPool() throws SQLException, InterruptedException { | ||
| JdbcClientPool pool = createPool(1); | ||
| try { | ||
| AtomicReference<Connection> replacedConn = new AtomicReference<>(); | ||
| AtomicReference<Connection> thirdConn = new AtomicReference<>(); | ||
|
|
||
| // Close the connection to trigger replacement | ||
| pool.run( | ||
| connection -> { | ||
| connection.close(); | ||
| return null; | ||
| }); | ||
|
|
||
| // This call gets the replacement connection | ||
| pool.run( | ||
| connection -> { | ||
| replacedConn.set(connection); | ||
| return null; | ||
| }); | ||
|
|
||
| // The replacement should be reused since it's valid | ||
| pool.run( | ||
| connection -> { | ||
| thirdConn.set(connection); | ||
| return null; | ||
| }); | ||
|
|
||
| assertThat(thirdConn.get()).isSameAs(replacedConn.get()); | ||
| } finally { | ||
| pool.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testActionIsExecutedOnValidConnection() throws SQLException, InterruptedException { | ||
| JdbcClientPool pool = createPool(1); | ||
| try { | ||
| // Close the connection to simulate staleness | ||
| pool.run( | ||
| connection -> { | ||
| connection.close(); | ||
| return null; | ||
| }); | ||
|
|
||
| // The action should receive a valid connection and succeed | ||
| boolean result = | ||
| pool.run( | ||
| connection -> { | ||
| // Execute a real SQL statement to verify the connection works | ||
| return connection.prepareStatement("SELECT 1").execute(); | ||
| }); | ||
|
|
||
| assertThat(result).isTrue(); | ||
| } finally { | ||
| pool.close(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is better to change a method name?