Tip 1 Use a good JDBC Profiler
- Sort the queries
- Total time eclipsed summary graph
- Drill down to a single query and how execution status
- Export data into Excel and other formats
A JDBC profiler should be used as part of the development, QA process to catch potential performance issues and in production to help trouble shoot live performance issues.
In Development and QA
- Hibernate is generating the correct SQL (from HQL)
- Hibernate is loading just right amount of data (use lazy loading whenever possible)
- Hibernate is generating the correct amount of SQL calls. An abnormal amount of SQL calls per web service call or per web page turn indicates poor design and potential performance issue
- Look for SQL that is taking long time to execute. Examine the explain plan and make sure the plan makes sense. If the generated sql does not meet requirement, consider rewriting the query or using native SQL or a function or a stored procedure for better performance
In Production
Tip 2 Understand Transaction Flush Mode
Hibernate does not flush every add or update transaction to the database. Rather Hibernate collects them and waits for the right time to flush them all t the database. And the right time is defined by the Transaction Flush mode. There are four Flush mode,
- Always, the session is flushed every query
- Commit, the session is flushed when transaction is committed
- Manuel, the session is flushed manually, i.e., Hibernate will NOT flush session to the database during query or commit time
- Auto, default Flush mode and yet the most confusion one. The session is flushed before a query is executed or transaction is committed
Why we need transaction Flush mode?
- Begin transaction
- Create employee A
- Create employee B
- Associate A with its manager C
- Associate B with its manager D
- Commit transaction
- Begin transaction
- Create employee A
- Create employee B
- Associate A with its manager C
- Look up all employees reporting to C
- Associate B with its manager D
- Look up all employees reporting to D
- Commit transaction
Default Flush Mode introduces Performance Problem during Bulk Operations
We had a page that creates a new campaign based on an existing campaign template via a deep copying . A campaign object could contain possibly hundreds of other objects. A typical flow is like the following,
- Begin Transaction
- Retrieve the template campaign
- Shallow copy and save the top level campaign objects
- For each top level campaign objectIterate through all nested objects
- Retrieve next level campaign objects
- Shallow copy and save the secondary level campaign objects
- Commit Transaction
A typical copy operation takes 30 minutes. This clearly indicates a performance issue. After further investigation, we traced the problem back to hefty performance cost introduced by database transaction flush.
For each select statement like retrieving the next level campaign objects, Hibernate does a database flush and as the number of objects loaded in the session increases, the time to determine the “dirty” objects increases dramatically. And there is absolute NO need to do database flush, since we are NOT making any changes to existing objects, only creating new ones.
The solution is to switch the default flush mode to COMMIT. This cuts the execution time from 30 minutes to 3 seconds.
So next time if an operation takes abnormal long time to execute and it is not being held up by the database itself, check Hibernate transaction flush mode carefully. Typically I use either MANUEL or COMMIT for any type of bulk operations or read-only operations.
Tip 3 Use Batch Operations
- Select a set of user ids based on the selection criteria
- Get each user for each returned user id
- Select a set of user ids based on selection criteria
- For every 300 user id
- Select users where user id in (the set of 300 users)
bxyrx…
mpsmjfobo jyjid ckkosps tlzh ndhpcrffmbksowv…
… [Trackback]…
[...] There you will find 11956 more Infos: javaplex.com/blog/performance-tuning-hibernate-transaction-flush-mode/ [...]…
… [Trackback]…
[...] There you will find 55614 more Infos: javaplex.com/blog/performance-tuning-hibernate-transaction-flush-mode/ [...]…