Package org.dbunit.database.connection
Class ConnectionOwnership
java.lang.Object
org.dbunit.database.connection.ConnectionOwnership
Decides one question, the same way for every caller: may whatever is driving a test's dbUnit
lifecycle close the connection that lifecycle borrowed, once the test is over? A
TestScopedConnection consults its ConnectionOwnership in
TestScopedConnection.release().
Three inputs, each read fresh at release time since the tester's listener and the borrowing lifecycle's own state can both still change after this is constructed:
closeConnectionAfterTest- the@DbUnitConfigflag,trueby default.falsemeans a real owner exists elsewhere - aCachingConnectionProvider, an externally-supplied fixed connection - and it, not this lifecycle, closes the connection.testerListener- the tester'sIOperationListener. When it is (directly, or wrapped by aConnectionPreservingOperationListener)IOperationListener.NO_OP_OPERATION_LISTENER, that is the established, pre-existing signal that the connection is managed elsewhere and must not be closed.borrowingLifecycleRan- whether the borrowing lifecycle actually reached the point of owning the connection. On the setup/teardown path a connection was actually acquired; on the prep/expected path with an injected@DbUnitTestCase, itsconfigureTest()completed. When it did not - the injected instance threw beforeconfigureTest(), so its ownpostTest()/cleanupData()never ran to close the connection its way - closing here would strand that reused instance holding a closed connection.
Truth table - "close" only when every input allows it:
closeConnectionAfterTest | listener unwraps to NO_OP | borrowing lifecycle ran | -> decision
------------------------- | ------------------------ | ----------------------- | -----------
false | - | - | SKIP
true | true | - | SKIP
true | false | false | SKIP
true | false | true | CLOSE
TestScopedConnection.release() additionally skips a connection that is already
closed - an idempotency guard, not part of this decision: on the @DbUnitExpected
path the executor's borrowed connection and DefaultPrepAndExpectedTestCase's
are the same object, and whichever releases first closes it.
- Since:
- 3.6.0
- Author:
- Jeff Jensen
-
Constructor Summary
ConstructorsConstructorDescriptionConnectionOwnership(BooleanSupplier closeConnectionAfterTest, Supplier<IOperationListener> testerListener, BooleanSupplier borrowingLifecycleRan) Creates an ownership decision from its three inputs, each a supplier read fresh every timemayClose()is called. -
Method Summary
Modifier and TypeMethodDescriptionbooleanmayClose()Returns whether the borrowing lifecycle may close the connection it borrowed.
-
Constructor Details
-
ConnectionOwnership
public ConnectionOwnership(BooleanSupplier closeConnectionAfterTest, Supplier<IOperationListener> testerListener, BooleanSupplier borrowingLifecycleRan) Creates an ownership decision from its three inputs, each a supplier read fresh every timemayClose()is called.- Parameters:
closeConnectionAfterTest- Supplies the@DbUnitConfigflag.testerListener- Supplies the tester's currentIOperationListener; may supplynull, treated as "not the no-op listener".borrowingLifecycleRan- Supplies whether the borrowing lifecycle reached the point of owning the connection.
-
-
Method Details
-
mayClose
public boolean mayClose()Returns whether the borrowing lifecycle may close the connection it borrowed.- Returns:
trueonly when thecloseConnectionAfterTestflag is set, the lifecycle actually ran, and the tester's listener does not unwrap to the no-op listener.
-