Class TransactionTemplate

java.lang.Object
com.cockroachlabs.crdb.tx.TransactionTemplate

public final class TransactionTemplate extends Object
Runs ConnectionCallbacks inside a managed JDBC transaction.

The template opens a connection, sets the requested Isolation, applies follower-read staleness from CrdbContext (via SET TRANSACTION AS OF SYSTEM TIME), applies Cockroach vCluster tagging from CrdbContext — SET LOCAL application_name for the vCluster name and SET LOCAL default_transaction_quality_of_service for the workload QoS (HIGH→critical, NORMAL→regular, LOW→background) — runs the callback, and commits. SET LOCAL scopes the tags to this transaction so pooled connections are not polluted for the next borrower.

On a serialization conflict it rolls back and — when retry is enabled — replays the whole transaction through RetryEngine, realizing CockroachDB's cockroach_restart retry semantics as a full-transaction restart.

vCluster scope (v1): this is workload tagging and QoS assignment. Routing a workload to a different vCluster endpoint is platform-dependent and is not performed here.

  • Constructor Details

  • Method Details

    • execute

      public <T> T execute(Isolation iso, boolean retry, ConnectionCallback<T> work)
      Execute work in a transaction.
      Parameters:
      iso - the isolation level to set
      retry - whether to retry on serialization conflicts
      work - the work to run against the transactional connection