Spring @Transactional - 我可以覆盖 rollbackFor
我正在调用一个具有以下注释的服务:
@Transactional(rollbackFor=ExceptionA.class)
public void myMethodA(....) throws ExceptionA {
.
.
}
我正在从另一个 Spring Bean 中的另一个方法调用此方法。
@Transactional(rollbackFor=ExceptionB.class)
public void mainEntryPointMethod(....) throws ExceptionB {
.
try {
myMethodA()
}
catch (ExceptionA exp) {
.
}
.
}
我的问题是,如果 myMethodA 抛出异常,我的事务(默认传播从 mainEntryPointMethod -> myMethodA 传递)将被标记为回滚。有没有办法可以覆盖内部方法的“rollbackFor”?
提前致谢 克里斯
I am calling a service which has the following annotation:
@Transactional(rollbackFor=ExceptionA.class)
public void myMethodA(....) throws ExceptionA {
.
.
}
I am calling this method from an other method in another Spring Bean.
@Transactional(rollbackFor=ExceptionB.class)
public void mainEntryPointMethod(....) throws ExceptionB {
.
try {
myMethodA()
}
catch (ExceptionA exp) {
.
}
.
}
My problem is that if myMethodA throws an exception, my transaction (which is passed from mainEntryPointMethod -> myMethodA by default propagation) will be marked for rollback. Is there a way in which the 'rollbackFor' for the inner method can be overriden?
Thanks in advance
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案#1
您可以指定您正在调用的方法获取其自己的事务。
您可以通过注释该方法来做到这一点。
这样,如果方法 (myMethodA) 被标记为回滚,则仅回滚该事务,而不是调用方事务。
仅当您被允许让被调用的方法管理其自己的事务并且不想回滚调用者事务时,这才有效。
解决方案#2
也许您可以尝试子类化并更改事务注释属性。
Solution #1
You can specify that the method you are calling gets its own transaction.
You can do that by annotating the method.
That way if the method (myMethodA) is marked for roll back only that transaction will be rolled back and not the caller transaction.
That only works if you are allowed to let the called method manage its own transaction and don't want to roll back the caller transaction.
Solution #2
Maybe you can try to subclass and change the transactional annotation attributes.