Note that there are some explanatory texts on larger screens.

plurals
  1. POIs my @produced EntityManager thread safe in a servlet container?
    text
    copied!<p>In a project of mine I'm trying to switch management of my persistence from application to container. I'm following these instructions: <a href="http://docs.oracle.com/javaee/6/tutorial/doc/gkhrg.html" rel="nofollow">http://docs.oracle.com/javaee/6/tutorial/doc/gkhrg.html</a></p> <p>I've read about EntityManager not being thread safe and just want to make sure that my setup is correct. My concern: <a href="http://weblogs.java.net/blog/2005/12/19/dont-use-persistencecontext-web-app" rel="nofollow">http://weblogs.java.net/blog/2005/12/19/dont-use-persistencecontext-web-app</a>. </p> <p>I have a class that produces a persistence context.</p> <pre><code>@Singleton public class JpaResourceProducer { //The "pu" unit is defined with transaction-type="JTA" @Produces @PersistenceUnit(unitName = "pu") @Database EntityManagerFactory databasePersistenceUnit; @Produces @PersistenceContext(unitName = "pu") @Database EntityManager databaseEntityManager; /* Alternative @PersistenceContext(unitName = "pu") private EntityManager em; @Produces @UserDatabase public EntityManager create() { return em; } public void close(@Disposes @Database EntityManager em) { em.close(); } */ } </code></pre> <p>Then I have a jax-rs resource that injects a DAO.</p> <pre><code>@RequestScoped @Path("/endpoint") public class MyResource { @Inject private Dao dao; @GET @Produces({MediaType.APPLICATION_JSON}) public Converter get() { MyEntity entity = dao.find(1); Converter converter = new Converter(entity); return converter; } } </code></pre> <p>And finally a <code>DAO</code> where I inject the <code>EntityManager</code>.</p> <pre><code>@Singleton public class JpaDao&lt;T, K extends Serializable&gt; implements Dao&lt;T, K&gt; { protected Class&lt;T&gt; entityClass; @Inject @Database EntityManager em; public JpaDao() { ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); this.entityClass = (Class&lt;T&gt;) genericSuperclass.getActualTypeArguments()[0]; } public T find(K id) { return em.find(entityClass, id); } .... } </code></pre> <p><em><strong>1. Is this a good setup in terms of thread safety and overall performance?</em></strong></p> <hr> <p>Bonus questions:</p> <p>In the <code>JpaResourceProducer</code> I have an alternative setup for the <code>EntityManager</code> where I manually close the manager on dispose. </p> <p><em><strong>2. Is the container handling close of my <code>EntityManager</code> automatically?</em></strong></p> <p>The example from Oracle includes an <code>EntityManagerFactory</code>. </p> <p><em><strong>3. Do I really need an <code>EntityManagerFactory</code> when I'm using CMP?</em></strong></p>
 

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