7 ways to improve performance of a Hibernate application
HibernateĀ is a great tool forĀ integratingĀ JavaĀ web applications with a SQL database, but what shouldĀ we do when theĀ applicationās response times startĀ increasing considerably? What shouldĀ we do when weāreĀ getting doubts whetherĀ the application will scale with the growth of ourĀ customer base?
Before spending a fortune on hosting or rewriting the code, considerĀ analysing and improvingĀ these 7 areas of yourĀ web application.
#1 Tuning JVMĀ memory settings
The JVM allows us to configure the amount of memory it will allocate to run ourĀ application. If the memory settingsĀ (heap size, perm gen size, etc.) are not configured properly, chances are that theĀ Garbage CollectionĀ process runsĀ way too often which will cause spikes in the CPU usage and halts in the execution.
#2 Optimizing configuration of JDBC datasource andĀ Hibernate
In these configurations we canĀ define how Hibernate and the underlying JDBC layer should behave in general. Changes (pool size, ordering, etc.) made in this section of the application may impact every single SQL query andĀ update. Hence itās crucial to get these settings right for the optimal performanceĀ of your application. Itās also worthĀ mentioning here the Hibernate 2nd level cache. When the application runs the same queriesĀ over and over,Ā caching can save up to 99% of the roundtrips to the database. There are severalĀ caching frameworksĀ available on the market that can act as 2nd level cache providers, some of the most well-knownĀ ones areĀ EHCache,Ā Oracle Coherence,Ā InfinispanĀ andĀ Hazelcast.
#3Ā OptimizingĀ Hibernate mappings
Whether weāre using annotations or XML files toĀ configure theĀ HibernateĀ mappings, a few lines of changesĀ (lazy loading, batching, etc.) can greatly improveĀ the performance of our application. These mappings directly impact what SQL statements will be generated and executed by Hibernate. In most cases using the default mapping configurations may just work fine, but optimizing them for the critical SQLĀ queries and updates can substantially increase the number of round-trips to the DB and the actual time spent on execution.
#4Ā OptimizingĀ Hibernate Session management
When dealing with transactions, itās key to understand the different isolation levels and propagation types and to split the read-only and read-write DB operations into separateĀ transactions.
#5 Adding missing DB indexes
There are a few different ways toĀ identifyĀ theĀ most resource intensive SQL statements, but once we have a list of them,Ā adding the necessary indexes becomesĀ fairly straight forward. Without those indexes, the database may have to read through entireĀ DB tables row by row. That is one of the most resource intensive operations in the DBĀ and itās definitely something weĀ want to avoid.
#6 Improving SQL statements
Some Hibernate-generated SQLĀ statements mayĀ containĀ many unnecessary join operations. That is a sure-fire way to make SQL statements crawl. We must make sureĀ to only include joins that are absolutely necessary. Another thing to keep in mind is how WHEREĀ conditions are organized to filter the result set. Choosing the wrong order mayĀ slow down queries by an order of magnitude.
#7 Refactoring java code
One of the most typical issues in the code is the N + 1 query problem where we run a query to get a list of items and then loop through these items to run separate queries on each one of them. This anti-pattern alone can killĀ mostĀ attempts to scaleĀ theĀ applications and may cost us dearly in hosting. As its fix requiresĀ code refactoring, it is one of the most complex andĀ expensive performance issues to have.
Summary
Thereās still hope when every attemptĀ seem to beĀ failing to fix performance issues.Ā TheĀ above items may help identifying the mainĀ bottlenecks before they escalate into bigger problems. What performance issues are hurting your business now?
| Reference: | 7 ways to improve performance of a Hibernate application from our JCG partner David Kiss at the Building scalable enterprise applications blog. |





Hi David, I about fell out of my chair when I read this comment of yours about the “SELECT N+1” >> As its fix requires code refactoring, it is one of the most complex >>and expensive performance issues to have …because I see that on a regular basis in my day job as a performance engineer. This causes so much pain for us, I wrote a tool to help visualize it. It has been especially helpful for our hibernate developers. The help helps the developers visualize and understand how their hibernate changes impact the queries executed. Got time to take… Read more »