问题
@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 RuntimeException
s 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