Class StdTxManager

java.lang.Object
tech.ydb.yoj.repository.db.StdTxManager
All Implemented Interfaces:
TxManager, TxManagerState

public final class StdTxManager extends Object implements TxManager, TxManagerState
Standard implementation of transaction manager interface, which logs transaction statements and results, and reports transaction execution metrics: rollback and commit counts; attempt duration, total duration and retry count histograms.

If you need a transaction manager, this is the right choice: just construct an instance of StdTxManager and use it.

If you need to decorate transaction execution logic with e.g. additional logging, tracing, rate limiting etc., extend DelegatingTxManager, and override one or both of DelegatingTxManager.doRunTx(Supplier) and wrapTxBody() methods.

See Also:
  • Field Details

    • useNewTxNameGeneration

      public static volatile boolean useNewTxNameGeneration
  • Constructor Details

    • StdTxManager

      public StdTxManager(Repository repository)
    • StdTxManager

      @Deprecated public StdTxManager(Repository repository, int maxAttemptCount, String name, Integer logLine, String logContext, TxOptions options)
      Deprecated.
      Low-level constructor, kept for compatibility with existing code. Not recommended.
  • Method Details

    • separate

      public 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 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 TxManager noFirstLevelCache()
      Description copied from interface: TxManager
      Turn off first level cache
      Specified by:
      noFirstLevelCache in interface TxManager
    • failOnUnknownSeparateTx

      public 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 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 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
    • withTimeout

      public 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
    • withLogLevel

      public 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
    • readOnly

      public 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 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
    • tx

      public 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 <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
    • getState

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

      public boolean isFirstLevelCache()
      Specified by:
      isFirstLevelCache in interface TxManagerState
    • getIsolationLevel

      @Nullable public IsolationLevel getIsolationLevel()
      Specified by:
      getIsolationLevel in interface TxManagerState
    • isReadOnly

      public boolean isReadOnly()
      Specified by:
      isReadOnly in interface TxManagerState
    • isScan

      public boolean isScan()
      Specified by:
      isScan in interface TxManagerState
    • toString

      public String toString()
      Overrides:
      toString in class Object