5

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

SAVEPOINT aaa;
RELEASE aaa;
BEGIN;

现在,如果我一次执行上述所有语句,它会抛出一个错误,说A transaction cannot be started inside another transaction. 如果我一次运行它们,它工作正常。如果我运行前两个 Savepoint 和 release 命令并尝试通过执行Begin. 它再次抛出与以前相同的错误。

这里的链接说

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

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

4

1 回答 1

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

于 2017-08-21T12:31:52.303 回答