spring 的异常驱动的事务回滚之谜
来源:互联网 发布:无线传感器网络的特点 编辑:程序博客网 时间:2024/06/05 00:30
Spring的事务管理默认是针对unchecked exception回滚
即针对RuntimeException()异常极其子类回滚
事务回滚的实验
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
public void TestTransaction(){
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(2);
stu.setStu_name(strBuf.toString());
this.save1(stu); //先存
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu); //再更新看结果:
4:clair
2:testTran128504319 增加了新纪录,并被更新了
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
public void TestTransaction(){
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(3); 加一个新的id存入
stu.setStu_name(strBuf.toString());
this.save1(stu); //先存
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu); //再更新
int a =0;
int b = 3/a; //会造成除0 runtimeException结果:
java.lang.ArithmeticException: / by zero
4:clair
2:testTran128504319
没增加新的id=3的记录
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
public void newRuntime(){
throw new RuntimeException("运行期异常 unchecked异常");
}
public void TestTransaction(){
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(3);
stu.setStu_name(strBuf.toString());
this.save1(stu);
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu);
newRuntime(); // 自定义抛出一个runtime异常
} 运行:
java.lang.RuntimeException: 运行期异常 unchecked异常
4:clair
2:testTran128504319
没增加新的id=3的记录
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
public void TestTransaction() throws IOException{
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(3);
stu.setStu_name(strBuf.toString());
this.save1(stu);
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu);
File file = new File("d:/,,jjygfsjhkghsdfsadf");
FileReader in = new FileReader(file);
char[] data = new char[20];
in.read(data);java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)
4:clair
2:testTran128504319
3:testTran1895510505 看,id=3的记录已经存入了,说明save1(stu);和update(stu);都没回滚
@Transactional(rollbackFor=Exception.class)
public void TestTransaction() throws IOException{
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(3);
stu.setStu_name(strBuf.toString());
this.save1(stu);
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu);
File file = new File("d:/,,jjygfsjhkghsdfsadf");
FileReader in = new FileReader(file);
char[] data = new char[20];
in.read(data);
}执行:
java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)
4:clair
2:testTran128504319
没有id=3的记录,显然回滚了
@Transactional(rollbackFor=Exception.class)
public class StuService_dbcp implements StuService_dbcpIntf {执行:
java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)
4:clair
2:testTran128504319
显然回滚了
1:sily
2:testTran1651919412
3:testTran-233915713 出了runtime异常,但仍然插入了原因:MyISAM engine支持事务很差
Query OK, 0 rows affected (0.00 sec)
mysql> create table stu (stu_id int(5),stu_name varchar(50),primary key(stu_id)) ENGINE=INNODB;
Query OK, 0 rows affected (0.02 sec)
插入一个新纪录:0:sily
执行:
public void TestTransaction() throws IOException{
StringBuffer strBuf = new StringBuffer();
strBuf.append("testTran");
Random ran = new Random();
Stu stu = new Stu();
stu.setStu_id(3); // 再插入一个id=3的新记录
stu.setStu_name(strBuf.toString());
this.save1(stu);
strBuf.append(ran.nextInt());
stu.setStu_name(strBuf.toString());
update(stu);
int a =0;
int b = 3/a;看结果:
java.lang.ArithmeticException: / by zero
0:sily
没插入id=3的新纪录,说明回滚了.
即针对RuntimeException()异常极其子类回滚
事务回滚的实验
- 1.参照的标准无回滚事务流程
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
4:clair
2:testTran128504319
- 2.加入RuntimeException的标准回滚事务流程
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
java.lang.ArithmeticException: / by zero
4:clair
2:testTran128504319
- 3.抛出一个自定义的Runtime异常,也能引起spring 事务回滚
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
}
java.lang.RuntimeException: 运行期异常 unchecked异常
4:clair
2:testTran128504319
- 4.抛出一个普通异常(例子中是一个普通的IO异常),不会引起spring 事务回滚,事务正常执行完成(尽管期间发生了异常,数据可能是无效数据)
@Transactional
public class StuService_dbcp implements StuService_dbcpIntf {
4:clair
2:testTran128504319
3:testTran1895510505
- 5.还是上例,虽然抛出一个普通IO异常,但通过(rollbackFor=Exception.class),设置所有checked异常也回滚
java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)
4:clair
2:testTran128504319
- 6.上面是把(rollbackFor=Exception.class)设在方法上,现在设到整个Class 上
@Transactional(rollbackFor=Exception.class)
public class StuService_dbcp implements StuService_dbcpIntf {执行:
java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)
4:clair
2:testTran128504319
- 7.以上例子,在ORACLE上测试通过,但在mysql上测试没通过
1:sily
2:testTran1651919412
3:testTran-233915713
- 8.改成在mysql innoDB Engine上测试通过:
Query OK, 0 rows affected (0.00 sec)
mysql>
Query OK, 0 rows affected (0.02 sec)
插入一个新纪录:0:sily
执行:
java.lang.ArithmeticException: / by zero
0:sily
0 0
- spring 的异常驱动的事务回滚之谜
- Spring中发生异常的事务回滚问题
- Spring异常捕获而且回滚事务的方法
- 实验spring事务回滚与异常的关系
- Spring管理事务默认回滚的异常是什么?
- Spring管理事务默认回滚的异常是什么?
- Spring管理事务默认回滚的异常是什么?
- spring事务的回滚
- Spring的事务回滚
- Spring事务异常回滚
- Spring事务异常回滚
- Spring MVC 注解事务 之 异常回滚
- Spring-Service-事务中线程异常执行事务回滚的方式
- spring frame 事务回滚的测试
- 关于Spring事务回滚的问题
- 关于Spring事务回滚的问题
- Spring事务回滚的问题
- 关于Spring事务回滚的问题
- java 正则表达式
- 暴风影音5【版本:5.36.0428.1111】---给您影院般的“极致”体验
- ZOJ 3700 Ever Dream
- swig
- HDU 1002 A+B Problem
- spring 的异常驱动的事务回滚之谜
- [MySQL]查询
- 64位windows7 上安装32位oracle 10g 的方法
- Android tabHost 刷新Activity
- RTMP流媒体技术零基础学习方法
- 国嵌消息队列实验代码解析
- Smarty模板引擎XSS漏洞的出现与防范分享
- 一双跑鞋的mybatis(六)---使用generator自动生成mybatis映射代码
- 我对j2ee的认识