Skip to content

Commit

Permalink
Expand/improve SQLExceptionOverride handling flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Nov 16, 2024
1 parent c7cf4b3 commit ec6891a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/main/java/com/zaxxer/hikari/SQLExceptionOverride.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@

/**
* 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)
{
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
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}.
Expand Down Expand Up @@ -156,12 +155,14 @@ final SQLException checkException(SQLException sqle)
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;
Expand Down

0 comments on commit ec6891a

Please sign in to comment.