Recently, I am involved in a hibernate performance tuning project. We used to use Oracle Toplink as the Object-Relational mapping tool for our application. Several months ago, we decided to switch to the more popular JPA/Hibernate.
However, after the switch, we experienced wide-spread performance slowdown. To tune the performance of Hibernate, I did a thorough research in related articles. The more I dug in, the more I realized that performance tuning is a systematic effort. There are a lot factors contributing to the performance of Hibernate. I would like to list our experience here as a reference to everyone who use Hibernate.
Considerations for Hibernate Performance Tuning:
1. Database level
- Data Model: The data model design can significantly impact the performance of your OR mapping. Not all the existing data model fits the requirement of Hibernate. Some tables need to be restructured.
- Execution Plan: Hibernate uses its own set of queries. The execution plan for those queries need to be revisited.
- RBO vs. CBO: Did you make the best use of the Cost Based Optimizer in Oracle 10g?
- Full Analyze: Our DBA informed me this trick. Running a full analyze of Oracle may help Oracle to adjust to the new Hibernate usage pattern.
2. Connection/Sessions
- Driver: Are you using “thin” or “oci”? Still using Oracle 9i driver? That may be the issue.
- Connection pooling: Is the connection pooling set up appropriate?
- Session Management: Did you cache your hibernate SessionFactory? It worthwhile to review the hibernate configuration for the sessions.
- Flush mode: What is your flush strategy?
3. Query
- Hibernate vs. Stored Proc: In some of the scenarios, it’s better to use stored procedures instead of Hibernate.
- Tune your HQL
- Batch update/query
- Join: Revisit how you do the join.
- Fetch mode
4. Object Model
- Lazy loading: Without lazy loading, the performance may take a huge hit. Revisit your objects to avoid loading unnecessary objects in your object graph
- Collections: Revisit how the collections are loaded
5. Cache
- Cache Hit Ratio: The hit ratio of your secondary cache is an important factor of your performance. The more you cache, the better the performance, if you have enough memory
- Cache provider: You need to find the most appropriate cache provider for your application. Revisit the cache configuration to reflect the unique needs of your application.
6. Memory
- Heap size: Revisit your JVM settings
- Garbage Collector settings
The Process To Approach Hibernate Performance Tuning:
- Setup performance measurements
- Determine which performance metrics (page to page speed, transaction execution time, top SQLs, etc.) to measure
- Determine which resource utilization (memory, CPU, thread, connections, GC, etc.) to measure
- Setup necessary tools to monitor those metrics. The best tool we use is CA Wily Introscope. You may also use Window Performance Monitor or other similar tools.
- Run the Stress Test with performance monitors tuned on
- Identify the worst performance problems. Find out the top 5 worst performed business transactions. You may use the product of average execution time and total number of transactions to measure the overall “impact” of that transaction to your application’s overall performance.
- Review the resource utilizations to identify possible bottlenecks.
- Run Profiler tool against the worse performed business transaction
- Identify the bottleneck
- Understand the root cause of the problem
- Make changes according to the Profiling result.
- Rerun the Profiling to see if there is any improvement. Repeat until performance is satisfactory.
- Apply the lessons learned in tuning the worst performed transactions to similar transactions project wide.
- Repeat the Stress Test to find more heavy transactions.
In my own experience, performance tuning is a long and iterative trial-and-error process. You may try a lot of things before you find the right solution. But once you find one problem and the solution for that, most likely, it’s applicable to many places of your project.
Another tip is you need to keep repeating the full iteration of test-diagnosis-fix-test. Fixing one problem may cause another problem. Or other deeper problems may emerge once you clean up the easy to fix problems.
There is no silver bullet to solve all the issues. Every application is unique and requires unique configuration and tuning. So, be patient and keep working.
Reference:

2 comments
Comments feed for this article
May 12, 2008 at 12:59 pm
Mike Keith
Sometimes people get what they deserve. Switching to another product just because it seems to be more popular doesn’t mean it is better. The best optimization you could have made would have been to stay with Oracle TopLink.
June 8, 2008 at 2:52 pm
bigapplezlp
Thanks, Mike, for making a good point. I have to admit that moving from TopLink to Hibernate didn’t give us immediate gain in performance. However, I still believe we made a right decision for the long run.
A popular technology means more knowledge available, with less cost. Try searching for any technical problems in Toplink in the internet and see how many answers you get. We used to only have one person in our organization knows TopLink and all the design, implementation and production support must go through him. People simply don’t want to learn TopLink because it won’t benefit their future career.
With JPA and Hibernate, yes, the major advantage is its popularity. Popularity means abundant resources both in people and in documents. Also, it means the organization can attract top developers to maintain and develop the product. The human factor is as important if not more important than the technical factors. Now, we have 10-20 developers all familiar with JPA/Hibernate. The resource scarcity bottleneck was removed. We are happy with it.
The same mentality can be applied to other technologies. So many people feared to move away from their legacy technology platform, only found they started to lose their best people and the maintenance fees rose above the roof.