1 2 Previous Next 16 Replies Latest reply on Mar 9, 2009 7:30 PM by Rafique Anwar

    Concurrency issue: Aren't application scoped components threadsafe?

    Rafique Anwar Newbie

      I have a javabean component in application scope which is stateless of course, the only field entityManager is injected with seam managed persistent context (which itself is in event scope I guess):


      @Name("userDAO")
      @AutoCreate
      @Scope(ScopeType.APPLICATION)
      public class UserDAO {
      
      @In
      protected EntityManager entityManager;
      
      ...
      
      }
      



      The UserDAO is injected in UserService which is then exposed as jax-ws endpoint with @WebService, all are application scoped POJO, no EJB.


      This works fine with a single request at a time, but in case of multiple requests (I used a thread pool in client) it randomly produces multiple exceptions and then hangs the server up in case of 5/10 concurrent threads.


      Some of the exceptions are following:


      2009-03-05 20:16:52,756 ERROR [org.hibernate.AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
      org.hibernate.AssertionFailure: possible non-threadsafe access to the session
           at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:100)
           at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
           at org.hibernate.loader.Loader.doQuery(Loader.java:729)
           at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
           at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
           at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
           at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
           at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
           at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
           at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
           at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
           at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
           at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
           at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
           at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
           at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
           at org.hibernate.type.EntityType.resolve(EntityType.java:379)
           at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
           at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
           at org.hibernate.loader.Loader.doQuery(Loader.java:729)
           at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
           at org.hibernate.loader.Loader.doList(Loader.java:2220)
           at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
           at org.hibernate.loader.Loader.list(Loader.java:2099)
           at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
           at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
           at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
           at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
           at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
           at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
           at com.moveplot.core.dao.jpa.UserJPADAO.findByAuthToken(UserJPADAO.java:95)
           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:597)
      



      and this one is frequeent:


      java.lang.IllegalStateException: EntityManager is closed
           at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:42)
           at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
           at org.jboss.seam.persistence.EntityManagerProxy.createQuery(EntityManagerProxy.java:81)
           at com.moveplot.core.dao.jpa.UserJPADAO.findByAuthToken(UserJPADAO.java:92)
           at sun.reflect.GeneratedMethodAccessor588.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
           at com.moveplot.core.dao.jpa.UserJPADAO_$$_javassist_4.findByAuthToken(UserJPADAO_$$_javassist_4.java)
      
      



      also this one:


      2009-03-05 20:16:52,777 INFO  [org.hibernate.event.def.DefaultLoadEventListener] Error performing load command
      org.hibernate.SessionException: Session is closed!
           at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
           at org.hibernate.impl.SessionImpl.getPersistenceContext(SessionImpl.java:1823)
           at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:182)
           at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
           at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
           at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
           at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
           at org.hibernate.type.EntityType.resolve(EntityType.java:379)
           at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
           at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
           at org.hibernate.loader.Loader.doQuery(Loader.java:729)
           at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
           at org.hibernate.loader.Loader.doList(Loader.java:2220)
           at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
           at org.hibernate.loader.Loader.list(Loader.java:2099)
           at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
           at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
           at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
           at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
           at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
           at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
           at com.moveplot.core.dao.jpa.UserJPADAO.findByAuthToken(UserJPADAO.java:95)
           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:597)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
           at com.moveplot.core.dao.jpa.UserJPADAO_$$_javassist_7.findByAuthToken(UserJPADAO_$$_javassist_7.java)
      



      Nature of these exceptions suggest that it is a cuncurrency related issue: entityManager injected by seam is being shared by multiple threads. My question is if this is expected behavior for an application scoped component? Shouldn't seams bijection take care of the concurrency or synchronization for application scoped components?


      Thanks in advace.

        1 2 Previous Next