One way to fix this is to inject EntityManagerFactory instead. EntityManagerFactory is guaranteed to be thread-safe. For example:
1 2 3 4 5 6 7 8 9 10 11 12 | public class EMTestServlet extends HttpServlet { //This field injection is thread-safe @PersistenceUnit private EntityManagerFactory emf; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EntityManager em = emf.createEntityManager(); //work with em } } |
Read full article from Improve your life Through Science and Art: JPA: Thread-safety when injecting EntityManager
No comments:
Post a Comment