Class TransactionHolder

java.lang.Object
io.github.blyznytsiaorg.bibernate.transaction.TransactionHolder

public class TransactionHolder extends Object
The TransactionHolder class is a utility for managing thread-local transactions. It uses a ThreadLocal variable to store and retrieve transactions associated with a particular thread. This class is designed to be used in scenarios where transactional information needs to be accessible within the scope of a single thread.

The class provides methods for getting, setting, and removing transactions associated with the current thread.

The class uses a ThreadLocal variable to ensure that each thread has its own isolated transaction reference, preventing interference between threads.

Since:
1.0
Author:
Blyzhnytsia Team
See Also:
  • Field Details

    • transactionHolder

      private static final ThreadLocal<Transaction> transactionHolder
      A ThreadLocal variable for storing transactions associated with a specific thread.
  • Constructor Details

    • TransactionHolder

      public TransactionHolder()
  • Method Details

    • getTransaction

      public static Transaction getTransaction()
      Retrieves the transaction associated with the current thread, if any.
      Returns:
      The transaction associated with the current thread, or null if no transaction is associated
    • setTransaction

      public static void setTransaction(Transaction transaction)
      Sets the transaction for the current thread, allowing transactions to be associated with the execution context of a thread.
      Parameters:
      transaction - The transaction to associate with the current thread
    • removeTransaction

      public static void removeTransaction()
      Removes the transaction association for the current thread. This method is typically used to clean up resources associated with a thread-local transaction.