joinTransaction has been called on a resource-local EntityManager in JBoss

点点圈 提交于 2019-12-12 04:37:09

问题


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.

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(){}
}

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");
    }
}

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 and guide me where I am going wrong. Thanks


回答1:


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

  1. To make it work on JBoss instead of @EJB, you will have to do a JNDI lookup and carry out the transaction. But the lookup for some reason failed on my desktop but worked fine on laptop may be due to some weird server configuration.
  2. Another and better solution which I feel is to use another server like Glassfish or WebLogic where @EJB works fine and not a single bit of extra coding.


来源:https://stackoverflow.com/questions/25459252/jointransaction-has-been-called-on-a-resource-local-entitymanager-in-jboss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!