Class ThreadTransactions<R>

java.lang.Object
de.aristaflow.adept2.util.threading.ThreadTransactions<R>
Type Parameters:
R - The type of the resource of a transaction.

public final class ThreadTransactions<R> extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Releases the resource of a transaction when its outermost level is closed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance without any transaction.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Checks whether the calling thread may write.
    void
    Closes the current level of the transaction of the calling thread.
    void
    finish(Consumer<? super R> commit)
    Finishes the current level of the transaction of the calling thread.
    Gets the resource of the transaction of the calling thread.
    void
    start(boolean readOnly, Supplier<? extends R> acquire)
    Starts a transaction of the calling thread or, if it has one already, joins it and opens another level.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ThreadTransactions

      public ThreadTransactions()
      Creates a new instance without any transaction.
  • Method Details

    • start

      public void start(boolean readOnly, Supplier<? extends R> acquire)
      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, an IllegalStateException will be thrown.
    • finish

      public void finish(Consumer<? super R> commit)
      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, an IllegalStateException will be thrown.
    • close

      public void close(ThreadTransactions.Release<? super R> release)
      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, an IllegalStateException will 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, an IllegalStateException will be thrown.
    • getResource

      public R getResource()
      Gets the resource of the transaction of the calling thread.
      Returns:
      The resource of the transaction of the calling thread or null if it has no transaction.