6 Replies Latest reply on Jun 12, 2006 8:50 AM by hoagiex

    EntityManagerFactory collides with AutoCommit

    hoagiex

      I am trying to create an EntityManagerFactory in a Generic DAO object, but as soon an the Child Object is created this following error is thrown.

      note: I manually added ehcache-1.2.1RC.jar, since the EJB3.0 setup is lacking an ehcache jar file)

      (I'm using JBoss 4.0.4GA EJB3.0 setup with MySQL 4.1)

      java.sql.SQLException: You cannot set autocommit during a managed transaction!
       at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:482)
       at org.jboss.resource.adapter.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:322)
       at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:36)
       at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:178)
       at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:131)
       at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:308)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
       at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at com.twopro.test.pluginsecurity.integration.dao.ejb.EJBDAO.<clinit>(EJBDAO.java:30)
       at com.twopro.test.pluginsecurity.integration.dao.factory.EJBSecurityDAOFactory.getPermissionDAO(EJBSecurityDAOFactory.java:82)
       at com.twopro.test.pluginsecurity.business.as.GenericSecurityAS.getPermissionDAO(GenericSecurityAS.java:70)
       at com.twopro.test.pluginsecurity.business.as.AccessAS.createPermission(AccessAS.java:24)
       at com.twopro.test.pluginsecurity.business.facade.SecuritySessionFacade.createPermission(SecuritySessionFacade.java:44)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:225)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:358)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:398)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:239)


      Generic DAO:
      public abstract class
      EJBDAO<T>
      implements DAO<T>
      {
       public T findByID (Long anID)
       {
       //dummy, not yet in use
       return null;
       }
      
       private static EntityManagerFactory theFactory =
       Persistence.createEntityManagerFactory("security");
      
      }


      Child class is an basic empty POJO at the moment.

      persistence.xml:
      <persistence>
       <persistence-unit name="security">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/MySQLDS</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
       </properties>
       </persistence-unit>
      </persistence>


      Any thoughts anyone?


        • 1. Re: EntityManagerFactory collides with AutoCommit
          abl

          I had the same problem creating an EMF. afaik you cannot call createEntityManagerFactory in an already running transaction. create it "outside" of transaction/bean and use it to create your application managed EntityManager.

          btw: you can avoid putting ehcache to classpath by setting jboss cache as cache explicitely:

          Map<String,String> map = new HashMap<String,String>();
          map.put( "hibernate.cache.provider_class", "org.jboss.ejb3.entity.TreeCacheProviderHook");
          map.put( "hibernate.treecache.mbean.object_name", "jboss.cache:service=EJB3EntityTreeCache");
          map.put ...
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "security", map);


          • 2. Re: EntityManagerFactory collides with AutoCommit
            hoagiex

            If I try to add those options to my persitence.xml properties, I get the following exception:

            java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/jboss/cache/TreeCacheMBean
             at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:105)
             at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
             at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
             at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
             at java.lang.reflect.Method.invoke(Method.java:585)
             at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
             at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
             at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
             at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
            


            What am I lacking? .. or should/can these options not be set in the persitence.xml?

            • 3. Re: EntityManagerFactory collides with AutoCommit
              hoagiex

              Also,

              Do you have any tips on how to preload/precreate the EMF?

              I'm developing a integratable jar which supplies all classes from the Facade to the Entity Objects.... there is no point in my entire module, where no transaction is active. Can I preload the class somehow when it is deployed in the .ear ? (and then use static initialization for the EMF. Note that my module does not contain any servlets or JSPs)

              • 4. Re: EntityManagerFactory collides with AutoCommit
                abl

                you must have jboss-cache.jar and jgroups.jar in your server/default/lib and also ejb3-entity-cache-service.xml in server/default/deploy.

                maybe copy them from the "all" config or even better use jboss installer with option "ejb3 distributed"

                you can have this setting in persistence.xml also, but in my tests I had to set them again in the map passed to createEntityManagerFactory. Otherwise the hibernate default (ehcache) is used.

                • 5. Re: EntityManagerFactory collides with AutoCommit
                  abl

                   

                  "Hoagiex" wrote:
                  Also,

                  Do you have any tips on how to preload/precreate the EMF?


                  hm, tricky. we do excatly that: call it directly from frontend (servlet) and then set the EMF to some kind of configuration singleton. not nice but it works.

                  maybe someone from jboss can give a hint how to deal with something like that?!


                  • 6. Re: EntityManagerFactory collides with AutoCommit
                    hoagiex

                    Thx for all your help :)

                    The initialization part will be tricky since the module containing all the EJB classes isn't even really initialized when first called upon.

                    I.o.w. the orm mappings/JNDI bindings are deployed just in time when the first call to the module is made.