在其中一个应用程序中,我看到实体管理器是为“每个用户 HttpSession”创建的,而 EntityManagerFactory 只创建了一次。
应用程序中未使用 Spring 或 EJB。 实体管理器缓存在 http session 中,并在 session 失效时关闭。
public EntityManager getEntityManager() {
//Logic to get entity manger from session
//If its present , then return
//Else create new one , entityManagerFactory.createEntityManager();
//Put it into session and then return.
//Returned entity manager is not closed by dao methods ,
//but clear() is invoked
}
- 此设计有哪些潜在问题。
- 如果 10 万用户同时登录到应用程序,我们会用完 jdbc 连接吗?
- 是否每个实体管理器都有与之关联的独立 JDBC 连接?
最佳答案
2 和 3 的答案是肯定的。至于Q1:
您会遇到的一个问题是, session 通常在上次访问后持续 2 到 24 小时(或更长时间)。这意味着您的 session 对象将尝试维持 EntityManager 的打开状态,而 EntityManager 将尝试保持 JDBC 连接处于 Activity 状态并且对自身独占。即使每小时有 50 个用户,您也会因此遇到大量异常和错误 500 页面。
我相信 Serge Ballesta 列出了这种方法会导致的其他主要问题。
一个更安全的解决方案是使用静态 ThreadLocal<EntityManager>
单例访问和 javax.servlet.Filter
在所有带有 try-finally 语句的 URL 上确保 EntityManager 在每个请求上正确关闭。否则,可能发生的任何异常都会使连接悬空并导致其他问题。
关于java - 在 Web 应用程序中为每个 HttpSession 创建 EntityManager 的潜在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805049/