Hello,
I have trouble trying to have charge tests using hibernate.
The problem is that when I insert lots of data (20.000 records), I get "out of memory error" from the jvm.
Obvious code for doing this is:
Code:
Configuration cfg = new Configuration();
cfg.addClass(Blsmatst.class);
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
int maxRecord = 100000;
Blsmatst record;
int i;
for (i = 0; i < maxRecord; i++){
record = createNewBlsmatst(i);
session.save(record);
}
session.flush();
session.connection().commit();
with createNewBlsmatst(...) filling each field of a newly created record.
Next, I thought: "too much data in the session makes it blow".
Sounds reasonable. then, I inserted a memory cleaner once in a while:
Code:
Configuration cfg = new Configuration();
cfg.addClass(Blsmatst.class);
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
int maxRecord = 100000;
Blsmatst record;
int i;
for (i = 0; i < maxRecord; i++){
record = createNewBlsmatst(i);
session.save(record);
if (i % 10000 == 0){
session.flush();
session.clear();
}
}
session.flush();
session.connection().commit();
And it worked better, but same problem occurred only later (around 100.000 record)
Calling garbage collector once in a while did not change anything ...
But closing connection and reopening it once in a while made me allow to test up to 10.000.000 inserts with no problems.
Here is final code:
Code:
Configuration cfg = new Configuration();
cfg.addClass(Blsmatst.class);
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
int maxRecord = 100000;
Blsmatst record;
int i;
for (i = 0; i < maxRecord; i++){
record = createNewBlsmatst(i);
session.save(record);
if (i % 10000 == 0){
session.flush();
session.connection().commit();
session.clear();
session.close();
session = sf.openSession();
System.gc();
}
}
session.flush();
session.connection().commit();
The most annoying is that I had same error and had to apply the same tactic for being able to real as much data: close and reopen the session!
My questions are:
Why does the session.clear() not clear it completely?If so: is there no other way to do it than to destory the session? Because, of course, I would like to be able to work with this very same session for query purposes.
Thanks
FYI:
- I use hibernate 2.1.2
- I use Sun's JVM on Win2000 Pro (jdk 1.4.2_01)
- I use Oracle 9 with oci driver (from ojdbc14.jar)
- the object I use here (Blsmatst) is very simple: one table only.
- Here follows my .hbm.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.netbnk.Blsmatst" table="PWA_BLSMATST">
<id name="hdruid" column="HDRUID" type="string">
<generator class="assigned"/>
</id>
<property name="hdrstt" column="HDRSTT" type="string" length="1" not-null="true"/>
<property name="hdrsyn" column="HDRSYN" type="string" length="20" not-null="true"/>
<property name="accidn" column="ACCIDN" type="string" length="36" not-null="true"/>
<property name="baluid" column="BALUID" type="string" length="9" not-null="true"/>
<property name="detuid" column="DETUID" type="string" length="9"/>
<property name="sttdup" column="STTDUP" type="string" length="1"/>
<property name="en1seqnbr" column="EN1SEQNBR" type="string" length="4"/>
<property name="en1detnbr" column="EN1DETNBR" type="string" length="4"/>
<property name="en1reffin" column="EN1REFFIN" type="string" length="13"/>
<property name="en1refext" column="EN1REFEXT" type="string" length="8"/>
<property name="en1trxsgn" column="EN1TRXSGN" type="string" length="1"/>
<property name="en1trxamt" column="EN1TRXAMT" type="string" length="15"/>
<property name="en1trxdat" column="EN1TRXDAT" type="string" length="8"/>
<property name="en1trxcod" column="EN1TRXCOD" type="string" length="8"/>
<property name="en1msgtyp" column="EN1MSGTYP" type="string" length="1"/>
<property name="en1msgzon" column="EN1MSGZON" type="string" length="53"/>
<property name="en1trxent" column="EN1TRXENT" type="string" length="8"/>
<property name="en1seqpap" column="EN1SEQPAP" type="string" length="3"/>
<property name="en1glocod" column="EN1GLOCOD" type="string" length="1"/>
<property name="en1codnxt" column="EN1CODNXT" type="string" length="1"/>
<property name="en1filcod" column="EN1FILCOD" type="string" length="1"/>
<property name="en1codlnk" column="EN1CODLNK" type="string" length="1"/>
<property name="en2seqnbr" column="EN2SEQNBR" type="string" length="4"/>
<property name="en2detnbr" column="EN2DETNBR" type="string" length="4"/>
<property name="en2msgzon" column="EN2MSGZON" type="string" length="53"/>
<property name="en2cliref" column="EN2CLIREF" type="string" length="26"/>
<property name="en2oricur" column="EN2ORICUR" type="string" length="3"/>
<property name="en2oriamt" column="EN2ORIAMT" type="string" length="15"/>
<property name="en2codnxt" column="EN2CODNXT" type="string" length="1"/>
<property name="en2codlnk" column="EN2CODLNK" type="string" length="1"/>
<property name="en3seqnbr" column="EN3SEQNBR" type="string" length="4"/>
<property name="en3detnbr" column="EN3DETNBR" type="string" length="4"/>
<property name="en3benacc" column="EN3BENACC" type="string" length="12"/>
<property name="en3bencod" column="EN3BENCOD" type="string" length="10"/>
<property name="en3benacx" column="EN3BENACX" type="string" length="15"/>
<property name="en3bencrd" column="EN3BENCRD" type="string" length="78"/>
<property name="en3codlnk" column="EN3CODLNK" type="string" length="1"/>
<property name="trxcur" column="TRXCUR" type="string" length="3" not-null="true"/>
</class>
</hibernate-mapping>