I am new to hibernate i have doubt in hibernate batch processing, i read some tutorial for hibernate batch processing they said
Session session = SessionFact
Batch Processing
in Hibernate
means to divide a task of huge numbers to some smaller tasks.
When you fire session.save(obj)
, hibernate will actually cache that object into its memory (still the object is not written into database), and would save it to database when you commit your transaction i.e when you call transactrion.commit()
.
Lets say you have millions of records to insert, so firing session.save(obj)
would consume a lot of memory and eventually would result into OutOfMemoryException
.
Solution : Creating a simple batch of smaller size and saving them to database.
if( i % 50 == 0 ) {
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
Note :
In code above session.flush()
would flush
i.e actually save the objects into database and session.clear()
would clear any memory occupied by those objects for a batch of size 50.