且构网

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

java.lang.IllegalStateException:试图在一个封闭的EntityManagerFactory执行操作

更新时间:2022-10-14 20:07:19

从您的堆栈跟踪:

  

产生的原因:java.lang.IllegalStateException:试图在一个封闭的EntityManagerFactory执行操作

这只能意味着你手动管理(与的EntityManager )而不是的EntityManagerFactory 让容器中只是 @PersistenceContext

做的工作

的EntityManagerFactory 是intented要在Web应用程序的启动创建只有一次,在整个Web应用程序的生命周期重用和Web应用的停机关闭。它不应该被某处中途封闭或序列化并再用于下一个重启循环

既然你显然是针对Glassfish的,我会强烈建议你自己来管理它,而是让Glassfish的做的工作。你最终要简单得多EJB code没有所有的麻烦手动管理的事务。

参见:

  • Best实践中获得的EntityManagerFactory 行使>

更新:您的EJB code看上去一切正常。这更可能是Glassfish的错误或配置错误:

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.

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.

I receive this message:

serverError: class 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:

@Stateless(mappedName = "logEAO")
@LocalBean
public class LogEAO {
    @PersistenceContext
    private EntityManager em;

    public LogEAO() {}


    public Log find(int id) {
        return em.find(Log.class, id);
    }
    // .. 

From your stacktrace:

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

This can only mean that you're manually managing the EntityManagerFactory (and EntityManager) instead of letting the container do the job by just @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:


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