Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityManager ThreadLocal pattern with JPA in JSE
    primarykey
    data
    text
    <p>I'm developing a simple "Book Store" project using Struts 1.3 + JPA (with Hibernate as persistence provider). I cannot switch to Spring or any other more sophisticated development environment (e.g., Jboss) and I cannot use any Hibernate-specific technique (e.g., <code>Session</code> class). </p> <p>Given the fact that I'm in a JSE Environment, I need to explicitly manage the whole EntityManager's lifecycle.</p> <p>The <code>Book</code> entity is defined as follows:</p> <pre><code>@Entity public class Book { @Id private String isbn; private String title; private Date publishDate; // Getters and Setters } </code></pre> <p>I defined three <code>Action</code> classes, which are responsible, respectively, of retrieving all book instances, retrieving a single book instance by its ISBN and merging a detached book into the DB.</p> <p>In order to increase separation of concerns between business-logic code and data-access code, I introduced a simple <code>BookDAO</code> object, which is charge of executing CRUD operations. Ideally, all data-access related calls should be delegated to the persistence layer. For example, the <code>ListBookAction</code> is defined as follows:</p> <pre><code>public class ListBookAction extends Action { private BookDAO dao = new BookDAO(); @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Retrieve all the books List&lt;Book&gt; books = dao.findAll(); // Save the result set request.setAttribute("books", books); // Forward to the view return mapping.findForward("booklist"); } } </code></pre> <p>The BookDAO object needs to access an <code>EntityManager</code> instance in order to do any operation. Given that <code>EntityManger</code> is not thread-safe, I introduced an helper class named <code>BookUnitSession</code> which encapsulates EntityManager within a <code>ThreadLocal</code> variable:</p> <pre><code>public class BookUnitSession { private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("BookStoreUnit"); private static final ThreadLocal&lt;EntityManager&gt; tl = new ThreadLocal&lt;EntityManager&gt;(); public static EntityManager getEntityManager() { EntityManager em = tl.get(); if (em == null) { em = emf.createEntityManager(); tl.set(em); } return em; } } </code></pre> <p>Everything seems to work, but I still have some concerns. Namely:</p> <ol> <li>Is this solution the best thing to do? which is the best practice in this case?</li> <li>I still need to explictly close both the EntityManager and the EntityManagerFactory. How can I do that?</li> </ol> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload