Package dev.getelements.elements.sdk.dao
Interface Transaction
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
MorphiaGuiceTransaction
Represents a transaction with the underlying datastore. This transaction object is used to acquire DAOs, perform
operations and then commit or rollback the transaction. This transaction assumes a snapshot and retry model where
transactions may fail due to transient conditions and can be retried safely. When a transaction fails due to a
transient condition, the
commit() method will throw a Transaction.RetryException. The caller should then
rollback the transaction, wait for the recommended delay and then restart and re-execute the entire transaction.
Specific implementations may provide additional guarantees, such as serializable isolation, or may not require
rollback and retry semantics. Consult the documentation for the specific implementation for details. In such
cases, the Transaction.RetryException may never be thrown and code relying on it should be aware that it may not be
and handle that case appropriately. Typically this means that the operation will simply be executed once and
the exception code is completely vestigial.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classIndicates that a transaction failed due to a transient condition and can be retried safely. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes and releases the transaction.voidcommit()Commits the transaction.<DaoT> DaoTGets the requested Dao type, bound to thisTransaction.booleanisActive()Returns true if this transaction is still active and open.default <T> TperformAndClose(Function<Transaction, T> op) Performs the operation on thisTransaction, processing the result and returning the result of the operation.default voidPerforms the operation on thisTransaction, processing the result and returning the result of the operation.voidrollback()Rolls back the transaction undoing any pending changes to the database.voidstart()Starts the transaction.default <T> Function<Transaction, T> wrap(Function<Transaction, T> op) Optionally, wraps the transaction operation in anotherFunctionto properly handle any retry exceptions specific to the transaction's implementation.
-
Method Details
-
getDao
Gets the requested Dao type, bound to thisTransaction. The returned DAO is only valid for the life of thisTransactionobject.- Type Parameters:
DaoT- the DAO Type- Parameters:
daoT- theClass<DaoT>- Returns:
- the DAO Instance
-
commit
Commits the transaction. In the event the transaction fails due to a transient condition, aTransaction.RetryExceptionis thrown. The caller should then rollback the transaction, wait for the recommended delay and then restart and re-execute the entire transaction.- Throws:
Transaction.RetryException- if the transaction failed due to a transient condition and can be retried safely.
-
start
void start()Starts the transaction. Note, transactions are stared automatically when the Transaction object is created so this is only necessary if the transaction has been explicitly aborted via rollback operation. -
rollback
void rollback()Rolls back the transaction undoing any pending changes to the database. -
close
void close()Closes and releases the transaction. Once closed, the transaction is no longer active and any DAOs acquired will have undefined behavior if used.- Specified by:
closein interfaceAutoCloseable
-
isActive
boolean isActive()Returns true if this transaction is still active and open.- Returns:
- true if active, false if it has been committed or rolled back.
-
performAndClose
Performs the operation on thisTransaction, processing the result and returning the result of the operation. For transactions requiring retry semantics, this method will automatically retry the operation until it succeeds, or fails with an exception other thanTransaction.RetryException. The transaction is also closed automatically at the end of the operation regardless of success or failure. For implementations that do not require retry semantics, the operation is simply executed once and the transaction closed. Implementations that do not require retry semantics may provide a more succinct implementation of this default method.- Type Parameters:
T- the return type- Parameters:
op- the operation- Returns:
- the result of the operation
-
wrap
Optionally, wraps the transaction operation in anotherFunctionto properly handle any retry exceptions specific to the transaction's implementation. This ensures that any executed code will properly honor the semantics ofTransaction.RetryException.- Type Parameters:
T- the return type- Parameters:
op- the operation- Returns:
- the wrapped function, or the original function
-
performAndCloseV
Performs the operation on thisTransaction, processing the result and returning the result of the operation.- Parameters:
op- the operation
-