如何使用带注释的事务顾问?

发布于 2024-10-04 23:24:03 字数 765 浏览 0 评论 0原文

假设我有以下示例:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-10-11 23:24:03

使用 tx:annotation-driven 和您将使用的拦截器的 order 属性。因此,您可以指定哪个在另一个之前运行。请参阅 AOP AspectJ 建议排序 部分

Use the order attribute of tx: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文