Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Slow Hibernate Session get

 
shaun brack
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have an issue when using the method org.hibernate.Session.get. It only peforms slowly when I add more than a 1000 records to my hsqldb database running on JBOSS. All the steps prior to this line of code are working instantly such 'Session session = mySessionFactory.getCurrentSession()', but when it come to eg 'session.get(arg0.class,arg1)' it takes 9 seconds when obtaining 1 record. When I use a SQL query I get 1000 records instantly. What does the .get do, the api says 'Return the persistent instance of the given entity class with the given identifier'.

I read something in another post about lazy loading in the hibernate mapping but that has been set to false. Just wanting some information on what the .get does so I can find where in my code I made a mistake. I'm thinking the culprit is either my applicationContext or my hibernate.cfg. Any thoughts would be a help. Thanks
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the following setting configured in your hibernate.cfg.xml file:



This will force all SQL statements to be printed out to the console, log, or system log files depending on how you've set up logging. I'd just be curious to see what the SQL statement that was spit out during this get call was?

Another little test I'd do is to use the Hibernate load method instead of get. The load method shouldn't have such a large impact, as it only loads a proxy, and doesn't hit the database until a getter on your returned object is acquired. Get loads the real entity.

The Difference Between Hibernate load and Hibernate get

Either way, you shouldn't be seeing this type of performance impact if straight SQL gets it that quickly. Is the memory or CPU utilization spiking when you run this query?

-Cameron McKenzie
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets take it step by step.

Just make sure that its not a Database problem.
Whatever query you see in logs, just run that query directly on Database and make sure that it returns instantly.

If there is bottleneck at DB level then hibernate cant improve performance. Sometimes it happens if there is no indexing defined on large db table holding large large data.


HTH
V
 
shaun brack
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you comments it put me on the right track.

The result when it comes to the .get is a few minutes of SQL select statements being printed out. The CPU utilization is 100% on java.exe. When I run the query directly on the database I get instant results. The record I am trying to retrieve has 8 foreign keys. When I do an SQL statement it returns the record and the foreign key values but when I use the hibernate .get it is returning the record + all the data linked to that record. But this wouldn't result in so many SQL queries. Maybe it�s trapped in a loop where it is returning the related records from the foreign keys but those related records are linking back to the original record or other records themselves. I tried removing all the foreign keys from the desired record and the hibernate .get works very fast. There is still about a page of outputed SQL though. BTW .load didn't make a difference.

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your mapping file (or POJO if you use annotations)?
 
Ray Johnson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that your mapping is leading to n+1 queries problem, if i understand the issue correctly. for fields having relationships with other entities check these appropriate attributes - join, subselect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic