Struct spanner_rs::TxRunner
source · [−]pub struct TxRunner { /* private fields */ }
Expand description
Allows running read/write transactions against Cloud Spanner.
Implementations
sourceimpl TxRunner
impl TxRunner
sourcepub async fn run<'b, O, F>(&'b mut self, work: F) -> Result<O, Error>where
F: for<'a> FnMut(&'a mut dyn TransactionContext) -> Pin<Box<dyn Future<Output = Result<O, Error>> + 'a>>,
pub async fn run<'b, O, F>(&'b mut self, work: F) -> Result<O, Error>where
F: for<'a> FnMut(&'a mut dyn TransactionContext) -> Pin<Box<dyn Future<Output = Result<O, Error>> + 'a>>,
Runs abitrary read / write operations against Cloud Spanner.
This function encapsulates the read/write transaction management concerns, allowing the application to minimize boilerplate.
Begin
The underlying transaction is only lazily created. If the provided closure does no work against Cloud Spanner, then no transaction is created.
Commit / Rollback
The underlying transaction will be committed if the provided closure returns Ok
.
Conversely, any Err
returned will initiate a rollback.
If the commit or rollback operation returns an unexpected error, then this function will return that error.
Retries
When committing, Cloud Spanner may reject the transaction due to conflicts with another transaction. In these situations, Cloud Spanner allows retrying the transaction which will have a higher priority and potentially successfully commit.
NOTE: the consequence of retyring is that the provided closure may be invoked multiple times. It is important to avoid doing any additional side effects within this closure as they will also potentially occur more than once.
Example
async fn bump_version(id: u32) -> Result<u32, Error> {
client
.read_write()
.run(|tx| {
Box::pin(async move {
let rs = tx
.execute_query(
"SELECT MAX(version) FROM versions WHERE id = @id",
&[("id", &id)],
)
.await?;
let latest_version: u32 = rs.iter().next().unwrap().get(0)?;
let next_version = latest_version + 1;
tx.execute_update(
"INSERT INTO versions(id, version) VALUES(@id, @next_version)",
&[("id", &id), ("next_version", &next_version)],
)
.await?;
Ok(next_version)
})
})
.await
}
Auto Trait Implementations
impl !RefUnwindSafe for TxRunner
impl Send for TxRunner
impl !Sync for TxRunner
impl Unpin for TxRunner
impl !UnwindSafe for TxRunner
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
sourcefn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request