mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
sqlite3: Move begin transaction and commit into the driver
This has a slight side-effect of removing the actual begin and commit statements from the `db_write` hooks, but they are mostly redundant anyway (no harm in grouping pre-init statements into one transaction, and we know that each post-init call is supposed to be wrapped anyway). Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
492d77f213
commit
716a3b11a5
@ -16,9 +16,18 @@ def init(configuration, options, plugin):
|
|||||||
plugin.conn = sqlite3.connect(plugin.get_option('dblog-file'),
|
plugin.conn = sqlite3.connect(plugin.get_option('dblog-file'),
|
||||||
isolation_level=None)
|
isolation_level=None)
|
||||||
plugin.log("replaying pre-init data:")
|
plugin.log("replaying pre-init data:")
|
||||||
|
plugin.conn.execute("PRAGMA foreign_keys = ON;")
|
||||||
|
|
||||||
|
print(plugin.sqlite_pre_init_cmds)
|
||||||
|
|
||||||
|
plugin.conn.execute("BEGIN TRANSACTION;")
|
||||||
|
|
||||||
for c in plugin.sqlite_pre_init_cmds:
|
for c in plugin.sqlite_pre_init_cmds:
|
||||||
plugin.conn.execute(c)
|
plugin.conn.execute(c)
|
||||||
plugin.log("{}".format(c))
|
plugin.log("{}".format(c))
|
||||||
|
|
||||||
|
plugin.conn.execute("COMMIT;")
|
||||||
|
|
||||||
plugin.initted = True
|
plugin.initted = True
|
||||||
plugin.log("initialized {}".format(configuration))
|
plugin.log("initialized {}".format(configuration))
|
||||||
|
|
||||||
@ -29,9 +38,15 @@ def db_write(plugin, writes, **kwargs):
|
|||||||
plugin.log("deferring {} commands".format(len(writes)))
|
plugin.log("deferring {} commands".format(len(writes)))
|
||||||
plugin.sqlite_pre_init_cmds += writes
|
plugin.sqlite_pre_init_cmds += writes
|
||||||
else:
|
else:
|
||||||
|
print(writes)
|
||||||
|
plugin.conn.execute("BEGIN TRANSACTION;")
|
||||||
|
|
||||||
for c in writes:
|
for c in writes:
|
||||||
plugin.conn.execute(c)
|
plugin.conn.execute(c)
|
||||||
plugin.log("{}".format(c))
|
plugin.log("{}".format(c))
|
||||||
|
|
||||||
|
plugin.conn.execute("COMMIT;")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
21
wallet/db.c
21
wallet/db.c
@ -789,30 +789,29 @@ size_t db_changes(struct db *db)
|
|||||||
|
|
||||||
void db_begin_transaction_(struct db *db, const char *location)
|
void db_begin_transaction_(struct db *db, const char *location)
|
||||||
{
|
{
|
||||||
|
bool ok;
|
||||||
if (db->in_transaction)
|
if (db->in_transaction)
|
||||||
db_fatal("Already in transaction from %s", db->in_transaction);
|
db_fatal("Already in transaction from %s", db->in_transaction);
|
||||||
|
|
||||||
db_prepare_for_changes(db);
|
db_prepare_for_changes(db);
|
||||||
db_do_exec(location, db, "BEGIN TRANSACTION;");
|
ok = db->config->begin_tx_fn(db);
|
||||||
|
if (!ok)
|
||||||
|
db_fatal("Failed to start DB transaction: %s", db->error);
|
||||||
|
|
||||||
db->in_transaction = location;
|
db->in_transaction = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
void db_commit_transaction(struct db *db)
|
void db_commit_transaction(struct db *db)
|
||||||
{
|
{
|
||||||
int err;
|
bool ok;
|
||||||
char *errmsg;
|
|
||||||
const char *cmd = "COMMIT;";
|
|
||||||
|
|
||||||
assert(db->in_transaction);
|
assert(db->in_transaction);
|
||||||
db_assert_no_outstanding_statements();
|
db_assert_no_outstanding_statements();
|
||||||
|
ok = db->config->commit_tx_fn(db);
|
||||||
|
|
||||||
/* We expect at least the BEGIN TRANSACTION */
|
if (!ok)
|
||||||
db_report_changes(db, cmd, 1);
|
db_fatal("Failed to commit DB transaction: %s", db->error);
|
||||||
|
|
||||||
err = sqlite3_exec(db->sql, cmd, NULL, NULL, &errmsg);
|
|
||||||
if (err != SQLITE_OK)
|
|
||||||
db_fatal("%s:%s:%s:%s", __func__, sqlite3_errstr(err), cmd, errmsg);
|
|
||||||
|
|
||||||
|
db_report_changes(db, NULL, 0);
|
||||||
db->in_transaction = NULL;
|
db->in_transaction = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1138,6 +1138,9 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx)
|
|||||||
/* Make sure we have our references correct */
|
/* Make sure we have our references correct */
|
||||||
CHECK(transaction_wrap(w->db,
|
CHECK(transaction_wrap(w->db,
|
||||||
db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);")));
|
db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);")));
|
||||||
|
db_begin_transaction(w->db);
|
||||||
|
db_commit_transaction(w->db);
|
||||||
|
|
||||||
chan->dbid = 1;
|
chan->dbid = 1;
|
||||||
chan->peer = peer;
|
chan->peer = peer;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user