pub struct Transaction { /* private fields */ }Expand description
A PostgreSQL transaction for the asynchronous connection.
This struct provides transaction control. The connection is passed
to commit and rollback methods to execute the transaction commands.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub async fn commit(self, conn: &mut Conn) -> Result<()>
pub async fn commit(self, conn: &mut Conn) -> Result<()>
Commit the transaction.
This consumes the transaction and sends a COMMIT statement to the server. The connection must be passed as an argument to execute the commit.
§Errors
Returns Error::InvalidUsage if the connection is not the same
as the one that started the transaction.
Sourcepub async fn rollback(self, conn: &mut Conn) -> Result<()>
pub async fn rollback(self, conn: &mut Conn) -> Result<()>
Rollback the transaction.
This consumes the transaction and sends a ROLLBACK statement to the server. The connection must be passed as an argument to execute the rollback.
§Errors
Returns Error::InvalidUsage if the connection is not the same
as the one that started the transaction.
Sourcepub async fn exec_portal_named<S: IntoStatement, P: ToParams>(
&self,
conn: &mut Conn,
statement: S,
params: P,
) -> Result<NamedPortal<'_>>
pub async fn exec_portal_named<S: IntoStatement, P: ToParams>( &self, conn: &mut Conn, statement: S, params: P, ) -> Result<NamedPortal<'_>>
Create a named portal for iterative row fetching within this transaction.
Named portals are safe to use within an explicit transaction because SYNC messages do not destroy them (only COMMIT/ROLLBACK does).
The statement can be either:
- A
&PreparedStatementreturned fromconn.prepare() - A raw SQL
&strfor one-shot execution
§Example
conn.transaction(|conn, tx| async move {
let mut portal = tx.exec_portal_named(conn, &stmt, ()).await?;
while !portal.is_complete() {
let rows: Vec<(i32,)> = portal.execute_collect(conn, 100).await?;
process(rows);
}
portal.close(conn).await?;
tx.commit(conn).await
}).await?;§Errors
Returns Error::InvalidUsage if the connection is not the same
as the one that started the transaction.