Wednesday, June 2, 2010

Cannot open connection Error on large hibernate updates/inserts

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:
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 persistent flush() and then clear() 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();

Strange Problem when integrating our webservice with EAI webMethods

I recently faced a strange error when integrating our webservice with webMethods.

In our WSDL we had a new element name= Culprit_Owner type="xsd:int" .... defined.
In the unit testing, when we tested the webservice with SOAP UI client, it was working very fine.

We faced this problem only at the time of Integration testing.

We were getting the error,
Failed to parse source:Failed to find read method or field for property 'culprit_Owner' in class...

I guess webMethods was treating the column as culprit_Owner instead of Culprit_owner and it was not finding the proper GETTER method.

I resolved the issue by changing the WSDL element name from Culprit_Owner to culpritOwner
and off course rengenerating the GETTER and SETTERS.