Batch Processing
Friends, batch processing means processing insert ,udate and delete operations in batch or
bulk, Just for the sake of our understanding lets take a small example,
If we want to insert 100000 records into the database then our code will be as below
Session sessionObj = sessionFactoryObj.openSession();
Transaction transactionObj = sessionObj.beginTransaction();
for ( int y=0; y<100000; y++ ) {
House houseObj = new House(……);
session.save(houseObj);
}
transactionObj.commit();
sessionObj.close();
Friends what do you think , will it run successfully ??
No ! This will fail with an OutOfMemoryException somewhere around the 50,000th row. That
is because Hibernate caches all the newly inserted Customer instances in the session-level
cache.
To overcome this problem If you are undertaking batch processing you will need to enable
the use of JDBC batching. This is absolutely essential if you want to achieve optimal
performance. Set the JDBC batch size to a appropriate number (10-20, for example):
e.g : hibernate.jdbc.batch_size 20
Point to be Noted : Hibernate disables insert batching at the JDBC level transparently if you
use an identity identifier generator.