我之前使用的是应用程序管理的RESOURCE-LOCAL事务,但现在我想使用容器管理的JTA事务。当我使用@Stateless时,一切似乎都没问题,但是一旦我使用@Stateful,我就会得到一个例外,如下所示
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是我的代码
@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代码
@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文件
<?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并指导我出错的地方。感谢
答案 0 :(得分:1)
经过大量工作后,我发现了问题。实际上我的代码没有问题,这是因为JBoss服务器。我用Glassfish4测试了相同的应用程序,它运行得很好
的原因强>
注释@EJB对JBoss没有影响。虽然你会看到bean发生了JNDI绑定,但是当你尝试tp持久时,它就不会工作了。
的解强>