Rollback会自动结束Postgres中的事务

时间:2015-01-14 13:37:41

标签: postgresql transactions

使用PostgreSQL 9.1

在此代码中:

BEGIN TRANSACTION
    // Do something
If error then
    ROLLBACK  //<--Does this automatically End Transaction
Else
    COMMIT  //<--Automatically Ends Transaction
End if

ROLLBACK命令后是否需要END TRANSACTION命令?

1 个答案:

答案 0 :(得分:0)

Rollback已结束您的交易,无需其他命令。

这里有一些例子:

test_general=# begin;
BEGIN
test_general=# lock TABLE t1 ;
LOCK TABLE
test_general=# select locktype, relname, mode from pg_locks l inner join pg_class c on c.oid = l.relation;
 locktype |          relname           |        mode         
----------+----------------------------+---------------------
 relation | pg_namespace_oid_index     | AccessShareLock
 relation | pg_class_relname_nsp_index | AccessShareLock
 relation | t1                         | AccessExclusiveLock
test_general=# rollback;
ROLLBACK
test_general=# select locktype, relname, mode from pg_locks l inner join pg_class c on c.oid = l.relation;
 locktype |          relname           |      mode       
----------+----------------------------+-----------------
 relation | pg_class_relname_nsp_index | AccessShareLock
 relation | pg_class_oid_index         | AccessShareLock
 relation | pg_class                   | AccessShareLock
 relation | pg_locks                   | AccessShareLock
(4 rows)

锁定已发布意味着交易结束。