aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate session management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate session management" Watch "Hibernate session management" New topic
Author

Hibernate session management

Brian Mulholland
Ranch Hand

Joined: Mar 12, 2009
Posts: 61
I am trying to improve a hibernate webapp written by others that is performing terribly. I turned on showSQL in the properties and am shocked by how many unnecessary queries it makes, as well as the fact that it updates every field of every row regardless of dirty-ness. Among many problems one that my co-worker and I think is a major contributor is that the app tends to call hibernateTemplate.method() for most of it's behaviors. Does this create a new session each time and therefore hibernate doesn't know how to reduce workload (dirty, duplicate queries etc)?

In addition to asking the question, i want to get a logging into the session creation and checkout process so that I can echo out how often sessions are created. I have one on the datasource where connections are created and can see connections being checked out way to often. I was hoping that I could do the same with the session to prove the point. the app uses AnnotationSessionFactoryBean for it's session factory and I subclassed it and overrode the wrapSessionFactoryIfNecessary method, but it never gets called. How can I get some logging in there?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Spring HibernateTemplate?


[How To Ask Questions][Read before you PM me]
Brian Mulholland
Ranch Hand

Joined: Mar 12, 2009
Posts: 61
Bill Gorder wrote:Spring HibernateTemplate?


Correct.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Well unless you are working with a legacy application using a really old version of Hibernate and Spring, then the ORM Template classes (like HibernateTemplate) are no longer the accepted pattern, rather you should use the Hibernate API directly.

I would start with just making sure that Hibernate and Spring are being integrated correctly.

I have some comments to that in this thread:
http://www.coderanch.com/t/590071/ORM/databases/org-hibernate-SessionException-Session-closed

The Spring documentation is a good source of info:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/orm.html
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/orm.html#orm-hibernate

Note Hibernate 4 uses different classes
http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/

Once you have gone through that you can re-assess.

For the query piece make sure you are not lazy loading where you should be eager loading or using fetch queries. For example if you know you are going to have to iterate over a collection and access every object in it don't lazy load it or hibernate will be issuing a select for every element as its accessed.

If you are using session.update() it will trigger an update if the entity was detached regardless of whether it has changed or not. This is because when it is reattached it is automatically marked as dirty. Consider using merge() instead to avoid this.

There are many ways to use a library like Hibernate improperly I am afraid a lot of it is just going to be wading through finding the bottlenecks and issues and addressing them.

Good Luck!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate session management