What is difference between @Transactional(rollbackFor = Exception.class) and @Transactional(propagation=Propagation.REQUIRED)

泄露秘密 提交于 2020-01-15 10:15:32

问题


@Transactional(rollbackFor = Exception.class) 
public void foo1() {
    `/**Some Code**/`
}

@Transactional(propagation=Propagation.REQUIRED)
public void foo2() {
    `/**Some Code**/`
}

回答1:


@Transactional(propagation=Propagation.REQUIRED) and @Transactional(rollbackFor = Exception.class) are roughly the same. As propagation=Propagation.REQUIRED is the default. So with that in mind they are equivalent to @Transactional(propagation=Propagation.REQUIRED) and @Transactional(propagation=Propagation.REQUIRED, rollbackFor = Exception.class).

The only difference is that without the rollbackFor = Exception.class it will rollback only for RuntimeExceptions and Error s not for other exceptions that occur. (This is the same for JEE when using EJB and the behavior has been translated to Spring as well).

This is also explained in the javadoc of @Transactional.




回答2:


Firstly, by default propagation is always present if you write it or not. If you write rollbackFor then the transaction will be rollback if an exception happens.

Here is a link for more help:@Transactional



来源:https://stackoverflow.com/questions/54898227/what-is-difference-between-transactionalrollbackfor-exception-class-and-tr

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