1 Reply Latest reply on Feb 14, 2007 5:54 PM by Tim Nevaker

    em.refresh() throwing error in JBoss AS 4.0.5.GA

    Tim Nevaker Newbie

      I am using a stateful EJB to manage user sessions on a web app, and manage the state of an Employee entity EJB that contains the user's information. The user information is returned as a bean by calling the getUserBean() method of the stateful EJB, which first attempts to use the persistence manager refresh() method, then creates and returns a bean object with the updated data.

      This code was working in JBoss 4.0.4.GA, but I am now deploying to JBoss 4.0.5.GA and it is now throwing a "java.lang.IllegalArgumentException: Entity not managed" error. The error is being caused by the call to the refresh() method of the EntityManager. Does anybody know why this code no longer runs in 4.0.5.GA, or what needs to be changed to get it running in the latest version?

      Here is the relevant code:

      UserSessionFacadeBean.java - I have traced the error to the em.refresh() call bolded below:

      @Stateful(name="UserSessionFacade")
      public class UserSessionFacadeBean implements UserSessionFacadeRemote,
       UserSessionFacadeLocal {
       @PersistenceContext(unitName="warehaus")
       protected EntityManager em;
      
       private Employee _user;
       private boolean _initialized = false;
      
       public UserSessionFacadeBean() {
       }
      
       public void initializeUser(String userId) {
       Collection<Employee> rsEmployee =
       em.createNamedQuery("findEmployeeByUserId").setParameter("userId", userId).getResultList();
      
       if (rsEmployee.size() == 1) {
       Iterator<Employee> iter = rsEmployee.iterator();
       if (iter.hasNext()) {
       this._user = iter.next();
       _initialized = true;
       }
       }
       }
      
       public UserBean getUserBean() {
       UserBean userBean = new UserBean();
      
       if (_initialized) {
       // Refresh the Employee entity
       em.refresh(_user);
      
       userBean.setEmployeeId(_user.getEmployeeId());
       userBean.setUserId(_user.getUserId());
       userBean.setFullName(_user.getFirstName() + " " + _user.getLastName());
       userBean.setFirstName(_user.getFirstName());
       userBean.setMiddleInitial(_user.getMiddleInitial());
       userBean.setLastName(_user.getLastName());
       userBean.setEmailAddress(_user.getEmailAddress());
       userBean.setAkoEmailAddress(_user.getAkoEmailAddress());
       userBean.setRecStatusFlag(_user.getRecStatusFlag());
       userBean.setRoles(_user.getRoles());
       userBean.setPermissions(_user.getPermissions());
       userBean.setApplications(getUserApplications());
       }
      
       return userBean;
       }
      }


      Employee.java
      @Entity
      @NamedQueries({
       @NamedQuery(name="findAllEmployee",
       query="select object(o) from Employee o"),
       @NamedQuery(name="findEmployeeByUserId",
       query="select object(o) from Employee o where upper(o.userId) = upper(:userId)")
      })
      @Table(name="EI")
      public class Employee implements Serializable {
       // class code...
      }


      persistence.xml
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
       version="1.0">
       <persistence-unit name="warehaus" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/warehausDS</jta-data-source>
       <class>mypackage.Employee</class>
       ... other class entries ...
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
       </properties>
       </persistence-unit>
      </persistence>


      And here is the stack trace on the error that is returned.
      javax.ejb.EJBException: java.lang.IllegalArgumentException: Entity not managed
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
       at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:203)
       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
       at $Proxy102.getUserBean(Unknown Source)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at coldfusion.runtime.StructBean.invoke(StructBean.java:326)
       at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:1650)
       at cfusertest2ecfm474474188.runPage(C:\jboss-4.0.5.GA\server\default\.\deploy\cfusion.ear\cfusion.war\earltest\cftest\usertest.cfm:110)
       at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:147)
       at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:357)
       at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:62)
       at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:107)
       at coldfusion.filter.PathFilter.invoke(PathFilter.java:80)
       at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:47)
       at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
       at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:35)
       at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:43)
       at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
       at coldfusion.CfmServlet.service(CfmServlet.java:105)
       at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: java.lang.IllegalArgumentException: Entity not managed
       at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:260)
       at org.jboss.ejb3.entity.TransactionScopedEntityManager.refresh(TransactionScopedEntityManager.java:193)
       at mil.army.arl.support.integration.earl.ejb.UserSessionFacadeBean.getUserBean(UserSessionFacadeBean.java:72)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       ... 54 more


        • 1. Re: em.refresh() throwing error in JBoss AS 4.0.5.GA
          Tim Nevaker Newbie

          I believe I may have found my problem. By changing the PersistenceContext in my SFSB from this:

          @PersistenceContext(unitName="warehaus")
           protected EntityManager em;


          to this:

          @PersistenceContext(unitName="warehaus", type=PersistenceContextType.EXTENDED)
           protected EntityManager em;


          it now appears to be working without throwing an error. Apparently in 4.0.4.GA the PersistenceContext was extended by default, now it has to be specifically set as extended, I'm guessing?