如何使用带注释的事务顾问?
假设我有以下示例:
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class MyServiceImpl implements MyService {
...
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public TransactionResponse addMyEntity(MyEntity e) throws SQLException{
...
}
...
}
在我的 applicationContext 中:
<tx:annotation-driven transaction-manager="txManager" />
好的,现在我想在抛出 SQLException 时在事务回滚后添加一个拦截器。我该怎么做?
预先感谢
编辑
我会尝试澄清我想做的事情:
我有一个WS,它在数据库上保留一些信息并返回响应(一个isOk布尔值和一个errorMessage,如果有的话)出了问题)
每当出现异常时,我需要:
- 回滚事务
- 构建包含错误消息的响应并将其返回给客户端。
这就像 spring 事务代理周围的 try/catch
supose I have the following example:
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class MyServiceImpl implements MyService {
...
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public TransactionResponse addMyEntity(MyEntity e) throws SQLException{
...
}
...
}
And in my applicationContext:
<tx:annotation-driven transaction-manager="txManager" />
Ok, now I want to add an interceptor after the rollback of the transaction if an SQLException is thrown. How can I do this?
Thanks in advance
EDIT
I'll try to clarify what I'm trying to do:
I have a WS, that persists some information on a DB and returns a response (an isOk boolean and an errorMessage if something went wrong)
Whenever an exception is risen, I need to:
- Rollback the transaction
- Build the response with the error message and return it to the client.
It's like a try/catch around spring's transaction proxy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
tx:annotation-driven
和您将使用的拦截器的order
属性。因此,您可以指定哪个在另一个之前运行。请参阅 AOP AspectJ 建议排序 部分Use the
order
attribute oftx:annotation-driven
, and of the interceptor you will be using. Thus you specify which one runs before the other. See the AOP AspectJ advice ordering section