来自委托服务方法的 Spring+OpenJPA TransactionRequiredException

发布于 2024-11-10 03:06:43 字数 1090 浏览 4 评论 0原文

我们在 Spring 3.0.5 事务管理和 OpenJPA 2.0.1 方面遇到了问题,而且我们似乎无法查明问题所在,因此非常感谢您的帮助。

该架构可以细分如下:

服务层

@Autowired
private DAOInterface daoReference;
....
public void doStuff() {
     boolean test = performCheck();
}
....
private boolean performCheck() {
     return daoReference._performDAOCheck();
}

DAO 层

@PersistenceContext
private EntityManager entityManager;
.....
@Transactional
public boolean _performDAOCheck() {
     boolean result = true;
     // fetch entity manager, perform something and return boolean value
     return result;
}

这段代码按其应有的方式执行,但我们真的不喜欢 DAO 层上事务划分的存在,并且想将其转移到上面的服务层。

但是,如果我们将 @Transactional 注释移至服务层委托方法并将其从 DAO 层中删除,我们会得到 javax.persistence.TransactionRequiredException ,这表明需要事务但不活跃。

问题是 - 为什么以及如何从委托方法“激活”事务?请注意,我们已经尝试了各种事务传播修饰符,但它似乎没有做任何有用的事情(REQUIRES_NEW 是唯一真正适用于这种情况的修饰符,但我们尝试了其他修饰符只是为了安全起见)。

应用程序堆栈如下:

  1. Spring 3.0.5
  2. Bitronix 2.1.1
  3. OpenJPA 2.0.1
  4. Tomcat 6.0.32
  5. JUnit 4.8.2用作测试框架

We're running into problems with Spring 3.0.5 transactional management and OpenJPA 2.0.1 and we can't seem to pinpoint the problem so any help is appreciated.

The architecture is can be broken down as follows:

service-layer

@Autowired
private DAOInterface daoReference;
....
public void doStuff() {
     boolean test = performCheck();
}
....
private boolean performCheck() {
     return daoReference._performDAOCheck();
}

DAO-layer

@PersistenceContext
private EntityManager entityManager;
.....
@Transactional
public boolean _performDAOCheck() {
     boolean result = true;
     // fetch entity manager, perform something and return boolean value
     return result;
}

This code performs as it should but we really dislike the existence of transactional demarcation on DAO-layer and would like to transfer that to service-layer above.

However should we move the @Transactional annotation to service-layer delegate method and remove it from DAO layer, we get the javax.persistence.TransactionRequiredException which indicates that a transaction is required but is not active.

The question is - why and how do we "activate" the transaction from delegate method? Mind you, we've tried various transaction propagation modifiers, but it didn't seem to do anything useful (REQUIRES_NEW is the only actually applicable in this context, but we tried the others just to be on the safe side).

The application stack is as follows:

  1. Spring 3.0.5
  2. Bitronix 2.1.1
  3. OpenJPA 2.0.1
  4. Tomcat 6.0.32
  5. JUnit 4.8.2 is used as testing framework

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷情 2024-11-17 03:06:43

为了将来的参考——看来我们已经解决了这个问题——它是关于 Spring AOP 代理的,它不会注意到本地方法调用。

如果服务层performCheck()移动到另一个spring bean,注入然后调用,代理会正确生成一个新的事务上下文,并且事情会像魅力一样工作。

For future reference - it seems we've nailed down the problem - it's about Spring AOP proxy which doesn't notice local method calls.

If service-layer performCheck() is moved to another spring bean, injected and then called, proxy correctly generates a new transactional context and the thing works like a charm.

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