Class CachingConnectionProvider
IDatabaseConnection so that it - and the table
metadata it accumulates - can be reused across many test methods instead of
being rebuilt on every call.
A Callable supplies the connection-creation logic; the factory is
only invoked when there is no cached connection yet, or when the
previously cached one is no longer alive. A dead connection is closed and transparently replaced rather than
returned to the caller or left for the next call to fail on.
Usage. Construct one instance per target database and share that
same instance across the JdbcDatabaseTester,
DataSourceDatabaseTester, JndiDatabaseTester
or DefaultDatabaseTester instances that are created for
each test, for example via a static field on a common test base
class:
private static final CachingConnectionProvider CONNECTION_PROVIDER =
new CachingConnectionProvider();
@BeforeEach
void setUp() throws Exception
{
final IDatabaseTester tester = new JdbcDatabaseTester(driverClass,
connectionUrl, username, password, schema, CONNECTION_PROVIDER);
// The default IOperationListener closes the connection after every
// onSetup()/onTearDown() call, which would defeat the cache. Pair it
// with a listener that leaves the connection open, e.g.:
tester.setOperationListener(IOperationListener.NO_OP_OPERATION_LISTENER);
...
}
Thread safety. Access to the cached connection is synchronized, so
concurrent callers cannot create or replace it at the same time. The
returned IDatabaseConnection - and the underlying JDBC
Connection it wraps - is however not synchronized, so this class is
only appropriate for test suites that run sequentially, not for test
methods executing concurrently against the same cached connection.
Metadata staleness. Because the whole point of reuse is to avoid
re-fetching table metadata, a cached connection's DatabaseDataSet
does not notice schema changes (new/dropped/altered tables) made after it
was first cached. Do not share a provider across tests that alter DDL
mid-run.
- Since:
- 3.4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault number of secondsgetConnection(Callable)allowsConnection.isValid(int)to take when checking whether the cached connection is still alive. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a provider that validates the cached connection with thedefault validation timeout.CachingConnectionProvider(int validationTimeoutSeconds) Creates a provider that validates the cached connection with the given timeout. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes and discards the cached connection, if any.getConnection(Callable<IDatabaseConnection> connectionFactory) Returns the cached connection, creating it with the given factory on the first call and again whenever the previously cached connection is no longer alive.toString()
-
Field Details
-
DEFAULT_VALIDATION_TIMEOUT_SECONDS
public static final int DEFAULT_VALIDATION_TIMEOUT_SECONDSDefault number of secondsgetConnection(Callable)allowsConnection.isValid(int)to take when checking whether the cached connection is still alive.- See Also:
-
-
Constructor Details
-
CachingConnectionProvider
public CachingConnectionProvider()Creates a provider that validates the cached connection with thedefault validation timeout. -
CachingConnectionProvider
public CachingConnectionProvider(int validationTimeoutSeconds) Creates a provider that validates the cached connection with the given timeout.- Parameters:
validationTimeoutSeconds- The number of secondsConnection.isValid(int)is allowed to take when checking whether the cached connection is still alive. Zero means no timeout is applied.
-
-
Method Details
-
getConnection
public IDatabaseConnection getConnection(Callable<IDatabaseConnection> connectionFactory) throws Exception Returns the cached connection, creating it with the given factory on the first call and again whenever the previously cached connection is no longer alive.- Parameters:
connectionFactory- Creates a new connection. Only invoked when there is no live cached connection to reuse.- Returns:
- The cached, live connection.
- Throws:
Exception- IfconnectionFactorythrows while creating a new connection.
-
close
Closes and discards the cached connection, if any. The next call togetConnection(Callable)creates a fresh one.- Throws:
SQLException- If closing the cached connection fails.
-
toString
-