设置保存点
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条记录