4 Replies Latest reply on May 24, 2005 3:28 AM by Grzegorz Knapczyk

    Batch insert within session bean

    Grzegorz Knapczyk Newbie

      How to implement batch insert within a session bean?
      When I try to @Inject UserTransaction in session bean the server raise an error:


      java.lang.IllegalStateException: Container org.aplusc.portal.ejb.stateless.TvmanTypDAOBean: it is illegal to inject UserTransaction into a CMT bean


      I'am working with jboss 4.0.1.sp1 AS and EJB 3.0 Preview 5.0.


        • 1. Re: Batch insert within session bean
          Bill Burke Master

          as the error message implies...It is illegal to insert the UserTransaction service into a session or MDB that has Container Managed Transactions.

          • 2. Re: Batch insert within session bean
            Grzegorz Knapczyk Newbie

            Thanks for your answer. I am new to EJB, could you please tell me how may I force to flush data to the underlaying database within the session bean method?
            I've tried with em.flush(), but it seems not to work:

            @Stateless
            public class TypesDAOBean implements TypesDAO {
             @Inject
             private EntityManager em;
            
             void batchInsertMethod()
             {
             TypeEntity e1;
             while (e1 = readFromTXTFile())
             {
             em.persist( e1 );
             em.flush();
             }
             }
            }
            


            or
            @Stateless
            public class TypesDAOBean implements TypesDAO {
             @Inject
             private EntityManager em;
            
             void batchInsertMethod()
             {
             org.jboss.ejb3.entity.HibernateSession hs = (HibernateSession)em;
             org.hibernate.Session session = hs.getHibernateSession();
            
             TypeEntity e1;
             while (e1 = readFromTXTFile())
             {
             em.persist( e1 );
             session.flush();
             session.clear();
             }
             }
            }
            

            In both cases the data are flushed after leaving the method batchInsertMethod().

            • 3. Re: Batch insert within session bean
              Bill Burke Master

              You do know that batchInsertMethod() is running in a JTA transaction right? So, if you have code running in parallel, then you won't see these changes in the database...

              Or...

              Are you doing code from within batchInsertMethod that is trying to interact with persisted entities?

              • 4. Re: Batch insert within session bean
                Grzegorz Knapczyk Newbie

                Is it possible than to controll the transaction within batchInsertMethod()?
                When I try to import large TXT file (with a lot of rows) within a single transaction, I crash the JBOSS server (out of heap)...