#transaction #rollback #hook #diesel #handles #connection #thread

diesel_transaction_handles

shareable transaction handles for diesel, with optional rollback hooks

2 releases

0.1.1 Feb 28, 2019
0.1.0 Feb 28, 2019

#2504 in Database interfaces

Download history 113/week @ 2024-03-09 129/week @ 2024-03-16 62/week @ 2024-03-23 53/week @ 2024-03-30 56/week @ 2024-04-06 3/week @ 2024-04-13 51/week @ 2024-04-20 38/week @ 2024-04-27 67/week @ 2024-05-04 40/week @ 2024-05-11 25/week @ 2024-05-18 5/week @ 2024-05-25 9/week @ 2024-06-01 55/week @ 2024-06-08 7/week @ 2024-06-15

71 downloads per month

MIT license

14KB
242 lines

Diesel Transaction Handles

This crate adds a new type of Connection to diesel that is inherently transactional. It will open a transaction on creation, and will rollback on drop. Committing the transaction will consume self.

A prime use case for this is the ability to have multiple threads perform db operations within the same transaction.

Additionally, with the feature rollback_hooks, you can provide functions to execute in case the transaction rolls back. The main use case of this is if you perform IO that is not to your db, that you need to undo in the case of a rollback, you can provide the operation necessary to rollback to the connection and it will be performed if necessary.

Rollback hooks are performed last in, first out.

Usage

Add this to your Cargo.toml:

[dependencies]
diesel_transaction_handles = "0.1.0"

Example:

#[macro_use]
extern crate failure;

use diesel::prelude::*;
use diesel_transaction_handles::TransactionalConnection;
use std::sync::Arc;
use crate::external;
use crate::schema::things_done;

fn main() {

    let pgcon = diesel::PgConnection::establish("localhost:5432/pgdb").unwrap();
    let txcon = TransactionalConnection::new(pgcon).unwrap();
    let arccon = Arc::new(txcon);

    let arccon_ = arccon.clone();
    let job1 = std::thread::spawn(move || {
        let id = external::do_the_thing().unwrap(3);
        arccon_.add_rollback_hook(|| external::undo_the_thing(id));
        diesel::insert_into(things_done::table).values(things_done::id.eq(id)).execute(&*arccon_)
    });

    let arccon_ = arccon.clone();
    let job2 = std::thread::spawn(move || -> Result<usize, failure::Error> {
        let f = diesel::select(diesel::dsl::sql::<diesel::sql_types::Bool>("FALSE")).load::<bool>(&*arccon_)?;
        bail!("yikes")
    });

    let job1res = job1.join().unwrap();
    let job2res = job2.join().unwrap();
    let res = match (job1res, job2res) {
        (Ok(a), Ok(b)) => Ok((a, b)),
        (Err(e), _) => Err(e),
        (_, Err(e)) => Err(e),
    };
    println!(
        "{:?}",
        Arc::try_unwrap(arccon)
            .map_err(|_| "Arc still held by multiple threads.")
            .unwrap()
            .handle_result(res) // rollback occurs here
    );
}

Dependencies

~3.5MB
~75K SLoC