try catch影响Spring事务吗?

对于这个问题有两种情况:

  1.catch只打印异常,不抛出异常

try {
        数据库做添加订单表;
        int a=5/0;
        数据库减少库存;
        }catch (Exception e){
            e.printStackTrace();
        }

此方法会影响事务,此时数据库中订单数据会插入成功!因为Spring的事物的标准是RuntimeException

2.catch打印异常,并抛出异常

try {
        数据库做添加订单表;
        int a=5/0;
        数据库减少库存;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }

此方法不会影响事务,因为抛出了RuntimeException


版权声明:本文为qq_39539575原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。