Note: [Mar 23, 2017] This website was migrated to a new platform recently. Some linked content may not be accessible until all the links are configured properly,

Best way to use JPA in web-tier

Submitted by Kamal Wickramanayake on June 29, 2010 - 17:51

JPA (Java Persistence Architecture) greatly simplifies the persistence implementation of Java applications. In web-tier, the following forces/constraints exist:

  1. Either container managed EntityManagers or application managed EntityManagers should be used.
  2. Transaction demarcation should either use JTA transaction management (via UserTransaction object) or EntityManager methods.
  3. A single EntityManager instance is preferred to be used to serve an entire request.
  4. If you use the DAO (Data Access Object) pattern, typical DAO classes cannot use container managed EntityManagers since their life-cycle is not managed by the Servlet container. Resource injection via annotations is only possible in container managed resources (Servlets, Listeners, or JSF managed beans).

These forces lead to different implementation possibilities. Interestingly, when the combination A is better than B from one perspective, A is worse than B from another perspective. These perspectives include the amount of coding required, amount of object references to be shared, number of objects to be created and discarded while processing a single request, thread safety, ease in managing transactions and making sure EntityManagers are properly closed at the end of request processing.

Which combination is the best then? The best solution that I have found is not indicated in the above list. The best is to use the Spring framework. Another is not to depend on the Spring framework, but depend on JSF.

Spring Framework Approach

Spring framework takes away all the negative issues that you come across when you attempt to use a certain combination of the above. The Spring framework solution is to use EntityManagers in DAO classes that are injected using annotations. You configure DAO classes as Spring managed beans and hence Spring can inject EntityManagers to them via annotations. You will also use Spring transaction support which goes with Spring AOP (Aspect Oriented Programming).

Something notable here (Spring approach) is the Spring provided thread safe EntityManager that you inject into the DAOs so that you can use these DAO instances without discarding them. Neither you really need a DAO factory. You can just initialize the DAOs, inject them into the business code. Let the same DAO instances be reused by business code. No need to use EntityManagerFactory at all. Thread safety is still accomplished. You can use JTA transactions or depend on a resource local transaction manager. I have detailed this solution here.

In combination with the Spring framework, you may use any other web-tier framework like JSF, Struts, Spring MVC and so on. Note that you can go for a Spring + JSF solution.

JSF Only Approach

The other approach I mentioned is not to depend on Spring for persistence, but to use JSF. The strategy is as follows:

  1. Define DAOs as JSF managed beans in request scope.
  2. Use @PersistenceContext annotation in DAOs to inject EntityManagers (i.e. use container managed EntityManagers). Container creates and discards the EntityManagers gracefully in coordination with JTA transactions.
  3. Use JTA transaction demarcation.
  4. Use a DAO factory that does not cache DAO instances. Instead, go about returning DAO instances by getting them from JSF managed environment each time a DAO is requested (from the DAO factory). Define the DAO factory as a JSF managed been in application scope.
  5. Pass the DAO factory to the business classes. Let the business classes maintain the reference of the DAO factory.
  6. Within a business method, start a transaction, get new DAOs from the DAO factory, use them, committee the transaction, forget about the DAO instances.

Let's look at some important code segments.

A typical DAO (uses an injected EntityManager):

public class JSFJPAAccountDAO implements AccountDAO {

    private EntityManager em;
    public void delete(Account account) {

    public Account find(int pk) {
        return em.find(Account.class, pk);

    public Account save(Account account) {
        return account;

    public void update(Account account) {



Here's how it is defined as a request scoped managed bean in JSF:



Here's the DAO factory:

 * Define this as a managed bean in JSF in the application scope.
 * Inject to any other managed bean (in any scope) as required.
public class JSFJPADAOFactory implements DAOFactory {

     * These two properties should be set in the faces configuration file.
    private String accountDAOBeanName;
    private String customerDAOBeanName;
    public void setAccountDAOBeanName(String accountDAOBeanName) {
        this.accountDAOBeanName = accountDAOBeanName;

    public void setCustomerDAOBeanName(String customerDAOBeanName) {
        this.customerDAOBeanName = customerDAOBeanName;

     * Utility function to locate the JSF managed beans.
     * All getXXXDAO() methods should depend on it.
     * JSF will return a new bean only if a bean has not been created for the same
     * request (since all the DAO beans are request scoped). Otherwise, the same
     * managed bean instance will be returned by JSF.
    private Object getDAO(String name) {
        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        ValueExpression ve = context.getApplication()
                    "#{"+ name + "}",

        return ve.getValue(elContext);
    public AccountDAO getAccountDAO() {
        return ((AccountDAO) getDAO(accountDAOBeanName));

    public CustomerDAO getCustomerDAO() {
        return ((CustomerDAO) getDAO(customerDAOBeanName));

Here's how it is defined and configured to be an application scoped managed bean in JSF:


Here's the persistence unit configuration (Note that JTA is used. set to "create" will drop tables and recreate them when the application is deployed.):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns=""
    <persistence-unit name="BankPU" transaction-type="JTA">


      <!-- Auto-detect entity classes -->
      <property name="hibernate.archive.autodetection" value="class, hbm"/>

      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>

      <property name="hibernate.transaction.manager_lookup_class"

      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

      <property name="" value="create"/>


Here's how a typical business method is implemented in a class that captures business logic:

public class BankService {

    private UserTransaction utx;
    private DAOFactory daoFactory;
    public void setDaoFactory(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;

    public Account createAccount(Customer customer, Account account) throws BankServiceException {
        try {
            // Make sure to access the DAOs within the JTA transaction
            CustomerDAO customerDAO = daoFactory.getCustomerDAO();
            AccountDAO accountDAO = daoFactory.getAccountDAO();
            // Create a new customer
            // Create a new Account

        } catch (Exception ex) {
            try {
            } catch (Exception exe) {
                throw new BankServiceException("Rollback failed: " + exe.getMessage());
            throw new BankServiceException("Commit failed: " + ex.getMessage());
        return account;


See the attachment section to download the sample application. It is an Eclipse project deployed in JBoss 5.1.0 and uses MySQL and Hibernate (as the JPA provider).

JBoss data source configuration file is also available for downloading. You will edit it and copy it to the JBoss hot deployment directory (for example,  $JBOSS_HOME/server/default/deploy/). You also need the MySQL JDBC driver jar file copied to a suitable folder of JBoss (for example, $JBOSS_HOME/server/default/lib/). If you don't have the MySQL JDBC driver, download it from the MySQL web site.


Since different possibilities exist for web-tier persistence and transaction management, there is a quest for the best strategy that works well in most of the cases. Even I myself have used strategies that require front filters in intercepting requests and responses to assist in implementing persistence and transaction management in Java applications. The best approach is to use the layered architecture properly where different concerns are fully handled within the appropriate layers.

Spring is surely a winner in assisting in implementing web-tier persistence among many other capabilities it has. JTA or resource local transactions can be used with Spring. Another approach is to depend on container managed EntityManagers with JSF and JTA. In both cases it is relatively easy to make sure that a single EntityManager instance is used to serve an entire HTTP request. This includes requests that may use multiple DAOs. Thread safety is retained. EntityManagers are closed properly. Method calls from one layer to another that bypass intermediate layers are not used and hence the separation of concerns and the proper usage of layers architectural pattern are honored.