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

  • HibernateCursorItemReader and JTASessionContext

    I'm using Spring Batch to do the following processing:
    1. Read series of entities from database using HibernateCursorItemReader.
    2. Process each entity in the custom ItemProcessor. The processor executes some legacy logic on POJO. The existing logic modifies the state of the entity.
    3. Pass the entity to the writer (not HibernateItemWriter).

    Important information is that I'm using JTATransactionManager and Hibernate session context is set to JTASessionContext. So the session is associated with the overall JTA transaction. In the code I simply use SessionFactory.getCurrentSession() method to acquire the session.

    BTW, I set property useStatelessSession to 'false', so I'm using regular stateful session.

    The issue is that the state of the entity is not persisted to the database. I investigated this issue to its roots and I concluded that it happens, because HibernateCursorItemReader is creating a session using SessionFactory.openSession() that is not the same as the 'current session'.

    I could change the implementation by extending the HibernateCursorItemReader, but unfortunately it has been implemented in such a way that can't be easily extended, which in general is not a good thing.

    I'd like to find out what are your thoughts on that.


  • #2
    The implementation of HibernateCursorItemReader is actually quite deliberate (and therefore the lack of extension point is a good thing). The ItemReader contract is that its products can be cached across transaction boundaries, so to work with ORM features in an ItemProcessor you generally need to attach the item to the current session.