This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I need some help. We have a struts app that uses Spring and Hibernate3. When Hibernate does a load() (sql Select) we unencrypt some of the data in the record. As a result, Hibernate suspects the record might be dirty and checks by doing a SQL Update. We don't want this overhead - and also - it's causing hibernate stale object exceptions as we use an optimistic approach - so if one user saves a record while another user is trying to load it the load fails (we handle dirty conditions on the save).
To turn off the update statement calls I set the flush to NEVER on both the session and the hibernate template objects - this works great.
Here's the question:
Since we use Spring we used to call getHibernateTemplate on the Spring HibernateDaoSupport object, but the docs indicate
NOTE: from the doc on getHibernateTemplate() method -
The returned HibernateTemplate is a shared instance.
You may introspect its configuration, but not modify the
configuration (other than from within an DaoSupport.initDao()
implementation). Consider creating a custom HibernateTemplate
instance via new HibernateTemplate(getSessionFactory()), in which
case you're allowed to customize the settings on the resulting instance.
so now we call
HibernateTemplate lHibernateTemplate = new HibernateTemplate(getSessionFactory());
but this means that for every query we're creating a new HibernateTemplate object that will hang around until the garbage collector runs as opposed to using the shared one --- this application gets hammered on in production so we're concerned about that.
Does anyone have any thoughts about the performance issues of calling new on the HibernateTemplate over and over and over like that? We're not expert Hibernate users so maybe it's a non-issue, but we want to be careful.
Or - any one know of another way to turn off the flushing that causes the sql update right after the sql select ?
You can create another instance variable and just mark it as @Transient. Then have the real instance variable that is mapped to the field to use "property" AccessType. And in the setter method call a method that will set the @Transient value to the decrypted value.
Also, Since you are using Hibernate 3.x, the need for HibernateTemplate is gone. Especially the HibernateDaoSupport class. You are now tightly coupled to Spring by doing that.
As a Spring instructor, the slides even mention it that if you are using Hibernate 3.x, that the HibernateDaoSupport and HibernateTemplates are deprecated.