pub struct MultiUseReadOnlyTransaction { /* private fields */ }Expand description
A multi-use read-only transaction. This transaction can be used for multiple read queries ensuring consistency across all queries.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let tx = db_client.read_only_transaction().build().await?;
let stmt1 = Statement::builder("SELECT * FROM users WHERE id = @id")
.add_param("id", &42)
.build();
let mut rs1 = tx.execute_query(stmt1).await?;
let stmt2 = Statement::builder("SELECT * FROM other_table")
.build();
let mut rs2 = tx.execute_query(stmt2).await?;Implementations§
Source§impl MultiUseReadOnlyTransaction
impl MultiUseReadOnlyTransaction
Sourcepub fn read_timestamp(&self) -> Option<Timestamp>
pub fn read_timestamp(&self) -> Option<Timestamp>
Returns the read timestamp chosen for the transaction.
Sourcepub async fn execute_query(
&self,
statement: impl Into<Statement>,
) -> Result<ResultSet>
pub async fn execute_query( &self, statement: impl Into<Statement>, ) -> Result<ResultSet>
Executes a query using this transaction.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let tx = db_client.read_only_transaction().build().await?;
let stmt = Statement::builder("SELECT * FROM users WHERE id = @id")
.add_param("id", &42)
.build();
let mut rs = tx.execute_query(stmt).await?;
while let Some(row) = rs.next().await {
let _row = row?;
// process row
}§Note
If this transaction was configured with BeginTransactionOption::InlineBegin (the default)
and this is the first statement, an execution failure will prevent the transaction from starting and
mark it as permanently failed. All subsequent statements on this transaction will also fail. To allow a
read-only transaction to continue executing subsequent statements after an initial statement failure,
configure BeginTransactionOption::ExplicitBegin when building the transaction.
Sourcepub async fn execute_read(
&self,
read: impl Into<ReadRequest>,
) -> Result<ResultSet>
pub async fn execute_read( &self, read: impl Into<ReadRequest>, ) -> Result<ResultSet>
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to execute_query.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.read_only_transaction().build().await?;
// Read using the primary key
let read_by_pk = ReadRequest::builder("Users", vec!["Id", "Name"]).with_keys(KeySet::all()).build();
let mut result_set = transaction.execute_read(read_by_pk).await?;
while let Some(row) = result_set.next().await {
let _row = row?;
// process row
}
// Read using a secondary index
let read_by_index = ReadRequest::builder("Users", vec!["Id", "Name"])
.with_index("UsersByIndex", key![1_i64]).build();
let mut result_set = transaction.execute_read(read_by_index).await?;
while let Some(row) = result_set.next().await {
let _row = row?;
// process row
}§Note
If this transaction was configured with BeginTransactionOption::InlineBegin (the default)
and this is the first statement, an execution failure will prevent the transaction from starting and
mark it as permanently failed. All subsequent statements on this transaction will also fail. To allow a
read-only transaction to continue executing subsequent statements after an initial statement failure,
configure BeginTransactionOption::ExplicitBegin when building the transaction.
Trait Implementations§
Source§impl Clone for MultiUseReadOnlyTransaction
impl Clone for MultiUseReadOnlyTransaction
Auto Trait Implementations§
impl !Freeze for MultiUseReadOnlyTransaction
impl !RefUnwindSafe for MultiUseReadOnlyTransaction
impl !UnwindSafe for MultiUseReadOnlyTransaction
impl Forget for MultiUseReadOnlyTransaction
impl Move for MultiUseReadOnlyTransaction
impl Send for MultiUseReadOnlyTransaction
impl Sync for MultiUseReadOnlyTransaction
impl Unpin for MultiUseReadOnlyTransaction
impl UnsafeUnpin for MultiUseReadOnlyTransaction
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