Skip to content

Commit

Permalink
[misc] pool connection correction
Browse files Browse the repository at this point in the history
  • Loading branch information
diego Dupin committed Jan 3, 2022
1 parent e57faf8 commit 6f8f13b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mariadb/jdbc/Connection.java
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sql.ConnectionEvent;
import org.mariadb.jdbc.client.Client;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.impl.StandardClient;
Expand Down Expand Up @@ -195,8 +196,7 @@ public void rollback() throws SQLException {
@Override
public void close() throws SQLException {
if (poolConnection != null) {
MariaDbPoolConnection poolConnection = this.poolConnection;
poolConnection.close();
poolConnection.fireConnectionClosed(new ConnectionEvent(poolConnection));
return;
}
client.close();
Expand Down
Expand Up @@ -22,10 +22,6 @@ public MariaDbInnerPoolConnection(Connection connection) {
lastUsed = new AtomicLong(System.nanoTime());
}

public void close() {
fireConnectionClosed(new ConnectionEvent(this));
}

/**
* Indicate last time this pool connection has been used.
*
Expand Down
21 changes: 14 additions & 7 deletions src/test/java/org/mariadb/jdbc/integration/PoolDataSourceTest.java
Expand Up @@ -65,20 +65,21 @@ public static void drop() throws SQLException {

@Test
public void basic() throws SQLException {
MariaDbPoolDataSource ds = new MariaDbPoolDataSource(mDefUrl);
MariaDbPoolDataSource ds = new MariaDbPoolDataSource(mDefUrl + "&maxPoolSize=2");
testDs(ds);
ds.close();

ds = new MariaDbPoolDataSource();
ds.setUrl(mDefUrl);
ds.setUrl(mDefUrl + "&maxPoolSize=2");
testDs(ds);
ds.close();
}

private void testDs(MariaDbPoolDataSource ds) throws SQLException {
try (Connection con1 = ds.getConnection()) {
try (Connection con2 = ds.getConnection()) {

long threadId;
try (org.mariadb.jdbc.Connection con2 = (org.mariadb.jdbc.Connection) ds.getConnection()) {
threadId = con2.getThreadId();
ResultSet rs1 = con1.createStatement().executeQuery("SELECT 1");
ResultSet rs2 = con2.createStatement().executeQuery("SELECT 2");
while (rs1.next()) {
Expand All @@ -88,6 +89,9 @@ private void testDs(MariaDbPoolDataSource ds) throws SQLException {
assertEquals(2, rs2.getInt(1));
}
}
try (org.mariadb.jdbc.Connection con2 = (org.mariadb.jdbc.Connection) ds.getConnection()) {
assertEquals(threadId, con2.getThreadId());
}
}

PooledConnection con1 = null;
Expand All @@ -104,10 +108,13 @@ private void testDs(MariaDbPoolDataSource ds) throws SQLException {
while (rs2.next()) {
assertEquals(2, rs2.getInt(1));
}

long threadId = ((org.mariadb.jdbc.Connection) con2.getConnection()).getThreadId();
if (con2 != null) con2.getConnection().close();
con2 = ds.getPooledConnection();
assertEquals(threadId, ((org.mariadb.jdbc.Connection) con2.getConnection()).getThreadId());
} finally {
if (con1 != null) con1.close();
if (con2 != null) con2.close();
if (con1 != null) con1.getConnection().close();
if (con2 != null) con2.getConnection().close();
}

XAConnection conx1 = null;
Expand Down
Expand Up @@ -31,7 +31,7 @@ public void testPooledConnectionClosed() throws Exception {
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
pc.addStatementEventListener(listener);
connection.close();
pc.close();
assertTrue(listener.closed);
assertThrows(SQLException.class, () -> connection.createStatement().execute("select 1"));
pc.removeConnectionEventListener(listener);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/mariadb/jdbc/integration/XaTest.java
Expand Up @@ -35,8 +35,8 @@ public static void beforeAll2() throws SQLException {
Assumptions.assumeTrue(
!"skysql".equals(System.getenv("srv")) && !"skysql-ha".equals(System.getenv("srv")));

drop();
Statement stmt = sharedConn.createStatement();
stmt.execute("DROP TABLE IF EXISTS xatable");
stmt.execute("CREATE TABLE xatable(i int)");
stmt.execute("FLUSH TABLES");
dataSource = new MariaDbDataSource(mDefUrl);
Expand Down Expand Up @@ -144,7 +144,7 @@ private int test2PhaseCommit(boolean doCommit, XADataSource dataSource) throws E
for (int i = 0; i < connectionNumber; i++) {
try {
if (xaConnections[i] != null) {
xaConnections[i].close();
xaConnections[i].getConnection().close();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 6f8f13b

Please sign in to comment.