EntityManager 在 JBoss JSF bean 中的 merge() 上抛出 TransactionRequiredException
我在 JBoss 5.0.1GA 上设置了一个 JSF 应用程序,以在表中显示用户列表,并允许通过每个用户旁边的按钮删除单个用户。
当调用deleteUser 时,该调用将传递给UserDAOBean,后者获取从JBoss 注入的EntityManager。
我正在使用代码
public void delete(E entity)
{
em.remove(em.merge(entity));
}
删除用户(代码是 JPA 教程中的 c&p)。 仅调用 em.remove(entity) 没有任何效果,并且仍然会导致相同的异常。
当到达这一行时,我收到 TransactionRequiredException:(
跳过明显不相关的堆栈跟踪内容)
...
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(本机 方法)
...
我已经尝试在其周围包装手动管理的事务(em.getTransaction().begin() + .commit()),但这失败了,因为 JBoss 容器中不允许这样做。 我的 UserTransaction 也没有成功。 网上搜索这个问题也没有类似的案例和解决方案。
有没有人经历过类似的事情并找到解决方案?
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.
When deleteUser is called, the call is passed to a UserDAOBean which gets an EntityManager injected from JBoss.
I'm using the code
public void delete(E entity)
{
em.remove(em.merge(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.
When this line is reached, I'm getting a TransactionRequiredException:
(skipping apparently irrelevant stacktrace-stuff)
...
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)...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
找到了缺失的链接。
这确实是一个缺失的事务,但解决方案不是使用 EntityManager 来处理它,而是添加一个注入的 UserTransaction。
感谢所有的建议,不知何故超过 100 个角落导致了这个解决方案。
Found the missing link.
It was indeed a missing transaction but the solution was not to use the EntityManager to handle it but to add an injected UserTransaction.
Thanks to all suggestions which somehow over 100 corners lead to this solution.
知道这是一个老问题,但以防万一有人像我一样偶然发现这个问题。
尝试一下
这就是我们在所有 @Stateful beans 中使用的。
如果您使用 Seam,还可以使用 @Transactional(TransactionPropagationType.REQUIRED) 注释。
Know this is an old question, but just in case somebody stumbles on this like me.
Try
That's what we use in all our @Stateful beans.
If you are using Seam, you can also use
@Transactional(TransactionPropagationType.REQUIRED)
annotation.你确定你用@Stateless注释了你的bean还是用xml注册了它?
尝试将事务的注释添加到您的代码中,这可以帮助您:
但这看起来很奇怪,因为如果您不明确设置它,这是默认值。
Are you sure that you annotated you bean with @Stateless or register it with xml?
Try add transaction's annotation to you code, this can help you:
But it seems strange, because this is default value if you don't set it explicitly.
请注意:我们今天遇到了同样的问题,结果有人将 EJB 标记为 TransactionAttributeType.NOT_SUPPORTED,并将方法标记为 TransactionAttributeType.REQUIRED,导致 em.merge 由于缺少事务而失败。
just a note: we ran into this same issue today, turned out someone had marked the EJB as TransactionAttributeType.NOT_SUPPORTED AND the method as TransactionAttributeType.REQUIRED, causing the em.merge to fail for lack of transaction.