简体   繁体   中英

HibernateCursorItemReader gets non-threadsafe access to the session Error

@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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM