zoukankan      html  css  js  c++  java
  • oracle 事务

    设置保存点
    savepoint test;
    取消部分事务
    rollback to test;
    取消全部事务
    rollback;

    insert into emp(empno, job) values(1, '测试');
    insert into emp(empno, job) values(2, '测试2');
    insert into emp(empno, job) values(3, '测试3');
    commit;
    savepoint test1;
    delete emp where empno = 1;
    savepoint test2;
    delete emp where empno = 2;
    savepoint test3;
    delete emp where empno = 3;
    rollback to test3;
    rollback to test2;
    rollback to test1;

    delete emp where empno in(1, 2, 3);
    insert into emp(empno, job) values(1, '测试');
    insert into emp(empno, job) values(2, '测试2');
    insert into emp(empno, job) values(3, '测试3');
    commit;
    savepoint test1;
    delete emp where empno = 1;
    savepoint test2;
    delete emp where empno = 2;
    savepoint test3;
    delete emp where empno = 3;
    rollback to test1;
    rollback to test2; //报错,保存点不存在
    rollback to test3; //报错,保存点不存在

    delete emp where empno in(1, 2, 3);
    insert into emp(empno, job) values(1, '测试');
    insert into emp(empno, job) values(2, '测试2');
    insert into emp(empno, job) values(3, '测试3');
    commit;
    savepoint test1;
    delete emp where empno = 1;
    savepoint test2;
    delete emp where empno = 2;
    savepoint test3;
    delete emp where empno = 3;
    rollback; // 已回退到test1
    rollback to test1; //报错,保存点不存在
    rollback to test2; //报错,保存点不存在
    rollback to test3; //报错,保存点不存在


     java 事务

    1、connection.setAutoCommit(false);
    Statement statement = connection.createStatement();
    statement.executeUpdate("update ...");
    int i = 7/0;
    statement.executeUpdate("update ...");
    2、connection.commit();
    异常处理:
    3、connection.rollback();


    只读事务

    使用只读事务可以确保用户只能取得某个时间点的数据。假定机票代售点每天18点开始统计今天的销售情况,这时可以使用只读事务,尽管这时其它会话可能会提交新的事务,但是只读事务将不会取得最新变化的数据。
    设置只读事务
    set transaction read only;

    开启一个sqlplus
    conn system/root ; // 注意不要使用sysdba登陆,否则不起作用
    set transaction read only;
    select * from emp; 14条记录

    再开启一个sqlplus
    conn scott/root;
    select * from emp; 14条记录
    insert into emp(empno, ename) values(1, '张三');
    commit;
    select * from emp; 15条记录

    system用户:
    select * from emp; 15条记录

  • 相关阅读:
    【SQL】含有NULL值的排序
    【SQL】结构化查询语言
    【Oracle】体系结构
    【PL/SQL】匿名块、存储过程、函数、触发器
    【Linux】VMware安装VMware Tools工具
    【SQL】INTERVAL YEAR TO MONTH 和 INTERVAL DAY TO SECOND
    Tornado源码浅析
    【Python之路】特别篇--property()函数 和 @property 装饰符
    【Python之路】特别篇--生成器(constructor)、迭代器(iterator)、可迭代对象(iterable)
    【Python之路】特别篇--基于领域驱动模型架构设计的京东用户管理后台
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9291238.html
Copyright © 2011-2022 走看看