Class AutoCloseableElement<T extends AutoCloseableElement<T>>
java.lang.Object
io.agroal.pool.util.AutoCloseableElement<T>
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
ConnectionWrapper, ResultSetWrapper, StatementWrapper, XAConnectionWrapper, XAResourceWrapper
public abstract class AutoCloseableElement<T extends AutoCloseableElement<T>>
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidintReturns the number of resources that were not properly closed.intReturns the number of resources that were not held and are not properly closed.protected AutoCloseableElement<T> protected abstract booleanabstract booleanisClosed()booleanCheck if the list of elements is emptybooleanisHeld()static <T extends AutoCloseableElement<T>>
AutoCloseableElement<T> newHead()Create a special marker element to be used as head of a collection.voidCheck for runs of closed elements after the current position and remove them from the linked listprotected AutoCloseableElement<T> protected booleansetNextElement(AutoCloseableElement<T> expected, AutoCloseableElement<T> element) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AutoCloseable
close
-
Constructor Details
-
AutoCloseableElement
-
-
Method Details
-
isHeld
public boolean isHeld() -
isClosed
-
internalClosed
protected abstract boolean internalClosed() -
beforeClose
protected void beforeClose() -
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. -
closeNotHeldAutocloseableElements
public int closeNotHeldAutocloseableElements()Returns the number of resources that were not held and are not properly closed. After run the resources that are not held are closed and remove from the list. This method should be invoked on the collection head only, otherwise it may not traverse the whole collection. -
isElementListEmpty
public boolean isElementListEmpty()Check if the list of elements is empty -
setNextElement
-
getNextElement
-
resetNextElement
-
pruneClosed
public void pruneClosed()Check for runs of closed elements after the current position and remove them from the linked list -
newHead
Create a special marker element to be used as head of a collection.
-