私のプロジェクトでは、永続性の管理をアプリケーションからコンテナーに切り替えようとしています。私は次の指示に従っています: http://docs.oracle.com/javaee/6/tutorial/doc/gkhrg.html
EntityManager がスレッドセーフではないことについて読んだことがありますが、セットアップが正しいことを確認したいだけです。私の懸念: http://weblogs.java.net/blog/2005/12/19/dont-use-persistencecontext-web-app。
永続コンテキストを生成するクラスがあります。
@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();
    } 
    */
}
次に、DAO を注入する jax-rs リソースがあります。
@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;
    }
}
そして最後にDAO、EntityManager.
@Singleton
public class JpaDao<T, K extends Serializable> implements Dao<T, K> {
    protected Class<T> entityClass;
    @Inject
    @Database
    EntityManager em;
    public JpaDao() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
    }
    public T find(K id) {
        return em.find(entityClass, id);
    }
    ....
}
1. これは、スレッドの安全性と全体的なパフォーマンスの点で適切な設定ですか?
おまけの質問:
には、破棄時にマネージャーを手動で閉じるJpaResourceProducerための代替セットアップがあります。EntityManager
2. コンテナの取り扱いはEntityManager自動的に終了しますか?
Oracle の例には、EntityManagerFactory.
EntityManagerFactory3. CMP を使用している場合、本当に必要ですか?