Hibernate version: 2.1
Using Oracle 9
Code between sessionFactory.openSession() and session.close():
I'm new to Hibernate and am experiencing some strage performance problems. I can't find the answer in the FAQ or docs, so here goes...
My application is essentially listening to a data stream, creating objects out of that stream, and saving them to a db. The objects in question contain references to other objects and if these 'reference' objects don't exists in the db then they are created also (I'm basically creating reference data on the fly). So it looks somethine like this:
private Session session;
public void receiveMessage(String[] rawData) throws HibernateException, SQLException { String name = rawData[0]; String companyName = rawData[1]; //this is reference data
List list = session.createQuery("from Company where name = ?") .setParameter(0, companyName, Hibernate.STRING) .setCacheable(true) .list();
Company company; if(list.size() == 0) { company = new Company(name); session.save(company); } else { company = (Company) list.get(0); }
Person person = new Person(name, company); session.save(person); session.flush(); session.connection().commit(); }
In the mapping file for company I have enabled caching by adding <cache usage="read-write"/> and in hibernate.properties i have hibernate.cache.provider_class net.sf.hibernate.cache.EhCacheProvider.
The cache does seem to be working; once a company is in memory it never seems to re-select it from the db. However, the problem is that after running for a few hundred times, the code that selects the company is getting slower and slower. The real application has about eight bits of 'reference data' (like company), and when its first run it takes just a few milliseconds to get all the reference data required for the final insert, but after a few minutes it takes seconds.
Inserts to the ref data table rarely occur.
What was strange is when I added session.clear() after the commit everything worked very quickly.
So, I guess my question is - what am i doing wrong? Using the cache seems to have slowed my app down!
|