Class Txn


  • public class Txn
    extends java.lang.Object
    Application utilities for executing code in transactions.

    Nested transaction are not supported but calling inside an existing transaction, which must be compatible, (i.e. a write needs a WRITE transaction). causes the existing transaction to be used.

    • Constructor Summary

      Constructors 
      Constructor Description
      Txn()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends Transactional,​X>
      X
      calc​(T txn, TxnType txnType, java.util.function.Supplier<X> r)
      Execute and return a value in a transaction with the given transaction type.
      static <T extends Transactional,​X>
      X
      calculate​(T txn, java.util.function.Supplier<X> r)
      Execute in a "read" transaction that can promote to "write" and return some calculated value.
      static <T extends Transactional,​X>
      X
      calculateRead​(T txn, java.util.function.Supplier<X> r)
      Execute and return a value in a read transaction
      static <T extends Transactional,​X>
      X
      calculateWrite​(T txn, java.util.function.Supplier<X> r)
      Execute and return a value in a write transaction.
      static <T extends Transactional>
      void
      exec​(T txn, TxnType txnType, java.lang.Runnable r)
      Execute application code in a transaction with the given transaction type.
      static <T extends Transactional>
      void
      execute​(T txn, java.lang.Runnable r)
      Execute in a "read" transaction that can promote to "write".
      static <T extends Transactional>
      void
      executeRead​(T txn, java.lang.Runnable r)
      Execute in a read transaction
      static <T extends Transactional>
      void
      executeWrite​(T txn, java.lang.Runnable r)
      Execute the Runnable in a write transaction
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Txn

        public Txn()
    • Method Detail

      • execute

        public static <T extends Transactional> void execute​(T txn,
                                                             java.lang.Runnable r)
        Execute in a "read" transaction that can promote to "write".

        Such a transaction may abort if an update is executed by another thread before this one is promoted to "write" mode. If so, the data protected by txn is unchanged.

        If the application knows updates will be needed, consider using executeWrite(T, java.lang.Runnable) which starts in "write" mode.

        The application code can call Transactional.promote() to attempt to change from "read" to "write"; the promote method returns a boolean indicating whether the promotion was possible or not.

      • calculate

        public static <T extends Transactional,​X> X calculate​(T txn,
                                                                    java.util.function.Supplier<X> r)
        Execute in a "read" transaction that can promote to "write" and return some calculated value.

        Such a transaction may abort if an update is executed by another thread before this one is promoted to "write" mode. If so, the data protected by txn is unchanged.

        If the application knows updates will be needed, consider using executeWrite(T, java.lang.Runnable) which starts in "write" mode.

        The application code can call Transactional.promote() to attempt to change from "read" to "write"; the promote method returns a boolean indicating whether the promotion was possible or not.

      • exec

        public static <T extends Transactional> void exec​(T txn,
                                                          TxnType txnType,
                                                          java.lang.Runnable r)
        Execute application code in a transaction with the given transaction type.
      • calc

        public static <T extends Transactional,​X> X calc​(T txn,
                                                               TxnType txnType,
                                                               java.util.function.Supplier<X> r)
        Execute and return a value in a transaction with the given transaction type.
      • executeRead

        public static <T extends Transactional> void executeRead​(T txn,
                                                                 java.lang.Runnable r)
        Execute in a read transaction
      • calculateRead

        public static <T extends Transactional,​X> X calculateRead​(T txn,
                                                                        java.util.function.Supplier<X> r)
        Execute and return a value in a read transaction
      • executeWrite

        public static <T extends Transactional> void executeWrite​(T txn,
                                                                  java.lang.Runnable r)
        Execute the Runnable in a write transaction
      • calculateWrite

        public static <T extends Transactional,​X> X calculateWrite​(T txn,
                                                                         java.util.function.Supplier<X> r)
        Execute and return a value in a write transaction.