简体   繁体   English

创建entitymanager的最佳实践

[英]best practise of creating entitymanager

I am using resource local entitymanager with JPA 1 . 我正在使用资源本地实体管理器与JPA 1。 What is the best practice for creating EntityManager that is 创建EntityManager的最佳实践是什么?

  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.) 为每个线程创建一个实体管理器并将其存储为ThreadLocal变量并使用它(a-应该关闭entitymanager b-会话范围bean中存储的任何实体将是一个分离的实体,因为entitymanager其持久化上下文中的哪个实体关闭或属于另一个线程现在。)

  3. Store entitymanager in a session bean and use same entitymanager during session(Sessions unawere of each others ) 将实体管理器存储在会话bean中并在会话期间使用相同的实体管理器(Sessions unawere of each others)

You should be injecting the EntityManager using the @PersistenceContext annotation. 您应该使用@PersistenceContext注释注入EntityManager Don't worry about where it comes from — that's dependency injection at work. 不要担心它来自哪里 - 这是工作中的依赖注入。

我按请求创建EntityManagers并在ServletRequestListener关闭它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM