Class TransactionManager
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidbeginTransaction(Plugin plugin) Begin a transaction for the current thread using the pool of a specific plugin with the default transaction isolation.static voidbeginTransaction(Plugin plugin, int nTransactionIsolation) Begin a transaction for the current thread using the pool of a specific pluginstatic voidclear()Clear the ThreadLocal context associated with the current thread.static voidcommitTransaction(Plugin plugin) Commit the transaction associated to the pool of a given plugin.static MultiPluginTransactiongetCurrentTransaction(Plugin plugin) Get the current transaction for the pool of a given plugin.static voidLog all uncommitted transactions in the current thread with detailed information, rollback them automatically to prevent data inconsistencies, and clear the ThreadLocal.static voidRoll back a transaction associated to the pool of a given plugin.static voidRoll back a transaction associated to the pool of a given plugin with an exception.static voidRoll back every transactions opened by the current thread.static voidRoll back every transactions opened by the current thread.
-
Method Details
-
beginTransaction
Begin a transaction for the current thread using the pool of a specific plugin with the default transaction isolation. The default transaction isolation isConnection.TRANSACTION_READ_COMMITTED.
Note that only one transaction may be active at a time by pool for each thread.- Parameters:
plugin- The plugin to use the pool of, or null to use the default pool.
-
beginTransaction
Begin a transaction for the current thread using the pool of a specific plugin- Parameters:
plugin- The plugin to use the pool of, or null to use the default pool.nTransactionIsolation- The transaction isolation. ViewConnection.setTransactionIsolation(int)to view the different available transaction isolations.
-
getCurrentTransaction
Get the current transaction for the pool of a given plugin.- Parameters:
plugin- The plugin to use the pool of, or null to use the default pool.- Returns:
- The transaction, or null if no transaction is currently running.
-
commitTransaction
Commit the transaction associated to the pool of a given plugin.- Parameters:
plugin- The plugin associated to the pool to commit the transaction of, or null to use the default pool.
-
rollBack
Roll back a transaction associated to the pool of a given plugin. Note that any plugin can roll a transaction back.- Parameters:
plugin- The plugin associated to the pool to roll back the transaction of, or null to use the default pool.
-
rollBack
Roll back a transaction associated to the pool of a given plugin with an exception.- Parameters:
plugin- The plugin associated to the pool to roll back the transaction of, or null to use the default pool.e- The exception to associates with the roll back.
-
rollBackEveryTransaction
public static void rollBackEveryTransaction()Roll back every transactions opened by the current thread. This method attempt to prevent connection leak. -
rollBackEveryTransaction
Roll back every transactions opened by the current thread. This method attempt to prevent connection leak.- Parameters:
e- The exception that occurs and that may have prevent transaction from being properly closed (committed or roll backed)
-
logAndRollbackUncommittedTransactions
public static void logAndRollbackUncommittedTransactions()Log all uncommitted transactions in the current thread with detailed information, rollback them automatically to prevent data inconsistencies, and clear the ThreadLocal.This method should be called at the end of a request in a servlet filter to detect transactions that were started but not properly committed.
-
clear
public static void clear()Clear the ThreadLocal context associated with the current thread.This method removes the internal ThreadLocal Map that stores the active transactions for the current thread. It MUST be called at the end of each request in a web application environment (typically in a servlet filter, inside a
finallyblock).Failing to call this method may cause serious issues, including:
- Memory leaks due to ThreadLocal values remaining attached to threads from the servlet container thread pool.
- Cross-request contamination, where transactions or transaction states from a previous request persist and interfere with the logic of subsequent requests handled by the same thread.
- Incorrect behavior in multi-instance or multi-tenant deployments, since ThreadLocal content may belong to a different instance.
This method does NOT attempt to commit or roll back any open transactions. It simply clears the ThreadLocal reference. Therefore, it should be used only after explicitly calling
commitTransaction(Plugin)orrollBackEveryTransaction(Throwable)depending on the request outcome.Typical usage inside a filter:
try { chain.doFilter(request, response); } catch (Exception e) { TransactionManager.rollBackEveryTransaction(e); throw e; } finally { TransactionManager.clear(); }- Since:
- 8.0.0
-