Class JpaUtilServlet

  • All Implemented Interfaces:
    javax.servlet.Servlet

    public class JpaUtilServlet
    extends Object
    implements javax.servlet.Servlet
    Servlet die persistence units kan initialiaseren bij het opstarten van de webapp en waaraan de code mbv een static methode een EntityManager kan opvragen. Init parameters: initialize-persistence-units: welke persistence units bij het initialiseren van de webapp moeten worden aangemaakt. Indien er een fout optreedt zal de webapp niet succesvol geinitialiseerd worden, dus komen fouten in de persistence unit meteen boven water. Meerdere persistence units scheiden door ',' default-persistence-unit: van welke persistence unit de entity manager moet worden teruggegeven bij aanroep van getThreadEntityManager() (dus zonder persistenceUnit parameter). Indien bij initialize-persistence-unit een enkele persistence unit wordt genoemd wordt dit automatisch de default persistence unit. Voorbeeld web.xml: JpaUtilServlet nl.b3p.commons.jpa.JpaUtilServlet initialize-persistence-units mijnPU 1
    Author:
    Matthijs
    • Constructor Detail

      • JpaUtilServlet

        public JpaUtilServlet()
    • Method Detail

      • getServletInfo

        public String getServletInfo()
        Specified by:
        getServletInfo in interface javax.servlet.Servlet
      • setDefaultPersistenceUnit

        public static void setDefaultPersistenceUnit​(String pu)
      • init

        public void init​(javax.servlet.ServletConfig config)
                  throws javax.servlet.ServletException
        Specified by:
        init in interface javax.servlet.Servlet
        Throws:
        javax.servlet.ServletException
      • getThreadEntityManager

        public static javax.persistence.EntityManager getThreadEntityManager()
                                                                      throws IllegalArgumentException
        Returns an EntityManager for the default persistence unit. The default persistence unit name is configured using the "default-persistence-unit" init parameter or automatically if there is only one persistence unit configured to initialize to that unit. If there is no default persistence unit configured, this method throws an IllegalArgumentException. It's important that when the unit-of-work of the current thread is completed, the entity manager is closed using closeThreadEntityManager()!
        Returns:
        an EntityManager for the default persistence unit
        Throws:
        IllegalArgumentException
        See Also:
        getThreadEntityManager(java.lang.String)
      • getThreadEntityManager

        public static javax.persistence.EntityManager getThreadEntityManager​(String persistenceUnit)
        Returns an EntityManager unique for the current thread for the specified persistence unit. If the EntityManagerFactory for the persistence unit hasn't been created, it is created first. This may cause an exception only at the stage the EntityManager is first requested. To see exceptions at application start, use the "initialize-persistence-units" init parameter. If the method is called for the first time in the current thread (or a previously created EntityManager was closed), a new EntityManger will be created. If an EntityManager was already created, that instance is returned. It's important that when the unit-of-work of the current thread is completed, the entity manager is closed using closeThreadEntityManager()!
        Returns:
        an EntityManager for the specified persistence unit
      • getEntityManagerFactory

        public static javax.persistence.EntityManagerFactory getEntityManagerFactory()
      • getEntityManagerFactory

        public static javax.persistence.EntityManagerFactory getEntityManagerFactory​(String persistenceUnit)
      • closeThreadEntityManager

        public static void closeThreadEntityManager()
      • closeThreadEntityManager

        public static void closeThreadEntityManager​(String persistenceUnit)
      • getServletConfig

        public javax.servlet.ServletConfig getServletConfig()
        Specified by:
        getServletConfig in interface javax.servlet.Servlet
      • service

        public void service​(javax.servlet.ServletRequest req,
                            javax.servlet.ServletResponse res)
                     throws javax.servlet.ServletException,
                            IOException
        Specified by:
        service in interface javax.servlet.Servlet
        Throws:
        javax.servlet.ServletException
        IOException
      • destroy

        public void destroy()
        Specified by:
        destroy in interface javax.servlet.Servlet