Class LeftOpenConnectionWarning

java.lang.Object
org.dbunit.database.connection.LeftOpenConnectionWarning
All Implemented Interfaces:
Consumer<IDatabaseConnection>

public class LeftOpenConnectionWarning extends Object implements Consumer<IDatabaseConnection>
Logs one warning, the first time it sees a connection that is going to be left open while the JdbcDatabaseTester that supplied it creates a new connection on every call. A connection is left open for its real owner to close, as ConnectionOwnership.isLeftOpenByConfiguration() decides: the closeConnectionAfterTest flag is false, or an operation listener is the no-op one. A JdbcDatabaseTester built without a CachingConnectionProvider has no such owner: nothing else holds the connection it just created, and the next call creates another. A test case used for one test leaves one open after it, and one reused across tests keeps one and leaves another each time a step fails, so a long run can exhaust the database's connections while its tests pass.

Only a JdbcDatabaseTester is judged, through JdbcDatabaseTester.isReusingConnection(). Another tester, such as a DataSourceDatabaseTester, cannot be told apart from one whose data source hands back the same connection, as a transaction-aware one does, so it is left alone.

Used as an onAcquired hook of a TestScopedConnection, next to AutoCommitOffWarning, so the check runs where a connection arrives. One instance per lifecycle owner; it warns at most once regardless of how many times the connection is (re)acquired. The tester is checked before the ownership is asked anything, so a tester that is not a JdbcDatabaseTester sees no extra calls.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • LeftOpenConnectionWarning

      public LeftOpenConnectionWarning(ConnectionOwnership ownership, Supplier<IDatabaseTester> tester)
      Creates the warning for one lifecycle owner.
      Parameters:
      ownership - Decides whether the configuration leaves the connection open.
      tester - Supplies the database tester the connection comes from, read fresh each time as it can change between tests; may supply null.
  • Method Details

    • accept

      public void accept(IDatabaseConnection connection)
      Warns when the connection is going to be left open and its tester creates a new connection on every call, unless a warning has already been logged by this instance.
      Specified by:
      accept in interface Consumer<IDatabaseConnection>
      Parameters:
      connection - The connection just acquired; not inspected, as it is the tester and the ownership that decide.