Interface Transactional
- All Known Implementing Classes:
DatabaseConfiguration,PropertiesFileConfiguration,TransactionalCompositeConfiguration
public interface Transactional
An object of which a thread can group the accesses into a transaction. A transaction belongs to
the thread which starts it. A nested start on the same thread joins the transaction and opens
another level of it, and the outermost start sets whether the transaction is read-only. Only the
outermost level commits, when it is finished, or rolls back, when it is closed without being
finished; a joined level therefore shares the outcome of the outermost one. What committing and
rolling back store, and what a transaction guarantees towards other threads, depend on the
implementation.
Every started level has to be closed in a finally block:
obj.startTransaction(false);
try
{
// The accesses grouped into the transaction.
obj.finishTransaction();
}
finally
{
obj.closeTransaction();
}
Misusing a transaction throws an IllegalStateException: finishing or closing without
a level open on the calling thread, finishing a level twice, starting a writable level or writing
within a read-only transaction, as well as starting a level or writing after the outermost level
has been finished.
-
Method Summary
Modifier and TypeMethodDescriptionvoidCloses the current level of the transaction of the calling thread.voidFinishes the current level of the transaction of the calling thread.voidstartTransaction(boolean readOnly) Starts a transaction of the calling thread on this object.
-
Method Details
-
startTransaction
void startTransaction(boolean readOnly) Starts a transaction of the calling thread on this object. If the calling thread has started one already, this joins it and opens another level. A failed start leaves no level behind.- Parameters:
readOnly- Whether the transaction only reads values. This is set by the outermost start; a nested read-only level joins a writable transaction.- Throws:
IllegalStateException- If a writable level is to be started within a read-only transaction or the outermost level has been finished already, anIllegalStateExceptionwill be thrown.RuntimeException- If acquiring what the transaction holds fails, for instance a database connection, aRuntimeExceptionspecific to the implementation will be thrown.
-
finishTransaction
void finishTransaction()Finishes the current level of the transaction of the calling thread. Finishing the outermost level commits the transaction, finishing a nested level only ends that level. The level still has to be closed. If committing fails, the outermost level remains unfinished, so that closing it rolls the transaction back.- Throws:
IllegalStateException- If the calling thread has no transaction on this object or its current level has been finished already, anIllegalStateExceptionwill be thrown.RuntimeException- If committing the transaction fails, aRuntimeExceptionspecific to the implementation will be thrown.
-
closeTransaction
void closeTransaction()Closes the current level of the transaction of the calling thread. Closing the outermost level rolls the transaction back unless it has been finished, and releases what the transaction holds. This belongs in afinallyblock. So as not to hide the exception which skipped finishing, a failure when rolling back or releasing is logged instead of thrown.- Throws:
IllegalStateException- If the calling thread has no transaction on this object, anIllegalStateExceptionwill be thrown.
-