Package nl.b3p.commons.jpa
Class JpaUtilServlet
- java.lang.Object
-
- nl.b3p.commons.jpa.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 Summary
Constructors Constructor Description JpaUtilServlet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidcloseThreadEntityManager()static voidcloseThreadEntityManager(String persistenceUnit)voiddestroy()static javax.persistence.EntityManagerFactorygetEntityManagerFactory()static javax.persistence.EntityManagerFactorygetEntityManagerFactory(String persistenceUnit)javax.servlet.ServletConfiggetServletConfig()StringgetServletInfo()static javax.persistence.EntityManagergetThreadEntityManager()Returns an EntityManager for the default persistence unit.static javax.persistence.EntityManagergetThreadEntityManager(String persistenceUnit)Returns an EntityManager unique for the current thread for the specified persistence unit.voidinit(javax.servlet.ServletConfig config)voidservice(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)static voidsetDefaultPersistenceUnit(String pu)
-
-
-
Method Detail
-
getServletInfo
public String getServletInfo()
- Specified by:
getServletInfoin interfacejavax.servlet.Servlet
-
setDefaultPersistenceUnit
public static void setDefaultPersistenceUnit(String pu)
-
init
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException- Specified by:
initin interfacejavax.servlet.Servlet- Throws:
javax.servlet.ServletException
-
getThreadEntityManager
public static javax.persistence.EntityManager getThreadEntityManager() throws IllegalArgumentExceptionReturns 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:
getServletConfigin interfacejavax.servlet.Servlet
-
service
public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws javax.servlet.ServletException, IOException- Specified by:
servicein interfacejavax.servlet.Servlet- Throws:
javax.servlet.ServletExceptionIOException
-
destroy
public void destroy()
- Specified by:
destroyin interfacejavax.servlet.Servlet
-
-