Skip to content

Commit 6f8f13b

Browse files
author
diego Dupin
committed
[misc] pool connection correction
1 parent e57faf8 commit 6f8f13b

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

src/main/java/org/mariadb/jdbc/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.concurrent.locks.ReentrantLock;
1212
import java.util.regex.Matcher;
1313
import java.util.regex.Pattern;
14+
import javax.sql.ConnectionEvent;
1415
import org.mariadb.jdbc.client.Client;
1516
import org.mariadb.jdbc.client.Context;
1617
import org.mariadb.jdbc.client.impl.StandardClient;
@@ -195,8 +196,7 @@ public void rollback() throws SQLException {
195196
@Override
196197
public void close() throws SQLException {
197198
if (poolConnection != null) {
198-
MariaDbPoolConnection poolConnection = this.poolConnection;
199-
poolConnection.close();
199+
poolConnection.fireConnectionClosed(new ConnectionEvent(poolConnection));
200200
return;
201201
}
202202
client.close();

src/main/java/org/mariadb/jdbc/pool/MariaDbInnerPoolConnection.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public MariaDbInnerPoolConnection(Connection connection) {
2222
lastUsed = new AtomicLong(System.nanoTime());
2323
}
2424

25-
public void close() {
26-
fireConnectionClosed(new ConnectionEvent(this));
27-
}
28-
2925
/**
3026
* Indicate last time this pool connection has been used.
3127
*

src/test/java/org/mariadb/jdbc/integration/PoolDataSourceTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,21 @@ public static void drop() throws SQLException {
6565

6666
@Test
6767
public void basic() throws SQLException {
68-
MariaDbPoolDataSource ds = new MariaDbPoolDataSource(mDefUrl);
68+
MariaDbPoolDataSource ds = new MariaDbPoolDataSource(mDefUrl + "&maxPoolSize=2");
6969
testDs(ds);
7070
ds.close();
7171

7272
ds = new MariaDbPoolDataSource();
73-
ds.setUrl(mDefUrl);
73+
ds.setUrl(mDefUrl + "&maxPoolSize=2");
7474
testDs(ds);
7575
ds.close();
7676
}
7777

7878
private void testDs(MariaDbPoolDataSource ds) throws SQLException {
7979
try (Connection con1 = ds.getConnection()) {
80-
try (Connection con2 = ds.getConnection()) {
81-
80+
long threadId;
81+
try (org.mariadb.jdbc.Connection con2 = (org.mariadb.jdbc.Connection) ds.getConnection()) {
82+
threadId = con2.getThreadId();
8283
ResultSet rs1 = con1.createStatement().executeQuery("SELECT 1");
8384
ResultSet rs2 = con2.createStatement().executeQuery("SELECT 2");
8485
while (rs1.next()) {
@@ -88,6 +89,9 @@ private void testDs(MariaDbPoolDataSource ds) throws SQLException {
8889
assertEquals(2, rs2.getInt(1));
8990
}
9091
}
92+
try (org.mariadb.jdbc.Connection con2 = (org.mariadb.jdbc.Connection) ds.getConnection()) {
93+
assertEquals(threadId, con2.getThreadId());
94+
}
9195
}
9296

9397
PooledConnection con1 = null;
@@ -104,10 +108,13 @@ private void testDs(MariaDbPoolDataSource ds) throws SQLException {
104108
while (rs2.next()) {
105109
assertEquals(2, rs2.getInt(1));
106110
}
107-
111+
long threadId = ((org.mariadb.jdbc.Connection) con2.getConnection()).getThreadId();
112+
if (con2 != null) con2.getConnection().close();
113+
con2 = ds.getPooledConnection();
114+
assertEquals(threadId, ((org.mariadb.jdbc.Connection) con2.getConnection()).getThreadId());
108115
} finally {
109-
if (con1 != null) con1.close();
110-
if (con2 != null) con2.close();
116+
if (con1 != null) con1.getConnection().close();
117+
if (con2 != null) con2.getConnection().close();
111118
}
112119

113120
XAConnection conx1 = null;

src/test/java/org/mariadb/jdbc/integration/PooledConnectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void testPooledConnectionClosed() throws Exception {
3131
MyEventListener listener = new MyEventListener();
3232
pc.addConnectionEventListener(listener);
3333
pc.addStatementEventListener(listener);
34-
connection.close();
34+
pc.close();
3535
assertTrue(listener.closed);
3636
assertThrows(SQLException.class, () -> connection.createStatement().execute("select 1"));
3737
pc.removeConnectionEventListener(listener);

src/test/java/org/mariadb/jdbc/integration/XaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public static void beforeAll2() throws SQLException {
3535
Assumptions.assumeTrue(
3636
!"skysql".equals(System.getenv("srv")) && !"skysql-ha".equals(System.getenv("srv")));
3737

38-
drop();
3938
Statement stmt = sharedConn.createStatement();
39+
stmt.execute("DROP TABLE IF EXISTS xatable");
4040
stmt.execute("CREATE TABLE xatable(i int)");
4141
stmt.execute("FLUSH TABLES");
4242
dataSource = new MariaDbDataSource(mDefUrl);
@@ -144,7 +144,7 @@ private int test2PhaseCommit(boolean doCommit, XADataSource dataSource) throws E
144144
for (int i = 0; i < connectionNumber; i++) {
145145
try {
146146
if (xaConnections[i] != null) {
147-
xaConnections[i].close();
147+
xaConnections[i].getConnection().close();
148148
}
149149
} catch (Exception e) {
150150
e.printStackTrace();

0 commit comments

Comments
 (0)