• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate/JPA performance

 
Will Myers
Ranch Hand
Posts: 392
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you rate hibernates performance against JDBC or Spring templates? Are there any strategies you use to optimise it?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking, JPA will add more overhead than straight JDBC. Remember at the end of the day the JPA provider is going to spit out sql anyway so there is an extra translation step. JPA however does add caching at various levels which can drastically improve performance over multiple data access calls. Optimizing JPA usually involves making the right decisions about caching, adding indexes where required (you now do that using JPA) and using the optimal fetch strategies for the application use cases. Ultimately (as for all optimization problems) you would have to measure the performance against required SLAs and investigate the source of the delays if any.
 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good answer above.

Hibernate is "complex" and so unpicking its performance strengths and weaknesses is really hard, and I would agree with the above - you need to write real code and test its performance.

But as mentioned in a separate post, if you try to use hibernate for a job it's not good at (eg bulk operations or complex reporting), then you are certain to see horrible performance problems. Its common to see projects writing code that accidentally generates 100x the number of SQL statements needed, because they've misunderstood the purpose of Hibernate (as an object/relational mapper) and they've forgotten that you can use hibernate AND regular SQL in the same project.

Use correctly, Hibernate can outperform hand written SQL because:

1) as mentioned above, caching can help a lot - in a single transaction hibernate won't repeat the same select statement if you inadvertently keep re-requesting the same object

2) automatic dirty checking can be quite efficient at optimizing the update statements needed, sometimes when writing JDBC by hand you might find yourself issuing unnecessary UPDATE statements because you're not sure if an object's state has changed - hibernate can do this automatically.

 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Will,

Just to add you might want to check out the similar question here. Theres a link to our chapter 27 where we talk abit about optimising etc.

Thanks,
Richard.
 
Will Myers
Ranch Hand
Posts: 392
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that.
The reason I asked is that I have inherited an application that pretty much does the same thing as your credit card example and was *very* slow, we changed it to use a set of jdbc batched merge statements and it is lightning fast (doh!).

I think it's a common misunderstanding when someone is thrown onto a project that uses Hibernate that you must use it everywhere, the view seems to be either use Hibernate for all database interaction or use JDBC but never mix.
 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Myers wrote:
I think it's a common misunderstanding when someone is thrown onto a project that uses Hibernate that you must use it everywhere, the view seems to be either use Hibernate for all database interaction or use JDBC but never mix.


Spot on! I think in the early days Hibernate was sold as a "hide the SQL" tool, but it isn't that at all. If anything, with Hbernate your understanding of SQL needs to increase - although the routine statements are generated, you need to keep on top of what is happening. We do a lot of "log inspection" on the course!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic