Class TransactionFactory
- java.lang.Object
-
- org.linuxforhealth.fhir.database.utils.transaction.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.
UsageIConnectionProvider cp = getConnectionProvider(...); try (ITransaction tx = TransactionFactory.open(cp)) { try { doStuff(); } catch (Exception x) { tx.setRollbackOnly(); } }
The transaction will commit whenAutoCloseable.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static void
clearTransaction()
Remove the transaction from the current thread.static TransactionFactory
getInstance()
Singleton instance getterstatic ITransaction
getTransaction()
Get the current transaction.static ITransaction
openTransaction(IConnectionProvider cp)
Open a transaction on this thread
-
-
-
Method Detail
-
getInstance
public static TransactionFactory getInstance()
Singleton instance getter- Returns:
-
openTransaction
public static ITransaction openTransaction(IConnectionProvider cp)
Open a transaction on this thread- 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 fromSimpleTransaction.close()
.
-
-