pub struct MultiUseReadOnlyTransactionBuilder { /* private fields */ }Expand description
A builder for MultiUseReadOnlyTransaction.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let read_only_tx = db_client.read_only_transaction()
.set_timestamp_bound(TimestampBound::strong())
.build()
.await?;Implementations§
Source§impl MultiUseReadOnlyTransactionBuilder
impl MultiUseReadOnlyTransactionBuilder
Sourcepub fn set_begin_transaction_option(
self,
option: BeginTransactionOption,
) -> Self
pub fn set_begin_transaction_option( self, option: BeginTransactionOption, ) -> Self
Sets the option for how to start a transaction.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.read_only_transaction().set_begin_transaction_option(BeginTransactionOption::ExplicitBegin).build().await?;
let statement = Statement::builder("SELECT * FROM users").build();
let result_set = transaction.execute_query(statement).await?;By default, the Spanner client will inline the BeginTransaction call with the first query
in the transaction (BeginTransactionOption::InlineBegin). This reduces the number of round-trips
to Spanner that are needed for a transaction. Setting this option to ExplicitBegin can be
beneficial for specific transaction shapes:
- When the transaction executes multiple parallel queries at the start of the transaction.
Only one query can include a
BeginTransactionoption, and all other queries must wait for the first query to return the first result before they can proceed to execute. An explicitBeginTransactionRPC will quickly return a transaction ID and allow all queries to start execution in parallel once the transaction ID has been returned. - When the first query in the transaction could fail, and the application wants to catch the error
and continue executing subsequent queries in the same read-only transaction. With
InlineBegin, if the initial statement fails, the transaction is marked as permanently failed and will not start; all subsequent queries on that transaction will also fail without starting a transaction that excludes the first statement. UsingExplicitBeginestablishes the snapshot transaction upfront before any statements are executed, allowing subsequent statements to continue executing at the same snapshot read timestamp even if an earlier statement fails.
§Example: Retrying an Initial Query with ExplicitBegin
Applications that want the latency benefit of InlineBegin for the
initial query, but want to fall back to ExplicitBegin if the first
statement fails, can implement a retry loop:
// Note: For read-only transactions that execute queries in parallel at the start,
// the recommendation is to use ExplicitBegin directly, rather than a retry loop.
let mut last_error = None;
for option in [
BeginTransactionOption::InlineBegin,
BeginTransactionOption::ExplicitBegin,
] {
let transaction = database_client
.read_only_transaction()
.set_begin_transaction_option(option)
.build()
.await?;
match transaction.execute_query(statement.clone()).await {
Ok(result_set) => return Ok((transaction, result_set)),
Err(error) => last_error = Some(error),
}
}
Err(last_error.expect("at least one attempt failed"))Default is BeginTransactionOption::InlineBegin.
Sourcepub fn with_begin_attempt_timeout(self, timeout: Duration) -> Self
pub fn with_begin_attempt_timeout(self, timeout: Duration) -> Self
Sets the per-attempt timeout for the BeginTransaction RPC.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.read_only_transaction()
.with_begin_attempt_timeout(Duration::from_secs(10))
.build()
.await?;Note: This timeout is only used if the transaction uses the ExplicitBegin transaction option.
Sourcepub fn with_begin_retry_policy(self, policy: impl Into<RetryPolicyArg>) -> Self
pub fn with_begin_retry_policy(self, policy: impl Into<RetryPolicyArg>) -> Self
Sets the retry policy for the BeginTransaction RPC.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.read_only_transaction()
.with_begin_retry_policy(NeverRetry)
.build()
.await?;Note: This policy is only used if the transaction uses the ExplicitBegin transaction option.
Sourcepub fn with_begin_backoff_policy(
self,
policy: impl Into<BackoffPolicyArg>,
) -> Self
pub fn with_begin_backoff_policy( self, policy: impl Into<BackoffPolicyArg>, ) -> Self
Sets the backoff policy for the BeginTransaction RPC.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.read_only_transaction()
.with_begin_backoff_policy(ExponentialBackoff::default())
.build()
.await?;Note: This policy is only used if the transaction uses the ExplicitBegin transaction option.
Sourcepub fn set_timestamp_bound(self, bound: TimestampBound) -> Self
pub fn set_timestamp_bound(self, bound: TimestampBound) -> Self
Sets the timestamp bound for the read-only transaction.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let builder = db_client.read_only_transaction().set_timestamp_bound(TimestampBound::strong());Sourcepub async fn build(self) -> Result<MultiUseReadOnlyTransaction>
pub async fn build(self) -> Result<MultiUseReadOnlyTransaction>
Builds the MultiUseReadOnlyTransaction and starts the transaction
by calling the BeginTransaction RPC.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let tx = db_client.read_only_transaction().build().await?;Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MultiUseReadOnlyTransactionBuilder
impl !RefUnwindSafe for MultiUseReadOnlyTransactionBuilder
impl !UnwindSafe for MultiUseReadOnlyTransactionBuilder
impl Forget for MultiUseReadOnlyTransactionBuilder
impl Move for MultiUseReadOnlyTransactionBuilder
impl Send for MultiUseReadOnlyTransactionBuilder
impl Sync for MultiUseReadOnlyTransactionBuilder
impl Unpin for MultiUseReadOnlyTransactionBuilder
impl UnsafeUnpin for MultiUseReadOnlyTransactionBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request