1. 程式人生 > >try catch影響Spring事務嗎?

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