Class TestScopedConnection
ConnectionOwnership decides.
Not thread-safe; a test's steps run sequentially on one thread.
- Since:
- 3.6.0
- Author:
- Jeff Jensen
-
Constructor Summary
ConstructorsConstructorDescriptionTestScopedConnection(Callable<IDatabaseConnection> source, ConnectionOwnership ownership, Consumer<IDatabaseConnection> onAcquired) Creates a test-scoped connection. -
Method Summary
Modifier and TypeMethodDescriptionvoidadopt(IDatabaseConnection connection) Takes over a connection acquired elsewhere - the executor's listener piggyback, which memoizes whatever connection the tester's ownonSetup()retrieved - as ifgetConnection()had returned it.voidForgets the memoized connection so the nextgetConnection()acquires a fresh one.Returns the connection for this test, acquiring it from the supplier on first use and returning the same one thereafter.booleanReturns whethergetConnection()oradopt(IDatabaseConnection)has run this test, regardless of whether the resulting connection isnull.Returns the connection already acquired this test, ornullboth beforegetConnection()first runs and when the supplier had none - never triggers acquisition.voidrelease()Closes the connection whenConnectionOwnership.mayClose()allows it and it is not already closed; a no-op when no connection was ever acquired.voidreleaseSuppressing(Throwable primary) Releases the connection, attaching any close failure toprimaryviaThrowable.addSuppressed(Throwable)rather than letting it replace the more useful failure already in flight, then discards the memo.
-
Constructor Details
-
TestScopedConnection
public TestScopedConnection(Callable<IDatabaseConnection> source, ConnectionOwnership ownership, Consumer<IDatabaseConnection> onAcquired) Creates a test-scoped connection.- Parameters:
source- Acquires the connection on first use; may returnnull(e.g. a test double with no connection to offer).ownership- Decides, atrelease()time, whether this lifecycle may close the connection.onAcquired- Run once on each connectionsourcereturns, immediately after acquisition (e.g. to apply@DbUnitPropertyvalues, or warn on an autocommit-off connection); not run for anullconnection, nor for oneadopt(IDatabaseConnection)takes over. May benull.
-
-
Method Details
-
getConnection
Returns the connection for this test, acquiring it from the supplier on first use and returning the same one thereafter. A memoized connection that has since been closed - by a pool max-lifetime, a server reap, aCachingConnectionProvider.close()between reused test methods - is dropped and re-acquired rather than handed back dead.- Returns:
- The connection, or
nullwhen the supplier has none to offer. - Throws:
Exception- If the supplier fails.
-
peekConnection
Returns the connection already acquired this test, ornullboth beforegetConnection()first runs and when the supplier had none - never triggers acquisition.- Returns:
- The memoized connection, or
null.
-
isResolved
public boolean isResolved()Returns whethergetConnection()oradopt(IDatabaseConnection)has run this test, regardless of whether the resulting connection isnull.- Returns:
trueonce a connection (possiblynull) has been resolved.
-
adopt
Takes over a connection acquired elsewhere - the executor's listener piggyback, which memoizes whatever connection the tester's ownonSetup()retrieved - as ifgetConnection()had returned it.onAcquiredis not run: the caller doing the piggyback has already done that connection's post-acquisition setup.- Parameters:
connection- The connection to adopt.
-
release
Closes the connection whenConnectionOwnership.mayClose()allows it and it is not already closed; a no-op when no connection was ever acquired. Leaves the memo in place - a single-use holder is discarded with its owner, and a reused one relies onrevalidateOnReacquireto drop the now-closed connection on the nextgetConnection().When it does close, it forgets the connection too, so the next
getConnection()on a reused holder acquires a fresh one. When ownership says leave it -closeConnectionAfterTest=false, a no-op listener - the memo is kept for its real owner.The already-closed guard matters when this holder's connection is the same object another owner also closes - the
@DbUnitExpectedpath, where the executor's borrowed connection andDefaultPrepAndExpectedTestCase's are one and the same. Anullunderlying JDBC connection is treated as nothing to close, the same waygetConnection()'s liveness check does; aSQLExceptionfrom the check is left to propagate rather than swallowed: a connection whoseisClosed()throws has failed in a way worth surfacing (as a suppressed exception, viareleaseSuppressing(Throwable)).An uncommitted transaction is rolled back first when the connection's autocommit is off - see
rollBackOpenTransaction(Connection).- Throws:
Exception- If closing the connection, or checking whether it is already closed, fails.
-
releaseSuppressing
Releases the connection, attaching any close failure toprimaryviaThrowable.addSuppressed(Throwable)rather than letting it replace the more useful failure already in flight, then discards the memo.- Parameters:
primary- The failure already being thrown, to attach a close failure to.
-
discardOnFailure
public void discardOnFailure()Forgets the memoized connection so the nextgetConnection()acquires a fresh one. For use after a lifecycle step failed: the step that just threw may have left the connection broken, so the next step should not reuse it.
-