Expand/improve SQLExceptionOverride handling flexibility

pull/2266/head
Brett Wooldridge 3 months ago
parent c7cf4b3616
commit ec6891a08e
No known key found for this signature in database
GPG Key ID: 4CC08E7F47C3EC76

@ -4,24 +4,29 @@ import java.sql.SQLException;
/**
* Users can implement this interface to override the default SQLException handling
* of HikariCP. By the time an instance of this interface is invoked HikariCP has
* already made a determination to evict the Connection from the pool.
*
* If the {@link #adjudicate(SQLException)} method returns {@link Override#CONTINUE_EVICT} the eviction will occur, but if the
* method returns {@link Override#DO_NOT_EVICT} the eviction will be elided.
* of HikariCP. When a SQLException is thrown from JDBC execution methods, the
* SQLState and error code will be checked to determine if the connection should
* be evicted from the pool.
* <p>
* By supplying an implementation of this interface, users can override the default
* handling of SQLExceptions. The {@link #adjudicate(SQLException)} method will be called
* with the SQLException that was thrown. If the method returns {@link Override#CONTINUE_EVICT}
* the customary built-in handling will occur. If the method returns {@link Override#DO_NOT_EVICT}
* the eviction will be elided. If the method returns {@link Override#MUST_EVICT} the eviction will
* be evicted regardless of the SQLState or error code.
*/
public interface SQLExceptionOverride {
enum Override {
CONTINUE_EVICT,
DO_NOT_EVICT
DO_NOT_EVICT,
MUST_EVICT
}
/**
* If this method returns {@link Override#CONTINUE_EVICT} then Connection eviction will occur, but if it
* returns {@link Override#DO_NOT_EVICT} the eviction will be elided.
* This method is called when a SQLException is thrown from a JDBC method.
*
* @param sqlException the #SQLException to adjudicate
* @return either one of {@link Override#CONTINUE_EVICT} or {@link Override#DO_NOT_EVICT}
* @param sqlException the SQLException that was thrown
* @return an {@link Override} value indicating how eviction should proceed
*/
default Override adjudicate(final SQLException sqlException)
{

@ -27,8 +27,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executor;
import static com.zaxxer.hikari.SQLExceptionOverride.Override.CONTINUE_EVICT;
import static com.zaxxer.hikari.SQLExceptionOverride.Override.DO_NOT_EVICT;
import static com.zaxxer.hikari.SQLExceptionOverride.Override.*;
/**
* This is the proxy class for {@link Connection}.
@ -156,12 +155,14 @@ public abstract class ProxyConnection implements Connection
final var exceptionOverride = poolEntry.getPoolBase().exceptionOverride;
for (int depth = 0; delegate != ClosedConnection.CLOSED_CONNECTION && nse != null && depth < 10; depth++) {
final var sqlState = nse.getSQLState();
if (exceptionOverride != null && exceptionOverride.adjudicate(nse) == DO_NOT_EVICT) {
final var shouldEvict = exceptionOverride != null ? exceptionOverride.adjudicate(nse) : CONTINUE_EVICT;
if (shouldEvict == DO_NOT_EVICT) {
break;
}
else if (sqlState != null && sqlState.startsWith("08")
|| ERROR_STATES.contains(sqlState)
|| ERROR_CODES.contains(nse.getErrorCode())) {
|| ERROR_CODES.contains(nse.getErrorCode())
|| shouldEvict == MUST_EVICT) {
// broken connection
evict = true;

Loading…
Cancel
Save