Skip to content

Commit eb26c49

Browse files
committed
Added new getRawConnection() method to the ConnectionPool class
1 parent f562c5d commit eb26c49

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

src/javaxt/sql/ConnectionPool.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,34 @@ public ConnectionPool(ConnectionPoolDataSource dataSource, int maxConnections, M
137137
* using the connection, it must ne closed in order to return it to the pool.
138138
*/
139139
public Connection getConnection() throws SQLException {
140+
java.sql.Connection conn = getRawConnection();
141+
if (conn == null) {
142+
return null;
143+
}
144+
else{
145+
Connection c = new Connection();
146+
c.open(conn, database);
147+
return c;
148+
}
149+
}
150+
151+
152+
//**************************************************************************
153+
//** getRawConnection
154+
//**************************************************************************
155+
/** Retrieves a raw, java.sql.Connection from the connection pool. This
156+
* method is called by the getConnection() method which simply wraps the
157+
* java.sql.Connection into a javaxt.sql.Connection.
158+
*/
159+
public java.sql.Connection getRawConnection() throws SQLException {
140160
long time = System.currentTimeMillis();
141161
long timeoutTime = time + timeoutMs;
142162
int triesWithoutDelay = getInactiveConnections() + 1;
143163

144164
while (true) {
145165
java.sql.Connection conn = getConnection(time, timeoutTime);
146166
if (conn != null) {
147-
Connection c = new Connection();
148-
c.open(conn, database);
149-
return c;
167+
return conn;
150168
}
151169
triesWithoutDelay--;
152170
if (triesWithoutDelay <= 0) {
@@ -360,36 +378,36 @@ private void init(ConnectionPoolDataSource dataSource, int maxConnections, Map<S
360378
//** getConnection
361379
//**************************************************************************
362380
private java.sql.Connection getConnection(long time, long timeoutTime) {
363-
long rtime = Math.max(1, timeoutTime - time);
364-
java.sql.Connection conn;
365-
try {
381+
long rtime = Math.max(1, timeoutTime - time);
382+
java.sql.Connection conn;
383+
try {
366384
conn = acquireConnection(rtime);
367385
}
368386
catch (SQLException e) {
369387
return null;
370388
}
371389

372390
// Calculate remaining time for validation
373-
rtime = timeoutTime - System.currentTimeMillis();
391+
rtime = timeoutTime - System.currentTimeMillis();
374392
int rtimeSecs = Math.max(1, (int)((rtime + 999) / 1000));
375393

376-
try {
377-
if (conn.isValid(rtimeSecs)) {
394+
try {
395+
if (conn.isValid(rtimeSecs)) {
378396
return conn;
379397
}
380398
}
381399
catch (SQLException e) {
382400
log("isValid() failed: " + e.getMessage());
383-
// This Exception should never occur. If it nevertheless occurs, it's because of an error in the
384-
// JDBC driver which we ignore and assume that the connection is not valid.
401+
// This Exception should never occur. If it nevertheless occurs, it's because of an error in the
402+
// JDBC driver which we ignore and assume that the connection is not valid.
385403
}
386404

387-
// When isValid() returns false, the JDBC driver should have already called connectionErrorOccurred()
388-
// and the PooledConnection has been removed from the pool, i.e. the PooledConnection will
389-
// not be added to recycledConnections when Connection.close() is called.
390-
// But to be sure that this works even with a faulty JDBC driver, we call purgeConnection().
391-
purgeConnection(conn);
392-
return null;
405+
// When isValid() returns false, the JDBC driver should have already called connectionErrorOccurred()
406+
// and the PooledConnection has been removed from the pool, i.e. the PooledConnection will
407+
// not be added to recycledConnections when Connection.close() is called.
408+
// But to be sure that this works even with a faulty JDBC driver, we call purgeConnection().
409+
purgeConnection(conn);
410+
return null;
393411
}
394412

395413

@@ -679,7 +697,7 @@ private void performHealthCheck() {
679697
log("Pool warm-up: added connection " + currentRecycled + "/" + minConnections);
680698
} else {
681699
// If validation fails, dispose the connection and decrement counter
682-
disposeConnection(pconn);
700+
disposeConnection(pconn);
683701
pconn = null; // Ensure pconn is null so finally block doesn't try to close it again
684702
}
685703
} catch (SQLException e) {
@@ -862,12 +880,12 @@ private void disposeConnection (PooledConnection pconn) {
862880
//** log
863881
//**************************************************************************
864882
private void log(String msg) {
865-
String s = "ConnectionPool: "+msg;
866-
try {
867-
if (logWriter == null) {
883+
String s = "ConnectionPool: "+msg;
884+
try {
885+
if (logWriter == null) {
868886
//System.err.println(s);
869887
}
870-
else {
888+
else {
871889
logWriter.println(s);
872890
}
873891
}
@@ -902,13 +920,13 @@ private void assertInnerState() {
902920
//**************************************************************************
903921
private class PoolConnectionEventListener implements ConnectionEventListener {
904922
@Override
905-
public void connectionClosed (ConnectionEvent event) {
906-
PooledConnection pconn = (PooledConnection)event.getSource();
923+
public void connectionClosed (ConnectionEvent event) {
924+
PooledConnection pconn = (PooledConnection)event.getSource();
907925
recycleConnection(pconn);
908926
}
909927
@Override
910-
public void connectionErrorOccurred (ConnectionEvent event) {
911-
PooledConnection pconn = (PooledConnection)event.getSource();
928+
public void connectionErrorOccurred (ConnectionEvent event) {
929+
PooledConnection pconn = (PooledConnection)event.getSource();
912930
disposeConnection(pconn);
913931
}
914932
}

0 commit comments

Comments
 (0)