4 Replies Latest reply on Apr 30, 2009 8:31 AM by Gregory Nikle

    Non managed JPA EntityManager is broken.

    Gregory Nikle Apprentice

      Hi, I use Seam 2.0.1GA in asynchronous method with non managed JPA Entity Manager.
      Method is executed each second. After couple minutes I get


      Here I gete my emf



      This is my code


      EntityManagerFactory sguniOneEMF = Persistence.createEntityManagerFactory("sguniOneDatabase");
      EntityManager  ee = sguniOneEMF.createEntityManager();
                              try {
                                    ee = EMController.getEM(em);
                                    ee.getTransaction().begin();
                                                      //here is nothink ;)
                                    ee.getTransaction().commit();
                              } catch (Exception e) {
                                   if (ee != null) {
                                        if (ee.getTransaction().isActive())
                                             ee.getTransaction().setRollbackOnly();
                                   }
                              } finally {
                                   if (ee != null)
                                        ee.close();
                              }



      After while I get


      2009-02-03 12:41:58,109 INFO  [STDOUT] 12:41:58,109 ERROR
      [JDBCExceptionReporter] FATAL: sorry, too many clients already
      



      I seems that my em is not closed, but It is!


      Can anybody explain me, what's wrong?
      For some reasons I don't want to use Seam managed EM.

        • 1. Re: Non managed JPA EntityManager is broken.
          Zoltan Tiringer Newbie

          Most likely your method (sometimes) takes longer than a second to execute, and eventually you run out of allowed DB sessions...


          Consult DBA to increase number of sessions or reduce execution frequency or optimize execution time

          • 2. Re: Non managed JPA EntityManager is broken.
            Stuart Douglas Master

            Are you actually creating the EntityManagerFactory every time the method is called? Or was that included just to show us how you are creating it?

            • 3. Re: Non managed JPA EntityManager is broken.
              Gregory Nikle Apprentice

              Nop, EMF is created once. Its included here for example, but my EM is created every time on each execute at the method begin.


              My method is executed each second in


              synchronized(MyClass.class) {..}





              block, so concurently executions are eliminated.
              I think to check in tomcat activated connections, but don't know how to do it? Is any way to check it, or debug where new connection is created and another isn't closed?


              • 4. Re: Non managed JPA EntityManager is broken.
                Gregory Nikle Apprentice

                Here is My EMF factory class, shared for whole project:


                public class EMController {
                     public EMController() {}
                     
                     private static boolean initialised;
                    private static EntityManagerFactory sguniAdmEMF;
                    private static EntityManagerFactory sguniOneEMF;
                    private static EntityManagerFactory sguniTwoEMF;
                    
                    public static EntityManagerFactory getEntityManagerFactory(int emf) {
                             init();
                        switch (emf) {
                             case (0) :
                                  return sguniAdmEMF;
                             case (1) :
                                  return sguniOneEMF;
                             case (2) :
                                  return sguniTwoEMF;
                        }
                        return null;
                    }
                    
                    public static void init() {
                         if (!initialised) {
                              synchronized (EMController.class) {
                                   sguniAdmEMF = Persistence.createEntityManagerFactory("sguniAdmDatabase");
                                   sguniOneEMF = Persistence.createEntityManagerFactory("sguniOneDatabase");
                                   sguniTwoEMF = Persistence.createEntityManagerFactory("sguniTwoDatabase");
                                   initialised = true;
                              }
                         }
                    }
                    
                    public void destroy() {
                         sguniAdmEMF.close();
                         sguniOneEMF.close();
                         sguniTwoEMF.close();
                    }
                
                    public static EntityManager getEM(int em) throws RuntimeException {
                        return getEntityManagerFactory(em).createEntityManager();
                    }
                }