Class AutoCloseableElement

java.lang.Object
io.agroal.pool.util.AutoCloseableElement
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
ConnectionWrapper, ResultSetWrapper, StatementWrapper, XAConnectionWrapper, XAResourceWrapper

public abstract class AutoCloseableElement extends Object implements AutoCloseable
Abstract class to track auto-closeable resources by having them forming a collection between themselves. This class is designed to prevent leaks of Agroal JDBC wrapper objects. Once a new wrapper is created it tries to insert itself between the head and the first element of the list (if it exists). There is the invariant that at any given point in time the list can be traversed from the head and all inserted elements are reachable. As an implementation detail, the collection formed is actually a stack (FILO behaviour) and is thread-safe.

The resources do not remove themselves on close, rather there is a pruneClosed() method that must be called to remove subsequent closed elements.

Author:
Luis Barreiro
  • Constructor Details

  • Method Details

    • isClosed

      public abstract boolean isClosed() throws Exception
      Throws:
      Exception
    • internalClosed

      protected abstract boolean internalClosed()
    • closeAllAutocloseableElements

      public int closeAllAutocloseableElements()
      Returns the number of resources that were not properly closed. The resources are closed in the process and the collection is cleared. This method should be invoked on the collection head only, otherwise it may not traverse the whole collection.
    • pruneClosed

      public void pruneClosed()
      Check for runs of closed elements after the current position and remove them from the linked list
    • newHead

      public static AutoCloseableElement newHead()
      Create a special marker element to be used as head of a collection.