0

我正在使用 org. 休眠。流模式下的 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

谢谢,雷。

4

1 回答 1

0

问题是我使用了 2 个查询。正如我们所知,我们不应该在活动流中打开并行查询。解决方案:避免第二次查询。

射线。

于 2013-10-11T07:53:41.020 回答