Announcement Announcement Module
Collapse
No announcement yet.
collections cannot be fetched by a stateless session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • collections cannot be fetched by a stateless session

    I'm trying out Spring Batch for the first time with code I downloaded this week. I'm trying to adapt the hibernate sample to some more complicated hibernate mappings, and I get the following exception.

    I don't see any way to configure around this, as HibernateCursorInputSource always uses a StatelessSession, and there are no other Hibernate InputSources. Or am I missing something? Thanks.

    org.hibernate.SessionException: collections cannot be fetched by a stateless session
    at org.hibernate.impl.StatelessSessionImpl.initialize Collection(StatelessSessionImpl.java:304)
    at org.hibernate.collection.AbstractPersistentCollect ion.forceInitialization(AbstractPersistentCollecti on.java:454)
    at org.hibernate.engine.StatefulPersistenceContext.in itializeNonLazyCollections(StatefulPersistenceCont ext.java:755)
    at org.hibernate.loader.Loader.doQueryAndInitializeNo nLazyCollections(Loader.java:229)
    at org.hibernate.loader.Loader.loadEntity(Loader.java :1785)
    at org.hibernate.loader.entity.AbstractEntityLoader.l oad(AbstractEntityLoader.java:47)
    at org.hibernate.loader.entity.AbstractEntityLoader.l oad(AbstractEntityLoader.java:41)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.load(AbstractEntityPersister.java:2730)
    at org.hibernate.impl.StatelessSessionImpl.get(Statel essSessionImpl.java:287)
    at org.hibernate.impl.StatelessSessionImpl.get(Statel essSessionImpl.java:282)
    at org.hibernate.impl.StatelessSessionImpl.internalLo ad(StatelessSessionImpl.java:321)
    at org.hibernate.type.EntityType.resolveIdentifier(En tityType.java:266)
    at org.hibernate.type.EntityType.resolve(EntityType.j ava:303)
    at org.hibernate.engine.TwoPhaseLoad.initializeEntity (TwoPhaseLoad.java:116)
    at org.hibernate.loader.Loader.initializeEntitiesAndC ollections(Loader.java:842)
    at org.hibernate.loader.Loader.loadSingleRow(Loader.j ava:276)
    at org.hibernate.impl.ScrollableResultsImpl.prepareCu rrentRow(ScrollableResultsImpl.java:231)
    at org.hibernate.impl.ScrollableResultsImpl.setRowNum ber(ScrollableResultsImpl.java:211)
    at org.springframework.batch.io.cursor.HibernateCurso rInputSource$HibernateInputSourceTransactionSynchr onization.afterCompletion(HibernateCursorInputSour ce.java:138)
    at org.springframework.transaction.support.Transactio nSynchronizationUtils.invokeAfterCompletion(Transa ctionSynchronizationUtils.java:133)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.invokeAfterCompletion(Abs tractPlatformTransactionManager.java:904)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.triggerAfterCompletion(Ab stractPlatformTransactionManager.java:879)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.processRollback(AbstractP latformTransactionManager.java:782)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.rollback(AbstractPlatform TransactionManager.java:730)
    at org.springframework.transaction.support.Transactio nTemplate.rollbackOnException(TransactionTemplate. java:153)
    at org.springframework.transaction.support.Transactio nTemplate.execute(TransactionTemplate.java:131)
    at org.springframework.batch.execution.step.simple.Si mpleStepExecutor$1.doInIteration(SimpleStepExecuto r.java:203)
    at org.springframework.batch.repeat.support.RepeatTem plate.getNextResult(RepeatTemplate.java:296)
    at org.springframework.batch.repeat.support.RepeatTem plate.executeInternal(RepeatTemplate.java:195)
    at org.springframework.batch.repeat.support.RepeatTem plate.iterate(RepeatTemplate.java:128)
    at org.springframework.batch.execution.step.simple.Si mpleStepExecutor.process(SimpleStepExecutor.java:1 80)
    at org.springframework.batch.execution.job.DefaultJob Executor.run(DefaultJobExecutor.java:97)
    at org.springframework.batch.execution.launch.SimpleJ obExecutorFacade.start(SimpleJobExecutorFacade.jav a:197)
    at org.springframework.batch.execution.launch.SimpleJ obLauncher.runInternal(SimpleJobLauncher.java:249)
    at org.springframework.batch.execution.launch.SimpleJ obLauncher$1.run(SimpleJobLauncher.java:282)
    at org.springframework.core.task.SyncTaskExecutor.exe cute(SyncTaskExecutor.java:49)
    at org.springframework.batch.execution.launch.SimpleJ obLauncher.run(SimpleJobLauncher.java:279)

  • #2
    I think the intention of that input source is to enable you to loop through some lightweight "key" type objects that can be used by the main business logic in an ItemProcessor. Hence it is sort of an advantage to get the lazy loading exception - it's a signal (albeit a cryptic one) that you are misusing the input source and might be trying to load an unreasonable amount of data.

    A workaround for you might be to set the fetch mode in the query string for the lazy association youre hitting.

    I am not 100% convinced of the value of the HibernateCursorInputSource, but I am glad to hear of someone trying it out. If you can argue your case a bit we might think about making a version available with a full stateful session. Robert might also have something to say (he wrote it).

    P.S. Can you use [code][/code] to post stack traces and code, please?

    Comment


    • #3
      The current hibernate input source is just the simplest way to support hibernate and yes, stateless session does not support collections of associated objects. I think the current implementation can be renamed to SimpleHibernateCursorInputSource and a DefaultHibernateCursorInputSource could be implemented using a standard session (which would be flushed and cleared on a configurable interval).

      Comment


      • #4
        It would be straighforward to change the implementation to use stateful session (simply changing the session declaration and clearing the session on commit should do the job), but I think having both options (stateless and stateful session) would be best. However I don't yet see a good way how to implement both in a "DRY" way.
        Last edited by robert.kasanicky; Nov 19th, 2007, 03:51 AM. Reason: missing word added

        Comment


        • #5
          I've started a JIRA issue and submitted a patch that adds a boolean "useStatelessSession" setter to the input source:

          http://opensource.atlassian.com/proj...owse/BATCH-209

          Comment


          • #6
            Thanks for the patch, it worked well!

            Comment


            • #7
              How big is your job? How many records are you reading into your stateful session?

              Robert: can you comment on the cache strategy for the input source - how often is the session and its cache going to be cleared (and can we control that)?
              Last edited by Dave Syer; Nov 20th, 2007, 03:25 AM. Reason: confusing language updated

              Comment


              • #8
                In the submitted patch session is cleared after successful commit.

                I thought about making the interval configurable by the user but decided to proceed with the simplest solution first and see whether there is need for something more elaborate. Also adding a setter that would matter only for stateful session didn't feel right - it would make sense to have two input sources then, which I find problematic to implement in a "DRY" way.

                Comment


                • #9
                  I'm just toying with it right now, reading 10's of records but if we use spring batch we'd process 10's of thousands. Right now I'm having more trouble with the output end of things, but I think that's more a matter of integration with local code than anything else.

                  Comment


                  • #10
                    I am getting a similar error message(collections cannot be fetched by a stateless session) when trying to read the data using stateless session.

                    I am not using spring batch. I am using spring2.5 for a stand alone/batch application(we are not allowed to use spring batch yet) .
                    I am trying to use stateless sessions so that it could help improve my performance.

                    Can someone suggest the best way to optimise the performance for batch application while using hibernate?

                    Comment


                    • #11
                      what do I apply the patch to it doesnt seem to be working

                      Comment

                      Working...
                      X