已经在JBoss中的资源本地EntityManager上调用了joinTransaction [英] joinTransaction has been called on a resource-local EntityManager in JBoss
问题描述
我之前使用的是应用程序管理的RESOURCE-LOCAL事务,但现在我想使用容器管理的JTA事务。当我使用@Stateless时,一切似乎都没问题,但是一旦我使用@Stateful,我就会得到一个例外,如下所示
I have earlier worked with application-managed RESOURCE-LOCAL transaction but now I want to use container-managed JTA transaction. Everything seems to be ok while I am using @Stateless but as soon as I use @Stateful I get an exception as below
javax.ejb.EJBException: javax.persistence.TransactionRequiredException: joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.
我正在使用JBoss eap 6.2和eclipselink2.5以及Java8和Oracle.Here是我的代码
I am using JBoss eap 6.2 with eclipselink2.5 and Java8 and Oracle.Here are my codes
@Stateful
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class LoginDetailService {
@PersistenceContext(unitName="OracleDB", type=PersistenceContextType.EXTENDED)
protected EntityManager em;
public void addLoginDetails(String email, String pwd){
LoginDetail ld = new LoginDetail(email,pwd);
em.persist(ld);
}
@Remove
public void finished(){}
}
我的Servlet代码
My Servlet code
@WebServlet("/signup")
public class SignUpServlet extends HttpServlet {
@EJB LoginDetailService bean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = "EMAIL",
pwd = "PASSWORD";
bean.addLoginDetails(email, pwd); //exception occurs here
response.getWriter().println("Successful");
}
}
我的persistence.xml文件
And my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="OracleDB" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/jdbc/OracleDB</jta-data-source>
<class>com.entity.Student</class>
<class>com.entity.LoginDetail</class>
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
Plz hekp并指导我出错的地方。谢谢
Plz hekp and guide me where I am going wrong. Thanks
推荐答案
最后经过大量工作后,我发现了问题。实际上我的代码没有问题,这是因为JBoss服务器。我使用Glassfish4测试了相同的应用程序并且它运行良好。
REASON
注释@EJB对JBoss没有影响。虽然你会看到bean发生了JNDI绑定,但是当你尝试tp持续时,它就不会工作。
解决方案
Finally after working a lot, I found the problem. Actually there was no issue with my code, it was because of the JBoss server. I tested the same application with Glassfish4 and it worked perfectly.
REASON
The annotation @EJB has no effect in JBoss. Though you will see that a JNDI binding has occurred with the bean but when you will try tp persist, it wont work.
SOLUTION
- 要使其在JBoss而不是@EJB上运行,您必须执行JNDI查找并执行事务。但由于某些原因导致查找在我的桌面上失败,但在笔记本电脑上工作正常可能是由于一些奇怪的服务器配置。
- 我觉得另一个更好的解决方案是使用像Glassfish或WebLogic这样的其他服务器,其中@EJB工作正常,而不是一点额外的编码。
这篇关于已经在JBoss中的资源本地EntityManager上调用了joinTransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!