MySQL java.lang.IllegalStateException:试图对关闭的 EntityManagerFactory 执行操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9947293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory
提问by Valter Silva
I have Eclipse Indigo that I use to develop a JSF 2 project using Glassfish 3 Open Source, this in my computer then in my EC2 instance too, in Amazon AWS, for both Glassfish's I created a JDBC Connection Pool to use with my JPA Entity Manager.
我有 Eclipse Indigo,用于使用 Glassfish 3 Open Source 开发 JSF 2 项目,这在我的计算机中,然后在我的 EC2 实例中,在 Amazon AWS 中,对于两个 Glassfish,我都创建了一个 JDBC 连接池以与我的 JPA 实体管理器一起使用.
Locally my project works pretty fine, but when I deploy the project and try to execute the same form, which gets some values from the database that is running in the same instance EC2 that is running the GlassFIsh tough.
在本地,我的项目运行良好,但是当我部署该项目并尝试执行相同的表单时,该表单从运行 GlassFIsh 的同一实例 EC2 中运行的数据库中获取一些值。
I receive this message:
我收到这条消息:
serverError: class javax.faces.el.EvaluationException
服务器错误:类 javax.faces.el.EvaluationException
I'm searching about it, but I don't found anything so far.
我正在寻找它,但到目前为止我没有找到任何东西。
There's some configuration that I shoud do for it works ?
我应该为它做一些配置吗?
EDIT:
编辑:
Here's the stacktrace:
这是堆栈跟踪:
javax.faces.FacesException: #{reportc.generateReport}: javax.ejb.EJBException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:636)
Caused by: javax.faces.el.EvaluationException: javax.ejb.EJBException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 32 more
Caused by: javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at $Proxy225.listLastMinutes(Unknown Source)
at com.brainset.eao.__EJB31_Generated__LogEAO__Intf____Bean__.listLastMinutes(Unknown Source)
at com.brainset.controller.ReportControl.getLog(ReportControl.java:234)
at com.brainset.controller.ReportControl.generateReport(ReportControl.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 33 more
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.verifyOpen(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:276)
EDIT 2:This is how I'm using the EntityManager:
编辑 2:这就是我使用 EntityManager 的方式:
@Stateless(mappedName = "logEAO")
@LocalBean
public class LogEAO {
@PersistenceContext
private EntityManager em;
public LogEAO() {}
public Log find(int id) {
return em.find(Log.class, id);
}
// ..
采纳答案by BalusC
From your stacktrace:
从您的堆栈跟踪:
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
引起:java.lang.IllegalStateException:试图在关闭的 EntityManagerFactory 上执行操作。
Your EJB code looks all right. This is more likely a Glassfish bug or misconfiguration:
您的 EJB 代码看起来不错。这更可能是 Glassfish 错误或配置错误:
- Make sure that your JTA datasource configuration is right
- Make sure that you're using a Glassfish version which doesn't have a redeployment bug
- This redeployment bug is fixed in 3.1.2 and 4.0
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
(和EntityManager
)而不是让容器仅通过@PersistenceContext
.
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.
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.
See also:
该EntityManagerFactory
被intented要在Web应用程序的启动创建一次,重复使用在整个Web应用程序的生命周期以及Web应用程序的关闭关闭。它不应该在某个地方中途关闭,也不应该被序列化并在下一个重启周期中重复使用。
由于您显然是针对 Glassfish,我强烈建议您不要自己管理它,而是让 Glassfish 来完成这项工作。您最终会得到更简单的 EJB 代码,而无需手动管理事务。
也可以看看:
回答by Eduardo Dennis
If you are using Glassfish server, restarting the server did it for me.
如果您使用的是 Glassfish 服务器,则重新启动服务器对我来说就完成了。
回答by gammay
I had this problem and found a solution.
我遇到了这个问题并找到了解决方案。
The problem is that eclipse (with the glassfish plugin) starts/stops only the domain and not the database.
问题是 eclipse(使用 glassfish 插件)仅启动/停止域而不是数据库。
Steps:
脚步:
- Start database from command-line/outside eclipse
- Start server (domain) in eclipse
- Deploy application with @PersistenceContext injection --> works
- 从命令行/外部 eclipse 启动数据库
- 在eclipse中启动服务器(域)
- 使用 @PersistenceContext 注入部署应用程序 --> 有效
Details:
细节:
I exited eclipse - this stopped the glassfish server. Then shutdown the machine - stopped the database. On restart, opened eclipse, started server and tried deploying the same app. Failed with error:
我退出了 eclipse - 这停止了 glassfish 服务器。然后关闭机器 - 停止数据库。重新启动时,打开 eclipse,启动服务器并尝试部署相同的应用程序。失败,错误:
Deployment Error for module xxx
Error occurred during deployment: Exception while preparing the app :
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection.
Cause: Connection could not be allocated because: java.net.ConnectException :
Error connecting to server localhost on port 1527 with message Connection refused
The reason being database was not started. So started database from command-line. Got the error
原因是数据库没有启动。所以从命令行启动数据库。得到错误
java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory
Then followed the steps above and it worked.
然后按照上面的步骤操作,就成功了。
EDITThe above steps fixes the problem sometimes, but it also seems a known issuein GF 3.0/3.1: http://java.net/jira/browse/GLASSFISH-17295
编辑上述步骤有时可以解决问题,但它似乎也是 GF 3.0/3.1 中的一个已知问题:http://java.net/jira/browse/GLASSFISH-17295
Another option is to redeploy the application from admin console as mentioned here: http://richardchesterwood.blogspot.in/2011/09/javaee-course-ships-with-copy-of-javaee.html
另一种选择是从管理控制台重新部署应用程序,如下所述:http: //richardchesterwood.blogspot.in/2011/09/javaee-course-ships-with-copy-of-javaee.html