Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction { /* private fields */ }
Expand description

数据库事务

Implementations§

Source§

impl Transaction

Source

pub fn new(conn: Arc<dyn DatabaseConnection>) -> Self

Source

pub async fn commit(&mut self) -> Result<(), DbError>

Examples found in repository?
examples/postgresql_example.rs (line 214)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191    println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192    let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194    // Commit
195    let mut tx = db.begin_transaction().await?;
196    tx.execute(
197        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198        &[
199            SimpleUuid::new_v4().to_string().into(),
200            "Dave".into(),
201            40.into(),
202        ],
203    )
204    .await?;
205    tx.execute(
206        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207        &[
208            SimpleUuid::new_v4().to_string().into(),
209            "Frank".into(),
210            35.into(),
211        ],
212    )
213    .await?;
214    tx.commit().await?;
215    println!("  ✅ Transaction committed (2 users)");
216
217    // Rollback
218    let mut tx = db.begin_transaction().await?;
219    tx.execute(
220        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221        &[
222            SimpleUuid::new_v4().to_string().into(),
223            "Eve".into(),
224            45.into(),
225        ],
226    )
227    .await?;
228    tx.rollback().await?;
229    println!("  ✅ Transaction rolled back (Eve not saved)");
230
231    let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232    println!("  ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234    db.close().await?;
235    Ok(())
236}
Source

pub async fn rollback(&mut self) -> Result<(), DbError>

Examples found in repository?
examples/postgresql_example.rs (line 228)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191    println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192    let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194    // Commit
195    let mut tx = db.begin_transaction().await?;
196    tx.execute(
197        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198        &[
199            SimpleUuid::new_v4().to_string().into(),
200            "Dave".into(),
201            40.into(),
202        ],
203    )
204    .await?;
205    tx.execute(
206        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207        &[
208            SimpleUuid::new_v4().to_string().into(),
209            "Frank".into(),
210            35.into(),
211        ],
212    )
213    .await?;
214    tx.commit().await?;
215    println!("  ✅ Transaction committed (2 users)");
216
217    // Rollback
218    let mut tx = db.begin_transaction().await?;
219    tx.execute(
220        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221        &[
222            SimpleUuid::new_v4().to_string().into(),
223            "Eve".into(),
224            45.into(),
225        ],
226    )
227    .await?;
228    tx.rollback().await?;
229    println!("  ✅ Transaction rolled back (Eve not saved)");
230
231    let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232    println!("  ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234    db.close().await?;
235    Ok(())
236}
Source

pub async fn execute( &self, sql: &str, params: &[SqlValue], ) -> Result<u64, DbError>

Examples found in repository?
examples/postgresql_example.rs (lines 196-203)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191    println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192    let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194    // Commit
195    let mut tx = db.begin_transaction().await?;
196    tx.execute(
197        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198        &[
199            SimpleUuid::new_v4().to_string().into(),
200            "Dave".into(),
201            40.into(),
202        ],
203    )
204    .await?;
205    tx.execute(
206        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207        &[
208            SimpleUuid::new_v4().to_string().into(),
209            "Frank".into(),
210            35.into(),
211        ],
212    )
213    .await?;
214    tx.commit().await?;
215    println!("  ✅ Transaction committed (2 users)");
216
217    // Rollback
218    let mut tx = db.begin_transaction().await?;
219    tx.execute(
220        "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221        &[
222            SimpleUuid::new_v4().to_string().into(),
223            "Eve".into(),
224            45.into(),
225        ],
226    )
227    .await?;
228    tx.rollback().await?;
229    println!("  ✅ Transaction rolled back (Eve not saved)");
230
231    let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232    println!("  ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234    db.close().await?;
235    Ok(())
236}
Source

pub async fn execute_query( &self, sql: &str, params: &[SqlValue], ) -> Result<QueryResult, DbError>

Trait Implementations§

Source§

impl Drop for Transaction

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V