Skip to content

Commit 9804467

Browse files
committed
add more comments for PooledDataSource and eliminate magic number
1 parent 6f9105e commit 9804467

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class PooledDataSource implements DataSource {
5050
protected int poolMaximumIdleConnections = 5;
5151
protected int poolMaximumCheckoutTime = 20000;
5252
protected int poolTimeToWait = 20000;
53+
protected int poolMaximumLocalBadConnectionTolerance = 3;
5354
protected String poolPingQuery = "NO PING QUERY SET";
5455
protected boolean poolPingEnabled;
5556
protected int poolPingConnectionsNotUsedFor;
@@ -169,6 +170,18 @@ public void setPoolMaximumIdleConnections(int poolMaximumIdleConnections) {
169170
forceCloseAll();
170171
}
171172

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+
172185
/*
173186
* The maximum time a connection can be used before it *may* be
174187
* given away again.
@@ -257,6 +270,10 @@ public int getPoolMaximumIdleConnections() {
257270
return poolMaximumIdleConnections;
258271
}
259272

273+
public int getPoolMaximumLocalBadConnectionTolerance() {
274+
return poolMaximumLocalBadConnectionTolerance;
275+
}
276+
260277
public int getPoolMaximumCheckoutTime() {
261278
return poolMaximumCheckoutTime;
262279
}
@@ -400,6 +417,14 @@ private PooledConnection popConnection(String username, String password) throws
400417
try {
401418
oldestActiveConnection.getRealConnection().rollback();
402419
} 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+
*/
403428
log.debug("Bad connection. Could not roll back");
404429
}
405430
}
@@ -430,6 +455,7 @@ private PooledConnection popConnection(String username, String password) throws
430455
}
431456
}
432457
if (conn != null) {
458+
// ping to server and check the connection is valid or not
433459
if (conn.isValid()) {
434460
if (!conn.getRealConnection().getAutoCommit()) {
435461
conn.getRealConnection().rollback();
@@ -447,7 +473,7 @@ private PooledConnection popConnection(String username, String password) throws
447473
state.badConnectionCount++;
448474
localBadConnectionCount++;
449475
conn = null;
450-
if (localBadConnectionCount > (poolMaximumIdleConnections + 3)) {
476+
if (localBadConnectionCount > (poolMaximumIdleConnections + poolMaximumLocalBadConnectionTolerance)) {
451477
if (log.isDebugEnabled()) {
452478
log.debug("PooledDataSource: Could not get a good connection to the database.");
453479
}

0 commit comments

Comments
 (0)