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

Update checkException to adjudicate various exception #2031

Open
wants to merge 1 commit into
base: dev
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
4 changes: 4 additions & 0 deletions src/main/java/com/zaxxer/hikari/SQLExceptionOverride.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ default Override adjudicate(final SQLException sqlException)
{
return Override.CONTINUE_EVICT;
}

default boolean adjudicateAnyway() {
return false;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ final SQLException checkException(SQLException sqle)
if (sqlState != null && sqlState.startsWith("08")
|| nse instanceof SQLTimeoutException
|| ERROR_STATES.contains(sqlState)
|| ERROR_CODES.contains(nse.getErrorCode())) {
|| ERROR_CODES.contains(nse.getErrorCode())
|| exceptionOverride != null && exceptionOverride.adjudicateAnyway()) {

if (exceptionOverride != null && exceptionOverride.adjudicate(nse) == DO_NOT_EVICT) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setMaxFieldSize(int max) throws SQLException
@Override
public int getMaxRows() throws SQLException
{
return 0;
throw new SQLException("Simulated general error", "HY000", 1290);
}

/** {@inheritDoc} */
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/zaxxer/hikari/pool/TestConnections.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,45 @@ public void testEviction3() throws SQLException
}
}

@Test
public void testEviction4() throws SQLException
{
HikariConfig config = newHikariConfig();
config.setMaximumPoolSize(5);
config.setConnectionTimeout(2500);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setExceptionOverrideClassName(OverrideHandler2.class.getName());

try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);

while (pool.getTotalConnections() < 5) {
quietlySleep(100L);
}

try (Connection connection = ds.getConnection()) {
assertNotNull(connection);

PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
assertNotNull(statement);

ResultSet resultSet = statement.executeQuery();
assertNotNull(resultSet);

try {
statement.getMaxRows();
} catch (Exception e) {
assertSame(SQLException.class, e.getClass());
}
}

assertEquals("Total connections not as expected", 4, pool.getTotalConnections());
assertEquals("Idle connections not as expected", 4, pool.getIdleConnections());
}
}


@Test
public void testEvictAllRefill() throws Exception {
HikariConfig config = newHikariConfig();
Expand Down Expand Up @@ -908,4 +947,19 @@ public Override adjudicate(SQLException sqlException) {
return (sqlException.getSQLState().equals("08999")) ? Override.DO_NOT_EVICT : Override.CONTINUE_EVICT;
}
}

public static class OverrideHandler2 implements SQLExceptionOverride
{
@java.lang.Override
public Override adjudicate(SQLException sqlException) {
return (sqlException.getSQLState().equals("HY000") && sqlException.getErrorCode() == 1290)
? Override.CONTINUE_EVICT
: Override.DO_NOT_EVICT;
}

@java.lang.Override
public boolean adjudicateAnyway() {
return true;
}
}
}