在Hibernate中使用滚动器抛出异常

时间:2013-07-23 11:40:18

标签: java hibernate jpa

我正在使用组织。休眠。流媒体模式下的ScrollableResults。

我确保我不会遇到n + 1问题。并将所有连接放在同一个SQL语句中。

由于某种原因,在while循环中我有一个异常,在此之前我可以看到一个额外的选择(在流媒体模式下工作时我们期望的第一个选择旁边)

知道我错过了什么吗?

我的卷轴:

protected void scroll(ScrollableHandler<T> handler,String namedQuery, Object... values){
        T previousEntity=null;
        Session s = null;
        ScrollableResults results = null;
        try {
            s = (Session) em.getDelegate();
            org.hibernate.Query query = s.getNamedQuery(namedQuery);
            for (int i = 0; i < values.length; i++)
                query.setParameter(i, values[i]);
            results = query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY);
            while(results.next()) -> here I get the exception
                    {
                T entity = (T) results.get(0);
                if (null != entity && 
                        (! entity.equals(previousEntity))) {
                    handler.handle(entity);
                    previousEntity = entity;
                }
                s.clear();
            }
        } finally {
            if (results != null)
                results.close();
        }
    }

日志:

11:54:24,182  WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: null
11:54:24,182 ERROR SqlExceptionHelper:144 - Streaming result set com.mysql.jdbc.RowDataDynamic@729d8721 is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.
11:54:24,183  WARN CollectionLoadContext:347 - HHH000160: On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [3] entries
Exception in thread "Thread-11" java.lang.RuntimeException: Could not export

谢谢, 射线。

1 个答案:

答案 0 :(得分:0)

问题在于我使用了2个查询。我们知道不应该在活动流上打开并行查询。解决方案:避免第二次查询。

射线。