pub struct TxRunner { /* private fields */ }
Expand description

Allows running read/write transactions against Cloud Spanner.

Implementations

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

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Wrap the input message T in a tonic::Request
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more