簡體   English   中英

HibernateCursorItemReader獲取對會話錯誤的非線程安全訪問

[英]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
    }
}

當使用HibernateCursorItemReader ,我得到了org.hibernate.AssertionFailure: possible non-threadsafe access to the session異常。

怎么解決?

我需要它來運行read()以便我可以將結果轉儲到一個新的MyDto對象中供編寫者處理/寫入。 作者有自己的db調用來獲取其他細節。

正如Javadoc所說HibernateCursorItemReader 不是線程安全的 問題不在於您發布的課程,而在於您使用它的方式。 可能你是在多線程步驟中使用它,但你不能。 除了使用單線程步驟(顯而易見)之外,安全的多線程解決方案是使用異步ItemProcessor / ItemWriter

另請參見https://stackoverflow.com/a/28724199/685806

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM