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

Added check with exceptionOverride in case of connection dead #2045

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions src/main/java/com/zaxxer/hikari/pool/PoolBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference;

import static com.zaxxer.hikari.SQLExceptionOverride.Override.DO_NOT_EVICT;
import static com.zaxxer.hikari.pool.ProxyConnection.*;
import static com.zaxxer.hikari.util.ClockSource.*;
import static com.zaxxer.hikari.util.UtilityElf.createInstance;
Expand Down Expand Up @@ -145,39 +146,58 @@ void quietlyCloseConnection(final Connection connection, final String closureRea
}
}

boolean isConnectionDead(final Connection connection)
{
private boolean doIsConnectionDead(final Connection connection) throws SQLException {
setNetworkTimeout(connection, validationTimeout);
try {
setNetworkTimeout(connection, validationTimeout);
try {
final var validationSeconds = (int) Math.max(1000L, validationTimeout) / 1000;
final var validationSeconds = (int) Math.max(1000L, validationTimeout) / 1000;

if (isUseJdbc4Validation) {
return !connection.isValid(validationSeconds);
}

try (var statement = connection.createStatement()) {
if (isNetworkTimeoutSupported != TRUE) {
setQueryTimeout(statement, validationSeconds);
}
if (isUseJdbc4Validation) {
return !connection.isValid(validationSeconds);
}

statement.execute(config.getConnectionTestQuery());
try (var statement = connection.createStatement()) {
if (isNetworkTimeoutSupported != TRUE) {
setQueryTimeout(statement, validationSeconds);
}

statement.execute(config.getConnectionTestQuery());
}
finally {
setNetworkTimeout(connection, networkTimeout);
}
finally {
setNetworkTimeout(connection, networkTimeout);

if (isIsolateInternalQueries && !isAutoCommit) {
connection.rollback();
}
if (isIsolateInternalQueries && !isAutoCommit) {
connection.rollback();
}
}
return false;
}

return false;
private void connectionDeadException(final Connection connection, Exception e) {
lastConnectionFailure.set(e);
logger.warn("{} - Failed to validate connection {} ({}). Possibly consider using a shorter maxLifetime value.",
poolName, connection, e.getMessage());
}

boolean isConnectionDead(final Connection connection)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major change is here.

In case of exception let the exceptionOverride decide if the connection is good or not, and give it another try since it can be an exception of recovery (e.g. failover switch)

{
try {
return doIsConnectionDead(connection);
}
catch (Exception e) {
lastConnectionFailure.set(e);
logger.warn("{} - Failed to validate connection {} ({}). Possibly consider using a shorter maxLifetime value.",
poolName, connection, e.getMessage());
if (e instanceof SQLException && exceptionOverride != null &&
exceptionOverride.adjudicate((SQLException) e) == DO_NOT_EVICT) {
// try one more time, in case of failover
try {
return doIsConnectionDead(connection);
}
catch (Exception e2) {
connectionDeadException(connection, e2);
return true;
}
}

connectionDeadException(connection, e);
return true;
}
}
Expand Down