diff --git a/src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java b/src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java index 8efef77df40..e16e213a3a4 100644 --- a/src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java +++ b/src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java @@ -23,6 +23,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; +import java.util.concurrent.Executor; import java.util.logging.Logger; import javax.sql.DataSource; @@ -56,6 +57,9 @@ public class PooledDataSource implements DataSource { private int expectedConnectionTypeCode; + protected int networkPingTimeout = 0; + private final static Executor immediateExecutor = Runnable::run; + public PooledDataSource() { dataSource = new UnpooledDataSource(); } @@ -553,8 +557,18 @@ protected boolean pingConnection(PooledConnection conn) { log.debug("Testing connection " + conn.getRealHashCode() + " ..."); } Connection realConn = conn.getRealConnection(); + + final int timeout = realConn.getNetworkTimeout(); + if (networkPingTimeout != 0) { + log.debug("Testing connection " + conn.getRealHashCode() + " with network timeout " + networkPingTimeout + "ms ..."); + realConn.setNetworkTimeout(immediateExecutor, networkPingTimeout); + } try (Statement statement = realConn.createStatement()) { statement.executeQuery(poolPingQuery).close(); + } finally { + if (networkPingTimeout != 0) { + realConn.setNetworkTimeout(immediateExecutor, timeout); + } } if (!realConn.getAutoCommit()) { realConn.rollback(); diff --git a/src/site/es/xdoc/configuration.xml b/src/site/es/xdoc/configuration.xml index bd4035c3666..8a44b89c4bc 100644 --- a/src/site/es/xdoc/configuration.xml +++ b/src/site/es/xdoc/configuration.xml @@ -1847,6 +1847,8 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
poolPingConnectionsNotUsedFor
– Configura la frecuencia con la que se ejecutará la sentencia poolPingQuery. Normalmente se iguala al timeout de la conexión de base de datos para evitar pings innecesarios. Por defecto: 0 (todas las conexiones se testean continuamente – solo si se ha habilitado poolPingEnabled).
networkPingTimeout
– Configura un timeout (en milisegundos) a nivel de la conexión para la query de ping. En caso de que pase este tiempo, la conexión sera inválida. En caso de que un firewall o un problema de red descarte los paquetes, las conexiones no esperaran indefinidamente una respuesta. Por defecto: 0 (inactivo).
+ JNDI diff --git a/src/site/xdoc/configuration.xml b/src/site/xdoc/configuration.xml index 8a1464e893b..a765e3ff1df 100644 --- a/src/site/xdoc/configuration.xml +++ b/src/site/xdoc/configuration.xml @@ -2153,6 +2153,10 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, propert Default: 0 (i.e. all connections are pinged every time – but only if poolPingEnabled is true of course). +
networkPingTimeout
– This configures a timeout (in milliseconds) at the connection level for
+ the ping query. If this time passes, the connection will be invalid. In the event that a firewall or network
+ problem drops packets, connections will not wait indefinitely for a response. Default: 0 (inactive)
+ JNDI