@Transactional propogate transactions only for a few exceptions

笑着哭i 提交于 2020-01-17 12:13:40

问题


I am trying to make a transaction fail for all the unchecked exceptions except for a particular unchecked exception(in my case - DuplicateKeyException). How can I achieve this customization using @Transactional annotation of Spring framework ?

Thank you!


回答1:


Try the noRollbackFor option

@Transactional(noRollbackFor=DuplicateKeyException.class)



回答2:


Do it like this :

public void driverMethod(){
try{
   BeforeException()
}
catch(DuplicateKeyException e)
   AfterException()
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
private BeforeException(){
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
private AfterException(){
}


来源:https://stackoverflow.com/questions/18754357/transactional-propogate-transactions-only-for-a-few-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!