开发者

best practise of creating entitymanager

开发者 https://www.devze.com 2023-03-14 00:29 出处:网络
I am using resource local entitymanager with JPA 1 . What is the best practice for creating EntityManager that is

I am using resource local entitymanager with JPA 1 . What is the best practice for creating EntityManager that is

  1. Create do job close entitymanager everytime.(Entitymanager开发者_运维问答 not heavy weight but still boring )

  2. Create a entitymanager for every thread and store it as ThreadLocal variable and use it (a-where should i close entitymanager b-Any entitystored in a session scoped bean will be a detached entity because entitymanager which entity in its persistense context closed or belong another thread now.)

  3. Store entitymanager in a session bean and use same entitymanager during session(Sessions unawere of each others )


You should be injecting the EntityManager using the @PersistenceContext annotation. Don't worry about where it comes from — that's dependency injection at work.


I create EntityManagers per request and close them in a ServletRequestListener.

0

精彩评论

暂无评论...
验证码 换一张
取 消