休眠-java.lang.OutOfMemoryError:Java堆空间 [英] Hibernate - java.lang.OutOfMemoryError: Java heap space

查看:96
本文介绍了休眠-java.lang.OutOfMemoryError:Java堆空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此异常:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at java.util.AbstractMap.toString(AbstractMap.java:493)
    at org.hibernate.pretty.Printer.toString(Printer.java:59)
    at org.hibernate.pretty.Printer.toString(Printer.java:90)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:97)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)

使用此代码:

Query query = null;
        Transaction tx= session.beginTransaction();
        if (allRadio.isSelected()) {
            query = session.createQuery("select d from Document as d, d.msg as m, m.senderreceivers as s where m.isDraft=0 and d.isMain=1 and s.organization.shortName like '" + search + "' and s.role=0");
        } else if (periodRadio.isSelected()) {
            query = session.createQuery("select d from Document as d, d.msg as m, m.senderreceivers as s where m.isDraft=0 and d.isMain=1 and s.organization.shortName like '" + search + "' and s.role=0 and m.receivingDate between :start and :end");
            query.setParameter("start", start);
            query.setParameter("end", end);
        }
        final List<Document> documents = query.list();


        query = session.createQuery("select o from Organization as o");
        List<Organization> organizations = query.list(); <---AT THIS LINE
        tx.commit();

我连续进行了2次查询.如果我注释掉其中之一,则其他都可以.

Im making 2 consecutive queries. If i comment out 1 of them the other works fine.

如果我删除交易,则异常的异常消失.这是怎么回事?这是内存泄漏之类的吗?预先感谢.

if i remove transaction thingy exception dissappears. What's going on? Is this a memory leak or something? Thanks in advance.

推荐答案

我从多年的痛苦中得到了这样的建议:答案通常都小心翼翼地隐藏在堆栈跟踪的前10行中的某个位置.始终多次读取堆栈跟踪,如果没有提供足够的帮助,请读取发生故障的方法的源代码.

A tip I picked up from many years of pain with this sort of thing: the answer is usually carefully hidden somewhere in first 10 lines of the stack trace. Always read the stack trace several times, and if that doesn't give enough help, read the source code of the methods where the failure happens.

在这种情况下,问题出在Hibernate漂亮打印机中的某个地方.这是一个日志记录功能,因此问题在于Hibernate试图记录一些巨大的字符串.请注意,在尝试增加StringBuilder的大小时,它是如何失败的.

In this case the problem comes from somewhere in Hibernate's pretty printer. This is a logging feature, so the problem is that Hibernate is trying to log some enormous string. Notice how it fails while trying to increase the size of a StringBuilder.

为什么要尝试记录一个巨大的字符串?我不能从您提供的信息中说出来,但是我想您的组织实体中有很多东西(也许是BLOB?),Hibernate尝试记录查询已从数据库中拉出的对象. .映射中的错误也可能是错误的,因为这样急切的提取会拉入许多相关的对象-例如:---------------------------------------.一个由于错误的外键定义而加载整个表的子集合.

Why is it trying to log an enormous string? I can't say from the information you've given, but I'm guessing you have something very big in your Organization entity (maybe a BLOB?) and Hibernate is trying to log the objects that the query has pulled out of the database. It may also be a mistake in the mapping, whereby eager fetching pulls in many dependent objects - e.g. a child collection that loads the entire table due to a wrong foreign-key definition.

如果映射错误,则修复映射将解决问题.否则,最好的选择是关闭此特定的日志记录功能.关于非常相似的问题,有一个一个现有问题,其中有一些答案中的有用建议.

If it's a mistake in the mapping, fixing the mapping will solve the problem. Otherwise, your best bet is probably to turn off this particular logging feature. There's an existing question on a very similar problem, with some useful suggestions in the answers.

这篇关于休眠-java.lang.OutOfMemoryError:Java堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆