Announcement Announcement Module
Collapse
No announcement yet.
HibernateCursorItemReader thread safety Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateCursorItemReader thread safety

    Hi,

    I have a multi-threaded chunk step that uses HibernateCursorItemReader to read items from a MySQL DB.
    As HibernateCursorItemReader is not thread-safe, i wrapped it in another class, overriding the read() method and declared it synchronized.
    In the step configuration, i defined the reader in the chunk to be the wrapper reader, and the inner reader (HibernateCursorItemReader) is defined as a stream so that its lifecycle methods are called properly.

    I am on Hibernate 3.5.3 and use stateful session in the reader.

    Problem is that 5% of the reads fail with the following exceptions:

    Code:
     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:124)
            at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
            at org.hibernate.loader.Loader.doQuery(Loader.java:773)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
            at org.hibernate.loader.Loader.loadEntity(Loader.java:1933)
            at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
            at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
            at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
            at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
            at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
            at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
            at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
            at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
            at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
            at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
            at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
            at org.hibernate.type.EntityType.resolve(EntityType.java:431)
            at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
            at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
            at org.hibernate.loader.Loader.loadSingleRow(Loader.java:327)
            at org.hibernate.impl.ScrollableResultsImpl.prepareCurrentRow(ScrollableResultsImpl.java:254)
            at org.hibernate.impl.ScrollableResultsImpl.next(ScrollableResultsImpl.java:123)
            at org.springframework.batch.item.database.HibernateCursorItemReader.doRead(HibernateCursorItemReader.java:154)
            at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:85)
            at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    Code:
     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.event.def.DefaultPostLoadEventListener.onPostLoad(DefaultPostLoadEventListener.java:51)
            at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:234)
            at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
            at org.hibernate.loader.Loader.loadSingleRow(Loader.java:327)
            at org.hibernate.impl.ScrollableResultsImpl.prepareCurrentRow(ScrollableResultsImpl.java:254)
            at org.hibernate.impl.ScrollableResultsImpl.next(ScrollableResultsImpl.java:123)
            at org.springframework.batch.item.database.HibernateCursorItemReader.doRead(HibernateCursorItemReader.java:154)
            at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:85)
            at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
            at $Proxy90.read(Unknown Source)
            at com.muzicall.rbt4all.massactivation.reader.StopAwareStagingItemReader.read(StopAwareStagingItemReader.java:89)
            at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
            at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
            at $Proxy91.read(Unknown Source)
    Any clue?

    Thanks

    Daniel

  • #2
    if i synchronize also the update() method of the wrapper, the problem is solved.
    Reason is that i use stateful session, which get cleared in the update() method. This interferes with another thread in the read() method.

    Comment

    Working...
    X