Class TransactionFactory


  • public class TransactionFactory
    extends java.lang.Object
    Factory to encapsulate handling of transactions, of which there should be only one active on a thread at a time. This helps to ensure consistent (and correct) commit/rollback handling, which otherwise can get a bit messy.
    Usage IConnectionProvider cp = getConnectionProvider(...); try (ITransaction tx = TransactionFactory.open(cp)) { try { doStuff(); } catch (Exception x) { tx.setRollbackOnly(); } } The transaction will commit when AutoCloseable.close() is called, unless setRollbackOnly() was called, in which case a rollback will be performed instead.
    To be useable in a JEE context, a light refactor is needed so that an alternative solution can be implemented to use UserTransaction.
    • Method Detail

      • getInstance

        public static TransactionFactory getInstance()
        Singleton instance getter
        Returns:
      • getTransaction

        public static ITransaction getTransaction()
        Get the current transaction.
        Returns:
        the transaction currently open on this thread
        Throws:
        java.lang.IllegalStateException - if no transaction is open
      • clearTransaction

        protected static void clearTransaction()
        Remove the transaction from the current thread. Called from SimpleTransaction.close().