1
@Component
@Scope("step")
public class MyReader implements ItemReader<MyDto>, InitializingBean{
    private HibernateCursorItemReader<Object[]> reader;
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void afterPropertiesSet() throws Exception{
        reader = new HibernateCursorItemReader<>();
        reader.setSessionFactory(sessionFactory);
        reader.setUseStatelessSession(true);
        reader.setQueryString(/*query*/);
        //...
    }

    public MyDto read() throws Exception{
        Object[] record = reader.read(); //exception here org.hibernate.AssertionFailure: possible non-threadsafe access to the session
    }
}

When using the HibernateCursorItemReader, I got the org.hibernate.AssertionFailure: possible non-threadsafe access to the session Exception.

How to fix it?

I need it to run the read() so that I can dump the results into a new MyDto Object for the writer to process/write. The writer has its own db calls to get other details too.

Pino
  • 7,468
  • 6
  • 50
  • 69
theAnonymous
  • 1,701
  • 2
  • 28
  • 62
  • Your `MyReader` does not call `open` on the `HibernateCursorItemReader` before calling `read`. This may start using the `HibernateCursorItemReader` in a un-initialized state and can be the cause of your issue. Why don't you use the `HibernateCursorItemReader` directly? What is the added value of your reader? – Mahmoud Ben Hassine Mar 28 '19 at 09:39

1 Answers1

0

As Javadoc says, HibernateCursorItemReader is not thread-safe. The problem is not in the class you have posted but in the way you are using it. Probably you are using it in a multithreaded step but you can't. Besides using a single-threaded step (obvious), a safe multithreaded solution is to use async ItemProcessor/ItemWriter.

See also https://stackoverflow.com/a/28724199/685806

Pino
  • 7,468
  • 6
  • 50
  • 69