-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1489 allow specification of a user supplied exception eviction…
… override class
- Loading branch information
1 parent
76fc5d6
commit a51e6a0
Showing
6 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.zaxxer.hikari; | ||
|
||
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. | ||
*/ | ||
public interface SQLExceptionOverride { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
enum Override { | ||
CONTINUE_EVICT, | ||
DO_NOT_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. | ||
* | ||
* @param sqlException the #SQLException to adjudicate | ||
* @return either one of {@link Override#CONTINUE_EVICT} or {@link Override#DO_NOT_EVICT} | ||
*/ | ||
default Override adjudicate(final SQLException sqlException) | ||
{ | ||
return Override.CONTINUE_EVICT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import com.zaxxer.hikari.HikariPoolMXBean; | ||
import com.zaxxer.hikari.SQLExceptionOverride; | ||
import com.zaxxer.hikari.mocks.StubConnection; | ||
import com.zaxxer.hikari.mocks.StubDataSource; | ||
import com.zaxxer.hikari.mocks.StubStatement; | ||
|
@@ -40,6 +41,7 @@ | |
/** | ||
* @author Brett Wooldridge | ||
*/ | ||
@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) | ||
public class TestConnections | ||
{ | ||
@Before | ||
|
@@ -258,6 +260,81 @@ public void testEviction() throws Exception | |
} | ||
} | ||
|
||
@Test | ||
public void testEviction2() throws SQLException | ||
This comment has been minimized.
Sorry, something went wrong.
westse
|
||
{ | ||
HikariConfig config = newHikariConfig(); | ||
config.setMaximumPoolSize(5); | ||
config.setConnectionTimeout(2500); | ||
config.setConnectionTestQuery("VALUES 1"); | ||
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); | ||
config.setExceptionOverrideClassName(OverrideHandler.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.getMaxFieldSize(); | ||
} catch (Exception e) { | ||
assertSame(SQLException.class, e.getClass()); | ||
} | ||
} | ||
|
||
assertEquals("Total connections not as expected", 5, pool.getTotalConnections()); | ||
assertEquals("Idle connections not as expected", 5, pool.getIdleConnections()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEviction3() throws SQLException | ||
{ | ||
HikariConfig config = newHikariConfig(); | ||
config.setMaximumPoolSize(5); | ||
config.setConnectionTimeout(2500); | ||
config.setConnectionTestQuery("VALUES 1"); | ||
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); | ||
|
||
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.getMaxFieldSize(); | ||
} 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(); | ||
|
@@ -735,12 +812,13 @@ public void testMinimumIdleZero() throws SQLException | |
} | ||
} | ||
|
||
class StubDataSourceWithErrorSwitch extends StubDataSource { | ||
static class StubDataSourceWithErrorSwitch extends StubDataSource | ||
{ | ||
private boolean errorOnConnection = false; | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Connection getConnection() throws SQLException { | ||
public Connection getConnection() { | ||
if (!errorOnConnection) { | ||
return new StubConnection(); | ||
} | ||
|
@@ -753,4 +831,11 @@ public void setErrorOnConnection(boolean errorOnConnection) { | |
} | ||
} | ||
|
||
public static class OverrideHandler implements SQLExceptionOverride | ||
{ | ||
@java.lang.Override | ||
public Override adjudicate(SQLException sqlException) { | ||
return (sqlException.getSQLState().equals("08999")) ? Override.DO_NOT_EVICT : Override.CONTINUE_EVICT; | ||
} | ||
} | ||
} |
why not pass SQLExceptionOverride directly?