title: Attempting to execute an operation on a closed EntityManager (when invoking setParameter() function on a named query)
body: <p>I have Netbeans_8.0.2 to development my project using Glassfish_4.1. In the stateless bean, I initialize a named query in a method that has a @postconstruct annotation. But it throws an IllegalStateException when I try to invoke setParameter() on the named query.</p>
  
  <p><strong>Entity Class:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "ORDERS")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "OrdersEntity.findByClientNum", query = "SELECT o FROM OrdersEntity o WHERE o.orderClientNum = :orderClientNum")})
  
  public class OrdersEntity implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "ORDER_SYS_NUM")
      private Integer orderSysNum;
      @Column(name = "ORDER_CLIENT_NUM")
      private String OrderClientNum;
  
      public OrdersEntity() {
      }
  
      public OrdersEntity(Integer sysNum) {
          this.dtOrderSysNum = sysNum;
      }
  
  //setters &amp; getters
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Stateless Bean:</strong></p>
  
  <pre><code>@Stateless
  public class OrderServices implements OrderServicesRemote {
      @PersistenceContext(unitName = "OrderServicesPU")
      private EntityManager em;
  
      private TypedQuery&lt;OrdersEntity&gt;  queryFindOrderByOrderClientNum;
  
      @PostConstruct
      private void initializeBean() {
          queryFindOrderByOrderClientNum = em.createNamedQuery("OrdersEntity.findByClientNum", OrdersEntity.class);
          //comment 1: the following line of code works
          //OrdersEntity oe = queryFindOrderByOrderClientNum.setParameter("OrderClientNum", "OR123").getSingleResult();        
      }
  
  
      private OrdersEntity retrieveOrderEntity(String orderClientNum) {
          //comment 2: the following line of code causes the issue
              queryFindOrderByOrderClientNum.setParameter("OrderClientNum",   orderClientNum);
  
          OrdersEntity oe;
          try {
              oe = queryFindOrderByOrderClientNum.getSingleResult();
          } catch (NoResultException nre) {
              throw new OrderNotFoundException();
          } catch (NonUniqueResultException nure) {
             throw new OrderNotFoundException();
          }
  
          return oe;
      }
  }
  </code></pre>
  
  <p><strong>persistence.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
    &lt;persistence-unit name="RewardsManagerServicesPU" transaction-type="JTA"&gt;
      &lt;jta-data-source&gt;jdbc/Rewards_Pool&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="eclipselink.jdbc.batch-writing" value="JDBC"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Details:</strong></p>
  
  <ol>
  <li>I ran the program in debugger and figured out that the line of code below comment 2 causes the problem</li>
  <li>Entity Manager is functional. I have a couple of other functions in this statless bean, they all work well with it.</li>
  <li>As shows in comment 1, if I invoke setParameter() function on the named query immediately after it's initialized, it works.</li>
  </ol>
  
  <p><strong>Stacktrace:</strong></p>
  
  <pre><code>Warning: A system exception occurred during an invocation on EJB OrderServices, method: public OrderServices.retrieveOrderVO(java.lang.String) throws OrderNotFoundException
  Warning: javax.ejb.EJBException
  at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
  at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698)
  at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
  at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
  at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
  at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
  at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
  at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
  at com.sun.proxy.$Proxy301.retrieveOrderVO(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
  at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
  at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
  at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
  at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
  at com.sun.corba.ee.impl.protocol.SharedCDRClientRequestDispatcherImpl.marshalingComplete(SharedCDRClientRequestDispatcherImpl.java:119)
  at com.sun.corba.ee.impl.protocol.ClientDelegateImpl.invoke(ClientDelegateImpl.java:258)
  at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:198)
  at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
  at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManager.
  at org.eclipse.persistence.internal.jpa.EntityManagerImpl.verifyOpen(EntityManagerImpl.java:1913)
  at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:592)
  at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1)
  at com.sun.enterprise.container.common.impl.QueryWrapper.setParameter(QueryWrapper.java:284)
  at com.sun.enterprise.container.common.impl.TypedQueryWrapper.setParameter(TypedQueryWrapper.java:122)
  at OrderServices.retrieveOrderEntity(OrderServices.java:394)
  at OrderServices.retrieveOrderVO(OrderServices.java:656)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
  at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
  at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
  at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
  at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
  at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
  at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
  at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
  at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
  at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
  at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
  at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
  at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
  at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
  at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
  at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
  at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
  ... 50 more
  </code></pre>