Class ConnectionOwnership

java.lang.Object
org.dbunit.database.connection.ConnectionOwnership

public class ConnectionOwnership extends Object
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:

  1. closeConnectionAfterTest - the @DbUnitConfig flag, true by default. false means a real owner exists elsewhere - a CachingConnectionProvider, an externally-supplied fixed connection - and it, not this lifecycle, closes the connection.
  2. testerListener - the tester's IOperationListener. When it is (directly, or wrapped by a ConnectionPreservingOperationListener) IOperationListener.NO_OP_OPERATION_LISTENER, that is the established, pre-existing signal that the connection is managed elsewhere and must not be closed.
  3. 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, its configureTest() completed. When it did not - the injected instance threw before configureTest(), so its own postTest()/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 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 time mayClose() is called.
      Parameters:
      closeConnectionAfterTest - Supplies the @DbUnitConfig flag.
      testerListener - Supplies the tester's current IOperationListener; may supply null, 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:
      true only when the closeConnectionAfterTest flag is set, the lifecycle actually ran, and the tester's listener does not unwrap to the no-op listener.