Class QBackendTransaction

java.lang.Object
com.kingsrook.qqq.backend.core.actions.QBackendTransaction
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
MemoryConsistentReadTransaction, MongoDBTransaction, RDBMSTransaction

public class QBackendTransaction extends Object implements AutoCloseable
Container wherein backend modules can track data and/or objects that are part of a transaction. Most obvious use-case would be a JDBC Connection. See subclass in rdbms module. Ditto MongoDB. Also holds after-commit callbacks: work (e.g., publishing events about changed records) that must only happen once the transaction's data is committed, and never if it is rolled back. Subclasses that override commit() must call runAfterCommitCallbacks() once their commit succeeds, and discardAfterCommitCallbacks() if it fails. Subclasses that override rollback() must call super.rollback(), which discards them. Note: One would imagine that this class shouldn't ever implement Serializable...
  • Constructor Details

    • QBackendTransaction

      public QBackendTransaction()
  • Method Details

    • openFor

      public static QBackendTransaction openFor(AbstractTableActionInput input) throws QException
      Throws:
      QException
    • openConsistentReadFor

      public static QBackendTransaction openConsistentReadFor(ConsistentReadInput input) throws QException
      Open a consistent-read transaction (see QBackendModuleInterface .openConsistentReadTransaction) on the named backend.
      Throws:
      QException - if the backend's module doesn't support them.
    • getExportedSnapshotId

      public Optional<String> getExportedSnapshotId()
      For a transaction from openConsistentReadTransaction, the id of a snapshot that other transactions can attach to (via ConsistentReadInput .importSnapshotId) - for backends that support sharing one (e.g., postgres). Empty otherwise.
    • getConsistencyGuarantee

      public ConsistencyGuarantee getConsistencyGuarantee()
      What a transaction from openConsistentReadTransaction guarantees. Callers should record this, rather than assume ONE_INSTANT.
    • commit

      public void commit() throws QException
      Commit the transaction.
      Throws:
      QException
    • rollback

      public void rollback() throws QException
      Rollback the transaction.
      Throws:
      QException
    • addAfterCommitCallback

      public void addAfterCommitCallback(Runnable callback)
      Register a callback to run after this transaction's next successful commit. Callbacks run in the order they were added, and each runs at most once. If the transaction is rolled back instead, they are discarded without running. A callback that throws (an Exception, or a LinkageError, such as a NoClassDefFoundError from an optional library that isn't on the classpath) is logged, and does not fail the commit - whose data is already committed - nor stop later callbacks from running.
    • runAfterCommitCallbacks

      protected void runAfterCommitCallbacks()
      Run (and then forget) all after-commit callbacks, in the order they were added, catching and logging any Exception or LinkageError from each. For subclasses to call once their commit has succeeded.
    • discardAfterCommitCallbacks

      protected void discardAfterCommitCallbacks()
      Forget all after-commit callbacks, without running them - their work will not be committed. For rollback, and for subclasses to call when their commit fails (so a later commit on this transaction doesn't run them).
    • close

      public void close()
      Close any resources associated with the transaction. In theory, should only be called after a commit or rollback was done.
      Specified by:
      close in interface AutoCloseable