Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pooled connection timeout in pingConnection method #2621

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/site/es/xdoc/configuration.xml
Expand Up @@ -1847,6 +1847,8 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properti
</li>
<li><code>poolPingConnectionsNotUsedFor</code> – 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).
</li>
<li><code>networkPingTimeout</code> – 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).
</li>
</ul>
<p>
<strong>JNDI</strong>
Expand Down
4 changes: 4 additions & 0 deletions src/site/xdoc/configuration.xml
Expand Up @@ -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).
</li>
<li><code>networkPingTimeout</code> – 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)
</li>
</ul>
<p>
<strong>JNDI</strong>
Expand Down