From 1f7e370fda3976cd22de5a851dcc6de5560d7156 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 1 Nov 2017 11:40:48 +1030 Subject: [PATCH] db: rollback transaction if we had an error. This is temporary; we'll eventually fail on error. However, since db_exec() is a NOOP if we have an error, we need to do something. --- wallet/db.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/wallet/db.c b/wallet/db.c index 88a63de92..7036ae639 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -236,8 +236,24 @@ bool db_begin_transaction(struct db *db) bool db_commit_transaction(struct db *db) { + bool ret; + assert(db->in_transaction); - bool ret = db_exec(__func__, db, "COMMIT;"); + if (db->err) { + char *errmsg; + int err; + + /* Do this manually: db_exec is a NOOP with db->err */ + err = sqlite3_exec(db->sql, "ROLLBACK;", NULL, NULL, &errmsg); + if (err != SQLITE_OK) { + db->err = tal_fmt(db, "%s then ROLLBACK failed:%s:%s", + db->err, sqlite3_errstr(err), errmsg); + sqlite3_free(errmsg); + } + ret = false; + } else { + ret = db_exec(__func__, db, "COMMIT;"); + } db->in_transaction--; return ret; }