Skip to content

Commit ec6891a

Browse files
Expand/improve SQLExceptionOverride handling flexibility
1 parent c7cf4b3 commit ec6891a

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/main/java/com/zaxxer/hikari/SQLExceptionOverride.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
/**
66
* Users can implement this interface to override the default SQLException handling
7-
* of HikariCP. By the time an instance of this interface is invoked HikariCP has
8-
* already made a determination to evict the Connection from the pool.
9-
*
10-
* If the {@link #adjudicate(SQLException)} method returns {@link Override#CONTINUE_EVICT} the eviction will occur, but if the
11-
* method returns {@link Override#DO_NOT_EVICT} the eviction will be elided.
7+
* of HikariCP. When a SQLException is thrown from JDBC execution methods, the
8+
* SQLState and error code will be checked to determine if the connection should
9+
* be evicted from the pool.
10+
* <p>
11+
* By supplying an implementation of this interface, users can override the default
12+
* handling of SQLExceptions. The {@link #adjudicate(SQLException)} method will be called
13+
* with the SQLException that was thrown. If the method returns {@link Override#CONTINUE_EVICT}
14+
* the customary built-in handling will occur. If the method returns {@link Override#DO_NOT_EVICT}
15+
* the eviction will be elided. If the method returns {@link Override#MUST_EVICT} the eviction will
16+
* be evicted regardless of the SQLState or error code.
1217
*/
1318
public interface SQLExceptionOverride {
1419
enum Override {
1520
CONTINUE_EVICT,
16-
DO_NOT_EVICT
21+
DO_NOT_EVICT,
22+
MUST_EVICT
1723
}
1824

1925
/**
20-
* If this method returns {@link Override#CONTINUE_EVICT} then Connection eviction will occur, but if it
21-
* returns {@link Override#DO_NOT_EVICT} the eviction will be elided.
26+
* This method is called when a SQLException is thrown from a JDBC method.
2227
*
23-
* @param sqlException the #SQLException to adjudicate
24-
* @return either one of {@link Override#CONTINUE_EVICT} or {@link Override#DO_NOT_EVICT}
28+
* @param sqlException the SQLException that was thrown
29+
* @return an {@link Override} value indicating how eviction should proceed
2530
*/
2631
default Override adjudicate(final SQLException sqlException)
2732
{

src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import java.util.Set;
2828
import java.util.concurrent.Executor;
2929

30-
import static com.zaxxer.hikari.SQLExceptionOverride.Override.CONTINUE_EVICT;
31-
import static com.zaxxer.hikari.SQLExceptionOverride.Override.DO_NOT_EVICT;
30+
import static com.zaxxer.hikari.SQLExceptionOverride.Override.*;
3231

3332
/**
3433
* This is the proxy class for {@link Connection}.
@@ -156,12 +155,14 @@ final SQLException checkException(SQLException sqle)
156155
final var exceptionOverride = poolEntry.getPoolBase().exceptionOverride;
157156
for (int depth = 0; delegate != ClosedConnection.CLOSED_CONNECTION && nse != null && depth < 10; depth++) {
158157
final var sqlState = nse.getSQLState();
159-
if (exceptionOverride != null && exceptionOverride.adjudicate(nse) == DO_NOT_EVICT) {
158+
final var shouldEvict = exceptionOverride != null ? exceptionOverride.adjudicate(nse) : CONTINUE_EVICT;
159+
if (shouldEvict == DO_NOT_EVICT) {
160160
break;
161161
}
162162
else if (sqlState != null && sqlState.startsWith("08")
163163
|| ERROR_STATES.contains(sqlState)
164-
|| ERROR_CODES.contains(nse.getErrorCode())) {
164+
|| ERROR_CODES.contains(nse.getErrorCode())
165+
|| shouldEvict == MUST_EVICT) {
165166

166167
// broken connection
167168
evict = true;

0 commit comments

Comments
 (0)