Valter Silva
Valter Silva

Reputation: 16656

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

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:

enter image description here

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);
    }
    // .. 

Upvotes: 9

Views: 38601

Answers (3)

BalusC
BalusC

Reputation: 1108722

From your stacktrace:

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

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


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:

Upvotes: 9

Eddie Martinez
Eddie Martinez

Reputation: 13910

If you are using Glassfish server, restarting the server did it for me.

Upvotes: 23

gammay
gammay

Reputation: 6215

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.

Steps:

  1. Start database from command-line/outside eclipse
  2. Start server (domain) in eclipse
  3. Deploy application with @PersistenceContext injection --> works

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:

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.


EDIT The 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

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

Upvotes: 3

Related Questions