2015-11-16 17 views
3

比方說,我在Laravel使用事務:我如何知道Laravel的交易是否順利?

DB::transaction(function() { 

    // Do your SQL here 

}); 

有什麼辦法後,我可以知道,如果交易順利與否?

DB::transaction(function() { 

    // Do your SQL here 

}); 

// Here how can I know if the transaction is ok? 

回答

2

事務應該按預期工作,除非某些組件的事務執行過程中拋出一個異常

如果你想檢查一些具體的事情了錯誤,你可以這樣做:

try 
{ 
    DB::transaction(function() { 

    // Do your SQL here 

    }); 

    //at this point everything is OK if no exceptions is raised 
} 
catch(PDOException $e) 
{ 
    //something went wrong with mysql: handle your exception 
} 
catch(Exception $e) 
{ 
    //something went wrong with some other component 
} 

但請記住,Laravel將自行處理異常情況,因此如果您不需要執行特定的操作,則可以假設交易正常,如果交易主體內部沒有發生錯誤

相關問題