来自委托服务方法的 Spring+OpenJPA TransactionRequiredException
我们在 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 是唯一真正适用于这种情况的修饰符,但我们尝试了其他修饰符只是为了安全起见)。
应用程序堆栈如下:
- Spring 3.0.5
- Bitronix 2.1.1
- OpenJPA 2.0.1
- Tomcat 6.0.32
- 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:
- Spring 3.0.5
- Bitronix 2.1.1
- OpenJPA 2.0.1
- Tomcat 6.0.32
- JUnit 4.8.2 is used as testing framework
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将来的参考——看来我们已经解决了这个问题——它是关于 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.