zoukankan      html  css  js  c++  java
  • PostgreSQL事务特性之嵌套事务

    嵌套事务的实现是基于SAVEPOINTROLLBACK TO SAVEPOINTRELEASE SAVEPOINT的,也就是设置一个保存点,可以回滚到保存点和释放保存点。

    测试表的初始状态如下:

    postgres=# d test 
         Table "public.test"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     id     | integer | 
     name   | text    | 
    
    postgres=# select * from test ;
     id | name 
    ----+------
    (0 rows)

    开始测试

    postgres=# begin ;
    BEGIN
    postgres=# insert into test values (1, 'a');
    INSERT 0 1
    postgres=# savepoint insert_a;
    SAVEPOINT
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# insert into test values (2, 'b');
    INSERT 0 1
    postgres=# savepoint insert_b;
    SAVEPOINT
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# insert into test values (3, 'c');
    INSERT 0 1
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
      3 | c
    (3 rows)

    现在定义了两个SAVEPOINT,并且插入了3条数据,现在测试ROLLBACK TO SAVEPOINT

    postgres=# rollback to insert_b;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# rollback to insert_a;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)

    可见回滚到前面定义的保存点成功了。

    如果回滚到前面的保存点,后面的更改就丢失了,包括保存点,比如回滚到insert_a,那么在insert_a之后的数据就没有了,insert_b这个保存点也不存在了。

    postgres=# rollback to insert_a;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# rollback to insert_b;
    ERROR:  no such savepoint

    测试RELEASE SAVEPOINT

    postgres=# select * from test ;
     id | name 
    ----+------
    (0 rows)
    
    postgres=# begin ;             
    BEGIN
    postgres=# insert into test values (1, 'a');
    INSERT 0 1
    postgres=# savepoint insert_a;              
    SAVEPOINT
    postgres=# select * from test ; 
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# insert into test values (2, 'b');
    INSERT 0 1
    postgres=# savepoint insert_b;              
    SAVEPOINT
    postgres=# select * from test ;             
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# release insert_a;
    RELEASE
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# rollback to insert_a;
    ERROR:  no such savepoint

    保存点被释放后就不能再回滚到该保存点了。

  • 相关阅读:
    NanUI for Winform发布,让Winform界面设计拥有无限可能
    新浪微博.Net SDK第三版源代码和示例【最后一次更新了】
    写个C#命令行参数解析的小工具
    Mac安装Windows 10的简明教程
    自己动手,让Entity Framework Power Tools在VS2015重放光彩
    C++CLI使用.net委托,*Callback注意"this"
    【转】IIS上的反向代理
    asp.net mvc 验证码
    win2008R2 下解决关于mysql odbc无法正常工作问题
    中国健康医学教育网
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/11242016.html
Copyright © 2011-2022 走看看