0

While trying to persist an entity, I am getting the below exception.

public class MyClass {

    EntityManagerFactory emFactory;
    EntityManager entityManager;

    public MyClass(){
        emFactory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = emFactory.createEntityManager();
    }

    public void getData() {     
        try {
            TrigData trigData = new TrigData();
            trigData.setKey("abc");     
            entityManager.getTransaction().begin();
            entityManager.persist(trigData);
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


19:31:51,508 ERROR [stderr] (default task-6) java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
19:31:51,508 ERROR [stderr] (default task-6)    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:1368)
19:31:51,509 ERROR [stderr] (default task-6)    at com.xyz.services.MyClass.getData(MyClass.java:42)
19:31:51,509 ERROR [stderr] (default task-6)    at com.xyz.services.MyClass$Proxy$_$$_WeldClientProxy.getData(Unknown Source)
19:31:51,509 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
19:31:51,510 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
19:31:51,510 ERROR [stderr] (default task-6)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
19:31:51,510 ERROR [stderr] (default task-6)    at java.lang.reflect.Method.invoke(Method.java:606)
19:31:51,510 ERROR [stderr] (default task-6)    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:167)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
19:31:51,512 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
19:31:51,512 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
19:31:51,513 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="persistenceUnit" > 
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>    
        <class>com.xyz.TrigData</class>

        <properties>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>
Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • Jboss in that setup should be responsible for handling the transactions. Details depend on your setup. EJB? – mh-dev Mar 30 '16 at 14:17
  • @ep Since this is a JTA managed, we cannot use `getTransaction()` method. Although I never tried this, my best guess is, I think the correct way to do is get the `UserTransaction` from JNDI lookup and using to to commit/rollback. Are you running this code in the JBoss server or is it stanalone app? – Madhusudana Reddy Sunnapu Mar 30 '16 at 14:18
  • @mh-dev : Its not EJB – Nidheesh Mar 30 '16 at 14:22
  • @MadhusudanaReddySunnapu: It is deployed on WildFly 9 server – Nidheesh Mar 30 '16 at 14:22
  • 2
    @ep Then you may want to try with either inject or do JNDI look up of `UserTransaction` and use it to control transactions instead of `getTransaction()`. – Madhusudana Reddy Sunnapu Mar 30 '16 at 14:24
  • Or you can make the above code work by using a tag and define a datasource that isn't a part of JTA transactions instead of and specify see tomee.apache.org/jpa-concepts.html – Chris Mar 30 '16 at 19:09

1 Answers1

0

If you are attempting to deploy on Wildfly, then get your EntityManager with the @PersistenceContext annotation. All of the Wildfly quickstarts have a utility class that @Produces the entity Manager to be injected into other beans:

public class Resources {
    @Produces
    @PersistenceContext
    private EntityManager em;

    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

    @Produces
    @RequestScoped
    public FacesContext produceFacesContext() {
        return FacesContext.getCurrentInstance();
    }
}

It would be used like so:

@Stateless
public class Service {

    @Inject
    private EntityManager em;

Helpful Crossreference: How to inject EntityManager in CDI (weld)?

Community
  • 1
  • 1
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66