Recently I was getting "Cannot open connection/Transaction Inactive" Error when doing
large number (20000) updates with hibernate.
The solution was using batch update with hibernate.
A naive approach to inserting 20,000 rows in the database using Hibernate might look like this:
large number (20000) updates with hibernate.
The solution was using batch update with hibernate.
A naive approach to inserting 20,000 rows in the database using Hibernate might look like this:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); } tx.commit(); session.close();We can also do this kind of work in a process where interaction with the second-level cache is completely disabled: hibernate.cache.use_second_level_cache false Solution is as below: When making new objects persistentflush()and thenclear()the session regularly in order to control the size of the first-level cache.Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit();session.close();
No comments:
Post a Comment