Class DelegatingTxManager

java.lang.Object
tech.ydb.yoj.repository.db.DelegatingTxManager
All Implemented Interfaces:
TxManager

public abstract class DelegatingTxManager extends Object implements TxManager
Abstract base class for decorating TxManagers:
  • Enable additional tracing and logging, collect more metrics etc.: wrapTxBody()
  • Transform transaction results and accept/reject transactions (e.g. rate limiting): doRunTx()
See Also:
  • Field Details

    • delegate

      protected final TxManager delegate
  • Constructor Details

    • DelegatingTxManager

      protected DelegatingTxManager(@NonNull @NonNull TxManager delegate)
  • Method Details

    • doRunTx

      protected <T> T doRunTx(Supplier<T> supplier)
      Starts running the specified transaction manager logic in the desired environment (e.g. starts running the logic only if rate limit is not exceeded), postprocesses the run results and returns them to the user.

      Default implementation just returns the result of supplier.get().

      Type Parameters:
      T - transaction result type
      Parameters:
      supplier - some logic that calls the delegate transaction manager, e.g., a call to delegate.tx(...)).
      Returns:
      results returned by supplier, possibly post-processed by this doRun() method
    • wrapTxBody

      protected <T> Supplier<T> wrapTxBody(Supplier<T> supplier)
      Wraps the transaction body logic, e.g. establishes a tracing context for the specific transaction, runs the transaction body and closes the tracing context.

      Default implementation just returns the original supplier.

      Type Parameters:
      T - transaction result type
      Parameters:
      supplier - logic that is run inside the transaction
      Returns:
      transaction body logic to execute
    • createTxManager

      protected abstract TxManager createTxManager(TxManager delegate)
      Creates an instance of this class that wraps the specified TxManager.
      Parameters:
      delegate - transaction manager to delegate to
      Returns:
      wrapped delegate
    • tx

      public final void tx(Runnable runnable)
      Description copied from interface: TxManager
      Performs the specified action inside a transaction. The action must be idempotent, because it might be executed multiple times in case of transaction lock invalidation.
      Specified by:
      tx in interface TxManager
      Parameters:
      runnable - action to perform
    • tx

      public final <T> T tx(Supplier<T> supplier)
      Description copied from interface: TxManager
      Performs the specified action inside a transaction. The action must be idempotent, because it might be executed multiple times in case of transaction lock invalidation.
      Specified by:
      tx in interface TxManager
      Parameters:
      supplier - action to perform
      Returns:
      action result
    • withName

      public final TxManager withName(String name, String logContext)
      Description copied from interface: TxManager
      Short cut for:
      withName(name).withLogContext(logContext)
      Specified by:
      withName in interface TxManager
    • withName

      public final TxManager withName(String name)
      Description copied from interface: TxManager
      Set transaction name used for logging and metrics.

      Since name is used as metric label, there should be limited numbers of names used for transactions. Normally one per code occurrence.

      Specified by:
      withName in interface TxManager
      Parameters:
      name - used for logging and metrics, not null
    • withLogContext

      public final TxManager withLogContext(String logContext)
      Specified by:
      withLogContext in interface TxManager
      Parameters:
      logContext - dynamic context used for logging.
    • separate

      public final TxManager separate()
      Description copied from interface: TxManager
      Mark transaction as explicitly separate from current transaction. Otherwise, you will be failed or WARNed about initiating a new transaction while already being in a transaction
      Specified by:
      separate in interface TxManager
    • delayedWrites

      public TxManager delayedWrites()
      Description copied from interface: TxManager
      Enable pending write queue in transaction and execute write changes right before the transaction is committed.
      Specified by:
      delayedWrites in interface TxManager
    • immediateWrites

      public final TxManager immediateWrites()
      Description copied from interface: TxManager
      Disable pending write queue in transaction and execute write changes immediately.
      Specified by:
      immediateWrites in interface TxManager
    • noFirstLevelCache

      public final TxManager noFirstLevelCache()
      Description copied from interface: TxManager
      Turn off first level cache
      Specified by:
      noFirstLevelCache in interface TxManager
    • failOnUnknownSeparateTx

      public final TxManager failOnUnknownSeparateTx()
      Description copied from interface: TxManager
      Fails if you try to create a separate transaction inside other transaction. TxManager with this setting is good to use in tests. Call separate() before start transaction if you really need to create a one transaction inside other transaction.
      Specified by:
      failOnUnknownSeparateTx in interface TxManager
    • withMaxRetries

      public final TxManager withMaxRetries(int maxRetries)
      Description copied from interface: TxManager
      Sets maximum number of retries for each tx() call:
      Specified by:
      withMaxRetries in interface TxManager
      Parameters:
      maxRetries - maximum number of retries (>= 0)
    • withDryRun

      public final TxManager withDryRun(boolean dryRun)
      Description copied from interface: TxManager
      Marks the transaction as dry-run. If transaction is marked as dry-run, its changes will be rolled back but no exception will be thrown and transaction result will be returned.
      Specified by:
      withDryRun in interface TxManager
    • withLogLevel

      public final TxManager withLogLevel(TransactionLog.Level level)
      Description copied from interface: TxManager
      Changes logging verbosity.
      Specified by:
      withLogLevel in interface TxManager
      Parameters:
      level - minimum accepted log message level, e.g., DEBUG for DB queries
    • withLogStatementOnSuccess

      public TxManager withLogStatementOnSuccess(boolean logStatementOnSuccess)
      Description copied from interface: TxManager
      Flag for managing logging transaction statement on success.
      Specified by:
      withLogStatementOnSuccess in interface TxManager
    • withTimeout

      public final TxManager withTimeout(Duration timeout)
      Description copied from interface: TxManager
      Changes transaction timeout. If the timeout elapses before transaction finishes, DeadlineExceededException or QueryCancelledException might be thrown.
      Specified by:
      withTimeout in interface TxManager
      Parameters:
      timeout - transaction timeout
    • withVerboseLogging

      public final TxManager withVerboseLogging()
      Description copied from interface: TxManager
      Configures verbose logging for this transactions's execution. Short representations of DB queries performed and partial results of these queries will be logged.
      Specified by:
      withVerboseLogging in interface TxManager
      See Also:
    • withBriefLogging

      public final TxManager withBriefLogging()
      Description copied from interface: TxManager
      Configures brief logging for this transactions's execution. Only total time spent in DB session and commit/ rollback timings will be logged.
      Specified by:
      withBriefLogging in interface TxManager
      See Also:
    • noLogging

      public final TxManager noLogging()
      Description copied from interface: TxManager
      Disables logging of this transaction's execution. You will still see transaction result messages in the logs, e.g., "runInTx(): Commit/Rollback/...".
      Specified by:
      noLogging in interface TxManager
    • readOnly

      public final TxManager.ReadonlyBuilder readOnly()
      Description copied from interface: TxManager
      Start a transaction-like session of read-only statements. Each statement will be executed separately, with the specified isolation level (online consistent read-only, by default).

      YDB doesn't currently support multi-statement read-only transactions. If you perform more than one read, be ready to handle potential inconsistencies between the reads.

      You can also use readOnly().run(() -> [table].readTable(...)); to efficiently read data from the table without interfering with OLTP transactions. In this case, data consistency is similar to snapshot isolation. If perform more than one readTable(), be ready to handle potential inconsistencies between the reads.

      Specified by:
      readOnly in interface TxManager
    • scan

      public final TxManager.ScanBuilder scan()
      Description copied from interface: TxManager
      Start a transaction-like session of scan queries. Each query will be executed separately. Scan query consistency is similar to snapshot isolation, and these queries efficiently read data from the snapshot without interfering with OLTP transactions. Be ready to handle potential inconsistencies between the reads if you perform more than one scan query.
      Specified by:
      scan in interface TxManager
    • getState

      public final TxManagerState getState()
      Specified by:
      getState in interface TxManager
      Returns:
      information about current transaction settings set for this instance of TxManager
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • clone

      protected final Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException