2020年8月5日 星期三

@Transactional(rollbackFor = Exception.class)

因為Spring的設計之初參考EJB,預設只會對unchecked exceptions去rollback,如果自己開發的程式對自訂Business Exception(比如傳入的訂單金額為負數)需要RollBack,需要用annotation方式設定@Transactional(rollbackFor = Exception.class),對checked exceptions也rollback。


Although EJB container default behavior automatically rolls back the transaction on a system exception (usually a runtime exception), EJB CMT does not roll back the transaction automatically on anapplication exception (that is, a checked exception other than java.rmi.RemoteException). While the Spring default behavior for declarative transaction management follows EJB convention (roll back is automatic only on unchecked exceptions), it is often useful to customize this behavior.


ref:

沒有留言:

張貼留言