1 Reply Latest reply on Nov 24, 2013 5:30 PM by chienlou

    Problem with EntityManager, Container vs Bean transactions

    chienlou Newbie

      Hello,

      Migrating my old app to wildfly, I'm having a strange issue with EntityManager throwing exception "you cannot commit / rollback during managed transaction":

       

      Here is a Bean managed version:

      @Stateful
      @TransactionManagement(value = TransactionManagementType.BEAN)
      public abstract class JpaDao<K, E> implements Dao<K, E> {
      
      @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "here-pu")
      protected EntityManager entityManager;
      
      public void persist(E entity) {
           entityManager.getTransaction().begin();
           try {
                  entityManager.persist(entity);
                  entityManager.getTransaction().commit();
             } catch (Exception e) {
             e.printStackTrace();
             if (entityManager.getTransaction().isActive())
             entityManager.getTransaction().rollback();
      }
      }
      
      

       

      Or the default Container w/out transaction management:

       

      @Stateful
      public abstract class JpaDao<K, E> implements Dao<K, E> {
      public void persist(E entity) {
      
      @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "here-pu")
      protected EntityManager entityManager;
      
      public void persist(E entity) {
           try {
                  entityManager.persist(entity);      
           } catch (Exception e) {
             e.printStackTrace();
           }
      }
      
      

       

      It says either this below, or "cannot commit" if I use the Container version"above:

      Caused by: org.hibernate.TransactionException: rollback failed

        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:215) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

        at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:108) [hibernate-entitymanager-4.3.0.Beta4.jar:4.3.0.Beta4]

        ... 68 more

      Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection

        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:167) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:209) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

        ... 69 more

      Caused by: java.sql.SQLException: You cannot rollback during a managed transaction!

        at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcRollback(BaseWrapperManagedConnection.java:1110)

        at org.jboss.jca.adapters.jdbc.WrappedConnection.rollback(WrappedConnection.java:779)

        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:163) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

        ... 70 more

       

       

      19:45:10,988 INFO  [org.hibernate.engine.transaction.internal.jdbc.JdbcIsolationDelegate] (EJB default - 7) HHH000363: Unable to rollback connection on exception [java.sql.SQLException: You cannot rollback during a managed transaction!]

      19:45:10,988 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 7) SQL Error: 0, SQLState: null

      19:45:10,989 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 7) You cannot commit during a managed transaction!

      19:45:10,989 ERROR [stderr] (EJB default - 7) javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: error performing isolated work

       

      Here is my persistence.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.0"
        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 persistence_2_0.xsd">
        <persistence-unit name="here-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/ds/coucou</jta-data-source>
      
        <properties>
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        </properties>
        </persistence-unit>
      </persistence>
      
      

       

      What is wrong with that?

      Any hint appreciated,