1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#[macro_use]
extern crate failure;

use diesel::connection::Connection;
use diesel::connection::SimpleConnection;
use diesel::connection::TransactionManager;
use diesel::result::Error as DieselError;
use failure::Error as FailureError;
use std::sync::Mutex;

#[cfg(all(feature = "log_errors_on_drop", feature = "panic_errors_on_drop"))]
compile_error!(
    "Features: \"log_errors_on_drop\" and \"panic_errors_on_drop\" are mutually exclusive!"
);

#[cfg(all(nightly, feature = "rollback_hooks"))]
pub type RollbackHook = Box<dyn FnOnce() -> Result<(), failure::Error> + Send>;
#[cfg(all(not(nightly), feature = "rollback_hooks"))]
pub type RollbackHook = Box<fn() -> Result<(), failure::Error>>;

#[derive(Debug, Fail)]
pub enum Error {
    #[fail(display = "Diesel Error: {}", _0)]
    Diesel(DieselError),
    #[fail(display = "Custom Error: {}", _0)]
    Failure(FailureError),
}
impl From<DieselError> for Error {
    fn from(err: DieselError) -> Self {
        Error::Diesel(err)
    }
}
impl From<FailureError> for Error {
    fn from(err: FailureError) -> Self {
        Error::Failure(err)
    }
}

#[derive(Debug, Fail)]
#[fail(display = "The following errors ocurred: {:?}.", _0)]
pub struct ErrorVec(pub Vec<Error>);
impl From<Vec<Error>> for ErrorVec {
    fn from(err_vec: Vec<Error>) -> Self {
        ErrorVec(err_vec)
    }
}

#[cfg(feature = "rollback_hooks")]
pub struct TransactionalConnection<T: Connection>(Mutex<T>, Mutex<Vec<RollbackHook>>);
#[cfg(not(feature = "rollback_hooks"))]
pub struct TransactionalConnection<T: Connection>(Mutex<T>);
impl<C: Connection> TransactionalConnection<C> {
    pub fn new(conn: C) -> Result<TransactionalConnection<C>, Error> {
        let man = conn.transaction_manager();

        man.begin_transaction(&conn)?;

        #[cfg(feature = "rollback_hooks")]
        let res = Ok(TransactionalConnection(
            Mutex::new(conn),
            Mutex::new(vec![]),
        ));
        #[cfg(not(feature = "rollback_hooks"))]
        let res = Ok(TransactionalConnection(Mutex::new(conn)));

        res
    }

    pub fn rollback(self) -> Result<(), ErrorVec> {
        let mut errs = vec![];
        #[cfg(feature = "rollback_hooks")]
        {
            let mut guard = self.1.lock().unwrap();
            while !guard.is_empty() {
                let hook = guard.pop().unwrap();
                match hook() {
                    Err(e) => errs.push(Error::from(e)),
                    _ => (),
                }
            }
        }

        let guard = self.0.lock().unwrap();
        let man = guard.transaction_manager();

        while TransactionManager::<C>::get_transaction_depth(man) > 0 {
            match man.rollback_transaction(&*guard) {
                Err(e) => {
                    errs.push(Error::from(e));
                    break;
                }
                _ => (),
            }
        }

        if !errs.is_empty() {
            Err(errs)?;
        }

        Ok(())
    }

    pub fn commit(self) -> Result<(), Error> {
        let guard = self.0.lock().unwrap();
        let man = guard.transaction_manager();

        while TransactionManager::<C>::get_transaction_depth(man) > 0 {
            man.commit_transaction(&*guard)?;
        }

        Ok(())
    }

    pub fn handle_result<T, E>(self, res: Result<T, E>) -> Result<T, failure::Error>
    where
        failure::Error: From<E>,
    {
        match &res {
            Ok(_) => self.commit()?,
            Err(_) => self.rollback()?,
        };
        Ok(res?)
    }

    #[cfg(all(nightly, feature = "rollback_hooks"))]
    pub fn add_rollback_hook(&self, hook: impl FnOnce() -> Result<(), failure::Error> + Send) {
        self.1.lock().unwrap().push(Box::new(hook));
    }

    #[cfg(all(not(nightly), feature = "rollback_hooks"))]
    pub fn add_rollback_hook(&self, hook: fn() -> Result<(), failure::Error>) {
        self.1.lock().unwrap().push(Box::new(hook));
    }
}

impl<C: Connection> std::ops::Drop for TransactionalConnection<C> {
    fn drop(&mut self) {
        #[cfg(feature = "rollback_hooks")]
        {
            let guard = self.1.lock().unwrap();
            for hook in &*guard {
                let _res = hook();

                #[cfg(feature = "log_errors_on_drop")]
                _res.unwrap_or_else(|e| {
                    eprintln!(
                        "WARNING: Error ocurred while attempting transaction rollback: {}",
                        e
                    );
                });

                #[cfg(feature = "panic_errors_on_drop")]
                _res.unwrap_or_else(|e| panic!("{}", e));
            }
        }

        let guard = self.0.lock().unwrap();
        let man = guard.transaction_manager();

        while TransactionManager::<C>::get_transaction_depth(man) > 0 {
            let _res = man.rollback_transaction(&*guard);

            #[cfg(feature = "log_errors_on_drop")]
            _res.unwrap_or_else(|e| {
                eprintln!(
                    "WARNING: Error ocurred while attempting transaction rollback: {}",
                    e
                );
            });

            #[cfg(feature = "panic_errors_on_drop")]
            _res.unwrap_or_else(|e| panic!("{}", e));
        }
    }
}

impl<C: Connection> SimpleConnection for TransactionalConnection<C> {
    fn batch_execute(&self, query: &str) -> diesel::QueryResult<()> {
        self.0.lock().unwrap().batch_execute(query)
    }
}

impl<C: Connection> Connection for TransactionalConnection<C> {
    type Backend = C::Backend;
    type TransactionManager = Self;

    fn establish(_: &str) -> diesel::ConnectionResult<Self> {
        Err(diesel::ConnectionError::BadConnection(String::from(
            "Cannot directly establish a pooled connection",
        )))
    }

    fn execute(&self, query: &str) -> diesel::QueryResult<usize> {
        self.0.lock().unwrap().execute(query)
    }

    fn query_by_index<T, U>(&self, source: T) -> diesel::QueryResult<Vec<U>>
    where
        T: diesel::query_builder::AsQuery,
        T::Query:
            diesel::query_builder::QueryFragment<Self::Backend> + diesel::query_builder::QueryId,
        Self::Backend: diesel::sql_types::HasSqlType<T::SqlType>,
        U: diesel::deserialize::Queryable<T::SqlType, Self::Backend>,
    {
        self.0.lock().unwrap().query_by_index(source)
    }

    fn query_by_name<T, U>(&self, source: &T) -> diesel::QueryResult<Vec<U>>
    where
        T: diesel::query_builder::QueryFragment<Self::Backend> + diesel::query_builder::QueryId,
        U: diesel::deserialize::QueryableByName<Self::Backend>,
    {
        self.0.lock().unwrap().query_by_name(source)
    }

    fn execute_returning_count<T>(&self, source: &T) -> diesel::QueryResult<usize>
    where
        T: diesel::query_builder::QueryFragment<Self::Backend> + diesel::query_builder::QueryId,
    {
        self.0.lock().unwrap().execute_returning_count(source)
    }

    fn transaction_manager(&self) -> &Self::TransactionManager {
        &self
    }
}

impl<C: Connection> TransactionManager<TransactionalConnection<C>> for TransactionalConnection<C> {
    fn begin_transaction(&self, conn: &TransactionalConnection<C>) -> diesel::QueryResult<()> {
        let conn = conn.0.lock().unwrap();
        conn.transaction_manager().begin_transaction(&*conn)
    }

    fn rollback_transaction(&self, conn: &TransactionalConnection<C>) -> diesel::QueryResult<()> {
        let conn = conn.0.lock().unwrap();
        conn.transaction_manager().rollback_transaction(&*conn)
    }

    fn commit_transaction(&self, conn: &TransactionalConnection<C>) -> diesel::QueryResult<()> {
        let conn = conn.0.lock().unwrap();
        conn.transaction_manager().commit_transaction(&*conn)
    }

    fn get_transaction_depth(&self) -> u32 {
        diesel::connection::TransactionManager::<C>::get_transaction_depth(
            &*self.0.lock().unwrap().transaction_manager(),
        )
    }
}

#[test]
fn multiple_threads() {
    use diesel::prelude::*;
    use std::sync::Arc;

    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 || {
        println!("SELECTing TRUE");
        arccon_.add_rollback_hook(|| Ok(println!("Nevermind, rolling back.")));
        diesel::select(diesel::dsl::sql::<diesel::sql_types::Bool>("TRUE")).load::<bool>(&*arccon_)
    });

    let arccon_ = arccon.clone();
    let job2 = std::thread::spawn(move || {
        diesel::select(diesel::dsl::sql::<diesel::sql_types::Bool>("FALSE")).load::<bool>(&*arccon_)
    });

    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)
    );
}