Package org.dbunit

Class ConnectionPreservingOperationListener

java.lang.Object
org.dbunit.ConnectionPreservingOperationListener
All Implemented Interfaces:
IOperationListener

public class ConnectionPreservingOperationListener extends Object implements IOperationListener
An IOperationListener that forwards every callback to a delegate except a close of a connection whose lifecycle is owned elsewhere: it drops operationSetUpFinished/operationTearDownFinished for the connection (or connections) it protects, so the delegate's DefaultOperationListener-style close cannot run on one still in use for a later step - a row count check verify, a connection injected into the test method, or the shared connection DefaultPrepAndExpectedTestCase drives its whole lifecycle on.

Two shapes: a blanket listener protects every connection (the caller owns them all), and a scoped one protects the single connection its Supplier names, re-evaluated on each callback since that connection may not be resolved yet when the listener is installed; any other connection has its callbacks forwarded unchanged, so a tester handing out a fresh connection per operation still has each of those closed right after its own operation.

The shield suppresses the whole operationSetUpFinished/ operationTearDownFinished callback for a protected connection, not only its connection.close(): IOperationListener has no way to signal "do your other work but skip the close". This is exactly right for the delegates dbUnit ships - DefaultOperationListener (closes, nothing else) and IOperationListener.NO_OP_OPERATION_LISTENER (does nothing). A custom delegate that also does non-close work in those callbacks (commit a transaction, release a lock, record a metric) does not see them for a protected connection; do that work from connectionRetrieved instead, which is always forwarded, or manage the connection entirely in the delegate and do not protect it here.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • ConnectionPreservingOperationListener

      public ConnectionPreservingOperationListener(IOperationListener delegate)
      Creates a blanket listener that protects every connection from a close by the delegate, forwarding only connectionRetrieved.
      Parameters:
      delegate - The listener to forward to.
    • ConnectionPreservingOperationListener

      public ConnectionPreservingOperationListener(IOperationListener delegate, Supplier<IDatabaseConnection> protectedConnection)
      Creates a listener that protects the one connection protectedConnection supplies, forwarding every callback for any other connection.
      Parameters:
      delegate - The listener to forward to.
      protectedConnection - Supplies the connection to protect, re-read on each callback; may return null before that connection is resolved, protecting nothing until it is.
  • Method Details