使用 Spring 2.5 从外部事务控制内部事务设置 [英] Controlling inner transaction settings from outer transaction with Spring 2.5

查看:31
本文介绍了使用 Spring 2.5 从外部事务控制内部事务设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring 2.5 事务管理并且我有以下设置:

I'm using Spring 2.5 transaction management and I have the following set-up:

Bean1

@Transactional(noRollbackFor = { Exception.class })
public void execute() {
  try {
    bean2.execute();
  } catch (Exception e) {
    // persist failure in database (so the transaction shouldn't fail)
    // the exception is not re-thrown
  }
}

Bean2

@Transactional
public void execute() {
  // do something which throws a RuntimeException
}

失败不会从 Bean1 持久化到 DB 中,因为整个事务都回滚了.

The failure is never persisted into DB from Bean1 because the whole transaction is rolled back.

我不想在 Bean2 中添加 noRollbackFor 因为它被用在很多没有逻辑来正确处理运行时异常的地方.

I don't want to add noRollbackFor in Bean2 because it's used in a lot of places which don't have logic to handle runtime exceptions properly.

有没有办法避免我的事务仅在从 Bean1 调用 Bean2.execute() 时回滚?

Is there a way to avoid my transaction to be rolled back only when Bean2.execute() is called from Bean1?

否则,我想我最好的选择是在新事务中坚持我的失败?我还能做些什么清洁工作?

Otherwise, I guess my best option is to persist my failure within a new transaction? Anything else clean I can do?

推荐答案

这是注解的警告之一...您的类不可重用!

This is one of the caveats of annotations... your class is not reusable!

如果您在 XML 中配置您的交易,如果有可能.

If you'd configure your transactions in the XML, if would have been possible.

假设您使用 XML 配置:如果它不消耗昂贵的资源,您可以创建 bean2 的另一个实例以使用您指定的代码.也就是说,您可以配置一个按照上面指定的方式,一个不进行异常回滚.

Assuming you use XML configuration: if it's not consuming expensive resources, you can create another instance of bean2 for the use of the code you specified. That is, you can configure one been as you specified above, and one with no roll back for exception.

这篇关于使用 Spring 2.5 从外部事务控制内部事务设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆