Class ThreadTransactions<R>
java.lang.Object
de.aristaflow.adept2.util.threading.ThreadTransactions<R>
- Type Parameters:
R- The type of the resource of a transaction.
The transactions of the threads on a
Transactional, at most one per thread. This counts
the levels of a transaction and enforces the protocol of the interface, whereas the
implementation provides what a transaction holds, its resource: the outermost start acquires it,
the outermost finish commits it and the outermost close releases it. A failed acquisition leaves
no level behind. A failed commit leaves the outermost level unfinished, so that closing it rolls
the transaction back as after any other failure within it.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReleases the resource of a transaction when its outermost level is closed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks whether the calling thread may write.voidclose(ThreadTransactions.Release<? super R> release) Closes the current level of the transaction of the calling thread.voidFinishes the current level of the transaction of the calling thread.Gets the resource of the transaction of the calling thread.voidStarts a transaction of the calling thread or, if it has one already, joins it and opens another level.
-
Constructor Details
-
ThreadTransactions
public ThreadTransactions()Creates a new instance without any transaction.
-
-
Method Details
-
start
Starts a transaction of the calling thread or, if it has one already, joins it and opens another level. The outermost start acquires the resource of the transaction; if that fails, no level is opened.- Parameters:
readOnly- Whether the transaction only reads values. This is set by the outermost start; a nested read-only level joins a writable transaction.acquire- Acquires the resource of the transaction. This is called for the outermost start only.- 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.
-
finish
Finishes the current level of the transaction of the calling thread. Finishing the outermost level commits the resource of the transaction first; if that fails, the level remains unfinished.- Parameters:
commit- Commits the resource of the transaction. This is called for the outermost level only.- Throws:
IllegalStateException- If the calling thread has no transaction or its current level has been finished already, anIllegalStateExceptionwill be thrown.
-
close
Closes the current level of the transaction of the calling thread. Closing the outermost level ends the transaction and releases its resource.- Parameters:
release- Releases the resource of the transaction and rolls the transaction back if its outermost level has not been finished. This is called for the outermost level only.- Throws:
IllegalStateException- If the calling thread has no transaction, anIllegalStateExceptionwill be thrown.
-
checkWrite
public void checkWrite()Checks whether the calling thread may write. This is the case unless it has a read-only transaction or one of which the outermost level has been finished.- Throws:
IllegalStateException- If the calling thread has a read-only transaction or one of which the outermost level has been finished, anIllegalStateExceptionwill be thrown.
-
getResource
Gets the resource of the transaction of the calling thread.- Returns:
- The resource of the transaction of the calling thread or
nullif it has no transaction.
-