Skip to content

Commit 4d7ba34

Browse files
authored
Merge pull request #1058 from jasonleaster/master
add more comments for PooledDataSource and eliminate magic number
2 parents 6ffb461 + eac0d3b commit 4d7ba34

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

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

Lines changed: 29 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,20 @@ 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+
* @since 3.4.5
181+
*/
182+
public void setPoolMaximumLocalBadConnectionTolerance(
183+
int poolMaximumLocalBadConnectionTolerance) {
184+
this.poolMaximumLocalBadConnectionTolerance = poolMaximumLocalBadConnectionTolerance;
185+
}
186+
172187
/*
173188
* The maximum time a connection can be used before it *may* be
174189
* given away again.
@@ -257,6 +272,10 @@ public int getPoolMaximumIdleConnections() {
257272
return poolMaximumIdleConnections;
258273
}
259274

275+
public int getPoolMaximumLocalBadConnectionTolerance() {
276+
return poolMaximumLocalBadConnectionTolerance;
277+
}
278+
260279
public int getPoolMaximumCheckoutTime() {
261280
return poolMaximumCheckoutTime;
262281
}
@@ -400,6 +419,14 @@ private PooledConnection popConnection(String username, String password) throws
400419
try {
401420
oldestActiveConnection.getRealConnection().rollback();
402421
} catch (SQLException e) {
422+
/*
423+
Just log a message for debug and continue to execute the following
424+
statement like nothing happend.
425+
Wrap the bad connection with a new PooledConnection, this will help
426+
to not intterupt current executing thread and give current thread a
427+
chance to join the next competion for another valid/good database
428+
connection. At the end of this loop, bad {@link @conn} will be set as null.
429+
*/
403430
log.debug("Bad connection. Could not roll back");
404431
}
405432
}
@@ -430,6 +457,7 @@ private PooledConnection popConnection(String username, String password) throws
430457
}
431458
}
432459
if (conn != null) {
460+
// ping to server and check the connection is valid or not
433461
if (conn.isValid()) {
434462
if (!conn.getRealConnection().getAutoCommit()) {
435463
conn.getRealConnection().rollback();
@@ -447,7 +475,7 @@ private PooledConnection popConnection(String username, String password) throws
447475
state.badConnectionCount++;
448476
localBadConnectionCount++;
449477
conn = null;
450-
if (localBadConnectionCount > (poolMaximumIdleConnections + 3)) {
478+
if (localBadConnectionCount > (poolMaximumIdleConnections + poolMaximumLocalBadConnectionTolerance)) {
451479
if (log.isDebugEnabled()) {
452480
log.debug("PooledDataSource: Could not get a good connection to the database.");
453481
}

src/site/es/xdoc/configuration.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,14 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
16501650
</li>
16511651
<li><code>poolTimeToWait</code> – Este es un parámetro de bajo nivel que permite escribir un log y reintentar la adquisición de una conexión en caso de que no se haya conseguido la conexión transcurrido un tiempo razonable (esto evita que se produzcan fallos constantes y silenciosos si el pool está mal configurado). Por defecto: 20000ms (20 segundos)
16521652
</li>
1653+
<li><code>poolMaximumLocalBadConnectionTolerance</code> – This is a low level setting about
1654+
tolerance of bad connections got for any thread. If a thread got a bad connection, it may
1655+
still have another chance to re-attempt to get another connection which is valid. But the
1656+
retrying times should not more than the sum of <code>poolMaximumIdleConnections</code>
1657+
and <code>poolMaximumLocalBadConnectionTolerance</code>.
1658+
Default:
1659+
3 (Since: 3.4.5)
1660+
</li>
16531661
<li><code>poolPingQuery</code> – La query de ping (sondeo) que se envía a la base de datos para verificar que la conexión funciona correctamente y que está lista para aceptar nuevas peticiones de conexión. El valor por defecto es "NO PING QUERY SET", que hará que la mayoría de los drivers de base de datos devuelvan un error con un mensaje de error decente.
16541662
</li>
16551663
<li><code>poolPingEnabled</code> – Habilita o inhabilita la query de ping. Si está habilitada deberías informar también la propiedad poolPingQuery con una sentencia SQL (preferentemente una rápida). Por defecto: false.

src/site/ja/xdoc/configuration.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,14 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
17521752
<li>poolTimeToWait – 接続の取得に長時間を要した場合にログを出力し、接続の再取得を試みる機会を与えるための低レベルの設定です(プールが誤って設定された場合、無限に待機状態となってしまうのを防ぐため)。
17531753
デフォルト: 20000ms (20秒)
17541754
</li>
1755+
<li><code>poolMaximumLocalBadConnectionTolerance</code> – This is a low level setting about
1756+
tolerance of bad connections got for any thread. If a thread got a bad connection, it may
1757+
still have another chance to re-attempt to get another connection which is valid. But the
1758+
retrying times should not more than the sum of <code>poolMaximumIdleConnections</code>
1759+
and <code>poolMaximumLocalBadConnectionTolerance</code>.
1760+
Default:
1761+
3 (Since: 3.4.5)
1762+
</li>
17551763
<li>poolPingQuery – プールされた接続が正常で、リクエストを受け付けられる状態にあるかどうか確認するためにデータベースに送信される ping クエリを設定します。
17561764
デフォルトは "NO PING QUERY SET" で、一般的なドライバーであれば問い合わせは失敗し、適切なエラーメッセージが出力されるはずです。
17571765
</li>

src/site/ko/xdoc/configuration.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,14 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
16141614
디폴트는 20000ms(20 초)</li>
16151615
<li><code>poolTimeToWait</code> - 풀이 로그 상태를 출력하고 비정상적으로 긴 경우 커넥션을 다시 얻을려고 시도하는 로우 레벨 설정.
16161616
디폴트는 20000ms(20 초)</li>
1617+
<li><code>poolMaximumLocalBadConnectionTolerance</code> – This is a low level setting about
1618+
tolerance of bad connections got for any thread. If a thread got a bad connection, it may
1619+
still have another chance to re-attempt to get another connection which is valid. But the
1620+
retrying times should not more than the sum of <code>poolMaximumIdleConnections</code>
1621+
and <code>poolMaximumLocalBadConnectionTolerance</code>.
1622+
Default:
1623+
3 (Since: 3.4.5)
1624+
</li>
16171625
<li><code>poolPingQuery</code> - 커넥션이 작업하기 좋은 상태이고 요청을 받아서 처리할 준비가 되었는지 체크하기 위해 데이터베이스에 던지는 핑쿼리(Ping Query).
16181626
디폴트는 “핑 쿼리가 없음” 이다.
16191627
이 설정은 대부분의 데이터베이스로 하여금 에러메시지를 보게 할수도 있다.</li>

src/site/xdoc/configuration.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,14 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, propert
19471947
Default:
19481948
20000ms (i.e. 20 seconds)
19491949
</li>
1950+
<li><code>poolMaximumLocalBadConnectionTolerance</code> – This is a low level setting about
1951+
tolerance of bad connections got for any thread. If a thread got a bad connection, it may
1952+
still have another chance to re-attempt to get another connection which is valid. But the
1953+
retrying times should not more than the sum of <code>poolMaximumIdleConnections</code>
1954+
and <code>poolMaximumLocalBadConnectionTolerance</code>.
1955+
Default:
1956+
3 (Since: 3.4.5)
1957+
</li>
19501958
<li><code>poolPingQuery</code> – The Ping Query is sent to the database to
19511959
validate that a connection is in good working order and is ready
19521960
to

src/site/zh/xdoc/configuration.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,11 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
16391639
</li>
16401640
<li><code>poolTimeToWait</code> – 这是一个底层设置,如果获取连接花费的相当长的时间,它会给连接池打印状态日志并重新尝试获取一个连接(避免在误配置的情况下一直安静的失败),默认值:20000 毫秒(即 20 秒)。
16411641
</li>
1642+
<li><code>poolMaximumLocalBadConnectionTolerance</code> – 这是一个关于坏连接容忍度的底层设置,
1643+
作用于每一个尝试从缓存池获取连接的线程. 如果这个线程获取到的是一个坏的连接,那么这个数据源允许这
1644+
个线程尝试重新获取一个新的连接,但是这个重新尝试的次数不应该超过 <code>poolMaximumIdleConnections</code>
1645+
与 <code>poolMaximumLocalBadConnectionTolerance</code> 之和。 默认值:3 (Since: 3.4.5)
1646+
</li>
16421647
<li><code>poolPingQuery</code> – 发送到数据库的侦测查询,用来检验连接是否处在正常工作秩序中并准备接受请求。默认是“NO PING QUERY SET”,这会导致多数数据库驱动失败时带有一个恰当的错误消息。
16431648
</li>
16441649
<li><code>poolPingEnabled</code> – 是否启用侦测查询。若开启,也必须使用一个可执行的 SQL 语句设置 <code>poolPingQuery</code> 属性(最好是一个非常快的 SQL),默认值:false。

0 commit comments

Comments
 (0)