spring 的异常驱动的事务回滚之谜

来源:互联网 发布:无线传感器网络的特点 编辑:程序博客网 时间:2024/06/05 00:30
Spring的事务管理默认是针对unchecked exception回滚
即针对RuntimeException()异常极其子类回滚


事务回滚的实验
  • 1.参照的标准无回滚事务流程
@Service
@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     增加了新纪录,并被更新了
  • 2.加入RuntimeException的标准回滚事务流程
@Service
@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的记录

  • 3.抛出一个自定义的Runtime异常,也能引起spring 事务回滚
@Service
@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的记录

  • 4.抛出一个普通异常(例子中是一个普通的IO异常),不会引起spring 事务回滚,事务正常执行完成(尽管期间发生了异常,数据可能是无效数据)
@Service
@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);都没回滚
  • 5.还是上例,虽然抛出一个普通IO异常,但通过(rollbackFor=Exception.class),设置所有checked异常也回滚
    @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的记录,显然回滚了
  • 6.上面是把(rollbackFor=Exception.class)设在方法上,现在设到整个Class 上
@Service
@Transactional(rollbackFor=Exception.class)
public class StuService_dbcp implements StuService_dbcpIntf {执行:
java.io.FileNotFoundException: d:\,,jjygfsjhkghsdfsadf (系统找不到指定的文件。)

4:clair
2:testTran128504319
                           显然回滚了
  • 7.以上例子,在ORACLE上测试通过,但在mysql上测试没通过
java.lang.ArithmeticException: / by zero
1:sily
2:testTran1651919412
3:testTran-233915713          出了runtime异常,但仍然插入了原因:MyISAM engine支持事务很差
  • 8.改成在mysql innoDB Engine上测试通过:
mysql> drop table stu;
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的新纪录,说明回滚了.
0 0
原创粉丝点击