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 void
closeThreadEntityManager()
static void
closeThreadEntityManager(String persistenceUnit)
void
destroy()
static javax.persistence.EntityManagerFactory
getEntityManagerFactory()
static javax.persistence.EntityManagerFactory
getEntityManagerFactory(String persistenceUnit)
javax.servlet.ServletConfig
getServletConfig()
String
getServletInfo()
static javax.persistence.EntityManager
getThreadEntityManager()
Returns an EntityManager for the default persistence unit.static javax.persistence.EntityManager
getThreadEntityManager(String persistenceUnit)
Returns an EntityManager unique for the current thread for the specified persistence unit.void
init(javax.servlet.ServletConfig config)
void
service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
static void
setDefaultPersistenceUnit(String pu)
-
-
-
Method Detail
-
getServletInfo
public String getServletInfo()
- Specified by:
getServletInfo
in interfacejavax.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 interfacejavax.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 interfacejavax.servlet.Servlet
-
service
public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws javax.servlet.ServletException, IOException
- Specified by:
service
in interfacejavax.servlet.Servlet
- Throws:
javax.servlet.ServletException
IOException
-
destroy
public void destroy()
- Specified by:
destroy
in interfacejavax.servlet.Servlet
-
-