SQLite中的SAVEPOINT机制

时间:2016-07-27 20:26:37

标签: sqlite transactions savepoints

我正在尝试理解SQLite中的Savepoints和Transactions。 我在表/数据库上有以下命令,我正在使用保存点。

SAVEPOINT aaa;
RELEASE aaa;
BEGIN;

现在,如果我一次执行上述所有语句,则会抛出错误A transaction cannot be started inside another transaction。如果我一次运行一个,它运行正常。 如果我运行前两个Savepoint并释放命令并尝试通过执行Begin启动另一个事务。它再次抛出与之前相同的错误。

链接fiddle表示

  

如果在SQLite处于自动提交模式时(即在事务之外)发出SAVEPOINT命令,则将启动标准自动提交BEGIN DEFERRED TRANSACTION。但是,与大多数命令不同,自动提交事务在SAVEPOINT命令返回后不会自动提交,从而使系统处于打开的事务中。自动事务将保持活动状态,直到释放原始保存点,或者外部事务显式提交或回滚。 `

那么,在Release Savepoint命令之后,是否绝对需要Commit或Rollback命令?是不是release命令提交并允许我们使用BEGIN启动新事务?

1 个答案:

答案 0 :(得分:1)

SAVEPOINT aaa; RELEASE aaa; BEGIN;

由sqlite解释为

BEGIN DEFERRED TRANSACTION; SAVEPOINT aaa; // Create a transaction, and mark current db state as savepoint "aaa" [1] RELEASE aaa; // Remove all db changes made since savepoint "aaa", but keep on executing the transaction BEGIN; // Create another transaction, while there is already a transaction. This will FAIL because there cannot be 2 transactions executed simultaneously

以下情况可以接受:

BEGIN; SAVEPOINT "aaa"; RELEASE "aaa"; COMMIT; BEGIN;

[1] https://sqlite.org/lang_savepoint.html