try catch影响Spring事务吗?

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

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

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

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

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

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

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

猜你喜欢

转载自www.cnblogs.com/WPF0414/p/9991456.html