Skip to content

Commit 6b44a5f

Browse files
committed
database: better error handling to avoid getting stuck
1 parent ae0a0ee commit 6b44a5f

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

database/src/lib.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -522,21 +522,7 @@ impl Sqlite {
522522
*b = Some(curthread);
523523
}
524524

525-
let mut c = self.conn.lock().unwrap();
526-
let tx = c.transaction_with_behavior(txb)?;
527-
528-
let mut h = Handle { tx, log: self.log.clone() };
529-
530-
let res = match func(&mut h) {
531-
Ok(res) => {
532-
h.tx.commit()?;
533-
Ok(res)
534-
}
535-
Err(e) => {
536-
h.tx.rollback()?;
537-
Err(e)
538-
}
539-
};
525+
let res = self.tx_common_locked(txb, func);
540526

541527
{
542528
let mut b = self.busy.lock().unwrap();
@@ -552,6 +538,38 @@ impl Sqlite {
552538

553539
res
554540
}
541+
542+
fn tx_common_locked<T>(
543+
&self,
544+
txb: rusqlite::TransactionBehavior,
545+
func: impl FnOnce(&mut Handle) -> DBResult<T>,
546+
) -> DBResult<T> {
547+
let mut c = self.conn.lock().unwrap();
548+
549+
let mut h = Handle {
550+
tx: c.transaction_with_behavior(txb)?,
551+
log: self.log.clone(),
552+
};
553+
554+
/*
555+
* If this routine exits before expected due to an error, make sure we
556+
* roll back any work that was done. This is the current default
557+
* behaviour, but is sufficiently critical that we explicitly request
558+
* it:
559+
*/
560+
h.tx.set_drop_behavior(rusqlite::DropBehavior::Rollback);
561+
562+
match func(&mut h) {
563+
Ok(res) => {
564+
h.tx.commit()?;
565+
Ok(res)
566+
}
567+
Err(e) => {
568+
h.tx.rollback()?;
569+
Err(e)
570+
}
571+
}
572+
}
555573
}
556574

557575
pub type DBResult<T> = std::result::Result<T, DatabaseError>;

0 commit comments

Comments
 (0)