保存大量实体时,Hibernate会导致内存不足异常
|
在我的应用程序中,我使用CSVReader和hibernate从csv文件将大量实体(例如1 500 000或更多)导入数据库。代码如下:
Session session = headerdao.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
int count = 0;
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
try {
if (nextLine.length == 23
&& Integer.parseInt(nextLine[0]) > lastIdInDB) {
JournalHeader current = parseJournalHeader(nextLine);
current.setChain(chain);
session.save(current);
count++;
if (count % 100 == 0) {
session.flush();
tx.commit();
session.clear();
tx.begin();
}
if (count % 10000 == 0) {
LOG.info(count);
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
tx.commit();
session.close();
使用足够大的文件(大约70万行),我会出现内存不足异常(堆空间)。
看来问题似乎与休眠有关,因为如果我只注释session.save(current);行,运行正常。如果未注释,任务管理器将显示javaw的内存使用量不断增加,然后在某些时候解析变得很慢并且崩溃。
parseJournalHeader()
没什么特别的,它只是根据csv阅读器给出的String[]
解析实体。
没有找到相关结果
已邀请:
1 个回复
窝头菊