EntityManager在JBoss JSF bean中的merge()上引发TransactionRequiredException [英] EntityManager throws TransactionRequiredException on merge() in JBoss JSF bean
问题描述
我已经在JBoss 5.0.1GA上设置了一个JSF应用程序,以在表中显示用户列表,并允许通过每个用户旁边的按钮删除单个用户.
I've set up a JSF application on JBoss 5.0.1GA to present a list of Users in a table and allow deleting of individual users via a button next to each user.
调用deleteUser时,该调用将传递到UserDAOBean,该用户将从JBoss注入EntityManager.
When deleteUser is called, the call is passed to a UserDAOBean which gets an EntityManager injected from JBoss.
我正在使用代码
public void delete(E entity)
{
em.remove(em.merge(entity));
}
删除用户(JPA教程中的代码为c& p).仅仅调用em.remove(entity)无效,仍然会导致相同的异常.
to delete the user (code was c&p from a JPA tutorial). Just calling em.remove(entity) has no effect and still causes the same exception.
到达此行时,我收到了TransactionRequiredException:
When this line is reached, I'm getting a TransactionRequiredException:
(跳过似乎不相关的stacktrace-stuff)
(skipping apparently irrelevant stacktrace-stuff)
...
...
20:38:06,406错误[[Faces Servlet]] Servlet Faces的Servlet.service() Servlet抛出异常 javax.persistence.TransactionRequiredException: EntityManager必须在 交易于 org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:155) 在 org.jboss.jpa.tx.TransactionScopedEntityManager.merge(TransactionScopedEntityManager.java:192) 在 at.fhj.itm.utils.DAOImplTemplate.delete(DAOImplTemplate.java:54) 在 at.fhj.itm.UserBean.delete(UserBean.java:53) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机 方法)
20:38:06,406 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction at org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:155) at org.jboss.jpa.tx.TransactionScopedEntityManager.merge(TransactionScopedEntityManager.java:192) at at.fhj.itm.utils.DAOImplTemplate.delete(DAOImplTemplate.java:54) at at.fhj.itm.UserBean.delete(UserBean.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
我已经尝试在其周围包装一个手动管理的事务(em.getTransaction().begin()+ .commit()),但这失败了,因为JBoss容器中不允许这样做.我也没有与UserTransaction成功.在网络上搜索此问题的情况也没有类似的情况和解决方案.
I already tried to wrap a manually managed transaction (em.getTransaction().begin() + .commit() ) around it, but this failed because it is not allowed within JBoss container. I had no success with UserTransaction either. Searches on the web for this issue also turned up no similar case and solution.
任何人以前都经历过类似的事情并找到了解决方案吗?
Has anyone experienced something similar before and found a solution to this?
推荐答案
找到了丢失的链接.
这确实是丢失的事务,但是解决方案不是使用EntityManager来处理它,而是添加注入的UserTransaction.
It was indeed a missing transaction but the solution was not to use the EntityManager to handle it but to add an injected UserTransaction.
@Resource
UserTransaction ut;
...
public void delete(E entity)
{
ut.begin();
em.remove(em.merge(entity));
ut.commit();
}
感谢所有建议,这些建议以某种方式导致了100个弯道.
Thanks to all suggestions which somehow over 100 corners lead to this solution.
这篇关于EntityManager在JBoss JSF bean中的merge()上引发TransactionRequiredException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!