Struct diesel::pg::TransactionBuilder[][src]

pub struct TransactionBuilder<'a, C> { /* fields omitted */ }
Expand description

Used to build a transaction, specifying additional details.

This struct is returned by .build_transaction. See the documentation for methods on this struct for usage examples. See the PostgreSQL documentation for SET TRANSACTION for details on the behavior of each option.

Implementations

Makes the transaction READ ONLY

Example

conn.build_transaction()
    .read_only()
    .run::<_, diesel::result::Error, _>(|conn| {
        let read_attempt = users.select(name).load::<String>(conn);
        assert!(read_attempt.is_ok());

        let write_attempt = diesel::insert_into(users)
            .values(name.eq("Ruby"))
            .execute(conn);
        assert!(write_attempt.is_err());

        Ok(())
    })?;

Makes the transaction READ WRITE

This is the default, unless you’ve changed the default_transaction_read_only configuration parameter.

Example

conn.build_transaction()
    .read_write()
    .run(|conn| {
        let read_attempt = users.select(name).load::<String>(conn);
        assert!(read_attempt.is_ok());

        let write_attempt = diesel::insert_into(users)
            .values(name.eq("Ruby"))
            .execute(conn);
        assert!(write_attempt.is_ok());

        Ok(())
    })

Makes the transaction DEFERRABLE

Example

conn.build_transaction()
    .deferrable()
    .run(|conn| Ok(()))

Makes the transaction NOT DEFERRABLE

This is the default, unless you’ve changed the default_transaction_deferrable configuration parameter.

Example

conn.build_transaction()
    .not_deferrable()
    .run(|conn| Ok(()))

Makes the transaction ISOLATION LEVEL READ COMMITTED

This is the default, unless you’ve changed the default_transaction_isolation_level configuration parameter.

Example

conn.build_transaction()
    .read_committed()
    .run(|conn| Ok(()))

Makes the transaction ISOLATION LEVEL REPEATABLE READ

Example

conn.build_transaction()
    .repeatable_read()
    .run(|conn| Ok(()))

Makes the transaction ISOLATION LEVEL SERIALIZABLE

Example

conn.build_transaction()
    .serializable()
    .run(|conn| Ok(()))

Runs the given function inside of the transaction with the parameters given to this builder.

Returns an error if the connection is already inside a transaction, or if the transaction fails to commit or rollback

If the transaction fails to commit due to a SerializationFailure or a ReadOnlyTransaction a rollback will be attempted. If the rollback succeeds, the original error will be returned, otherwise the error generated by the rollback will be returned. In the second case the connection should be considered broken as it contains a uncommitted unabortable open transaction.

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

Performs the conversion.

Performs the conversion.

Convert self to an expression for Diesel’s query builder. Read more

Convert &self to an expression for Diesel’s query builder. Read more

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.