@@ -50,6 +50,7 @@ public class PooledDataSource implements DataSource {
50
50
protected int poolMaximumIdleConnections = 5 ;
51
51
protected int poolMaximumCheckoutTime = 20000 ;
52
52
protected int poolTimeToWait = 20000 ;
53
+ protected int poolMaximumLocalBadConnectionTolerance = 3 ;
53
54
protected String poolPingQuery = "NO PING QUERY SET" ;
54
55
protected boolean poolPingEnabled ;
55
56
protected int poolPingConnectionsNotUsedFor ;
@@ -169,6 +170,18 @@ public void setPoolMaximumIdleConnections(int poolMaximumIdleConnections) {
169
170
forceCloseAll ();
170
171
}
171
172
173
+ /*
174
+ * The maximum number of tolerance for bad connection happens in one thread
175
+ * which are applying for new {@link PooledConnection}
176
+ *
177
+ * @param poolMaximumLocalBadConnectionTolerance
178
+ * max tolerance for bad connection happens in one thread
179
+ */
180
+ public void setPoolMaximumLocalBadConnectionTolerance (
181
+ int poolMaximumLocalBadConnectionTolerance ) {
182
+ this .poolMaximumLocalBadConnectionTolerance = poolMaximumLocalBadConnectionTolerance ;
183
+ }
184
+
172
185
/*
173
186
* The maximum time a connection can be used before it *may* be
174
187
* given away again.
@@ -257,6 +270,10 @@ public int getPoolMaximumIdleConnections() {
257
270
return poolMaximumIdleConnections ;
258
271
}
259
272
273
+ public int getPoolMaximumLocalBadConnectionTolerance () {
274
+ return poolMaximumLocalBadConnectionTolerance ;
275
+ }
276
+
260
277
public int getPoolMaximumCheckoutTime () {
261
278
return poolMaximumCheckoutTime ;
262
279
}
@@ -400,6 +417,14 @@ private PooledConnection popConnection(String username, String password) throws
400
417
try {
401
418
oldestActiveConnection .getRealConnection ().rollback ();
402
419
} catch (SQLException e ) {
420
+ /*
421
+ Just log a message for debug and continue to execute the following
422
+ statement like nothing happend.
423
+ Wrap the bad connection with a new PooledConnection, this will help
424
+ to not intterupt current executing thread and give current thread a
425
+ chance to join the next competion for another valid/good database
426
+ connection. At the end of this loop, bad {@link @conn} will be set as null.
427
+ */
403
428
log .debug ("Bad connection. Could not roll back" );
404
429
}
405
430
}
@@ -430,6 +455,7 @@ private PooledConnection popConnection(String username, String password) throws
430
455
}
431
456
}
432
457
if (conn != null ) {
458
+ // ping to server and check the connection is valid or not
433
459
if (conn .isValid ()) {
434
460
if (!conn .getRealConnection ().getAutoCommit ()) {
435
461
conn .getRealConnection ().rollback ();
@@ -447,7 +473,7 @@ private PooledConnection popConnection(String username, String password) throws
447
473
state .badConnectionCount ++;
448
474
localBadConnectionCount ++;
449
475
conn = null ;
450
- if (localBadConnectionCount > (poolMaximumIdleConnections + 3 )) {
476
+ if (localBadConnectionCount > (poolMaximumIdleConnections + poolMaximumLocalBadConnectionTolerance )) {
451
477
if (log .isDebugEnabled ()) {
452
478
log .debug ("PooledDataSource: Could not get a good connection to the database." );
453
479
}
0 commit comments