且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.lang.IllegalStateException:试图对关闭的 EntityManagerFactory 执行操作

更新时间:2022-01-23 21:52:57

来自您的堆栈跟踪:

Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.

Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.

您的 EJB 代码看起来不错.这更有可能是 Glassfish 错误或配置错误:

Your EJB code looks all right. This is more likely a Glassfish bug or misconfiguration:

这只能意味着您正在手动管理 EntityManagerFactory(和 EntityManager),而不是让容器仅通过 @ 来完成这项工作持久化上下文.

This can only mean that you're manually managing the EntityManagerFactory (and EntityManager) instead of letting the container do the job by just @PersistenceContext.

EntityManagerFactory 旨在仅在 webapp 启动时创建一次,在整个 webapp 生命周期中重复使用,并在 webapp 关闭时关闭.它不应该在某个地方中途关闭或被序列化并在下一个重启周期中重复使用.

The EntityManagerFactory is intented to be created only once on webapp's startup, reused throughout the entire webapp's lifetime and closed on webapp's shutdown. It should not be closed somewhere halfway or be serialized and reused for a next restart cycle.

由于您显然是针对 Glassfish,我强烈建议您不要自己管理它,而是让 Glassfish 来完成这项工作.您最终会得到更简单的 EJB 代码,而无需手动管理事务.

Since you're apparently targeting Glassfish, I would strongly recommend to not manage it yourself, but letting Glassfish do the job. You end up with much simpler EJB code without all the hassle to manage transactions manually.