Spring - Hibernate application performance optimization of 7 kinds of methods

By Rick Greene,2015-07-09 09:36
33 views 0
Spring - Hibernate application performance optimization of 7 kinds of methods

    Spring - Hibernate application performance optimization

    of 7 kinds of methods

    for most typical Spring/Hibernate enterprise applications, its performance is almost entirely dependent on the performance of the persistence layer.This article will introduce how to confirm the application whether or not the database constraints, and at the same time introduces seven kinds of commonly used to improve the performance of application crash method, byOneAPMEngineers in translation.

    How to confirm whether the application is limited by the database

    Confirm whether the application is limited by the first step in the database, is in the development environment, and useVisualVMMonitor.VisualVM is a included in the Java JDK analyzer, at the command line input jvisualvm can call.Enable Visual VM, try the following steps:

    ; Double-click on your running applications

    ; Choose the Sampler

    ; Click Settings check box

    ; Select Profile only packages, and then enter the following package:




    Your. The database. The driver package, such as oracle. *

    Click on the Sample CPU

    If the performance is limited by database, the CPU analysis result will look like the image below

    We see, the client Java process waiting for database from the network medium return result accounts for 56% of the time.

    See the database query is applied, the reasons for the slow running is good one million head.Hibernate reflection calls accounted for 32.7% is normal, cannot be further optimized.

    The first step in performance tuning: define the benchmark run

    Performance tuning is the first step for the program to define the benchmark run, we need to define a set of effective implementation of input data, benchmark run your program and the operation of the production environment.

    The main difference is that a benchmark run time consuming much smaller.As a reference, 5 to 10 minutes of execution time is good.

    What is a good reference?

    Good benchmark should have the following characteristics:

    ; Function correctly

    ; The types of input data is similar to a production environment

    ; Completed in a short time

    ; The benchmark run optimization scheme can be extrapolated to complete operation

    Half of the defined baseline is successful to solve the problem.

    What is a bad reference

    For example, by batch operation processing communication system call data record, selecting 10000 records is wrong.

    The reason is: before 10000 records may be more for voice calls, and the performance of the unknown problem may occur in the process of message flow processing.If the benchmark is not good enough at the beginning, will lead to wrong conclusions.

    Collect SQL log and the query time

    The execution of the SQL query statement and its execution time can be collected by way of log4jdbc etc.Details about how to use log4jdbc SQL query information collection, click on the articleUsing log4jdbc optimization Spring/Hibernate application SQL logs

    Query execution time is from Java client, this time including a database query network calls back and forth.SQL query log is as follows:

    16 avr. 2014 11:13:48 | SQL_QUERY /* insert your.package.YourEntity */ insert into YOUR_TABLE (...) values (...) {executed in 13 msec}

    Prepared statements are also important sources of information, they often revealed some common types of queries.Learn more log messages, can view the article:Why/where to use the SQL query Hibernate

    What indicators through SQL log can understand?

    SQL log can answer the following questions:

    ; What is the slowest query execution?

    ; What is the most commonly used query?

    ; Generate a primary key time consuming?

    ; Whether there is data for cache?

    How to parse SQL log

    For a large number of log files, the most feasible analytical method is to use the command line tools, the advantages of this method is very flexible, as long as write a small script or command, we can extract almost most of the indicators.As long as you like, any command-line tools are applicable.

    How you used to the Unix command line, bash, or a good choice.Bash can also be used in a Windows workstation,CygwinorGitContains the bash command line.

    Quick method in common use

    Here is quick method to find out the performance of the common problems in the Spring/Hibernate application, as well as the corresponding solutions.

    Quick method 1 - minimize the cost of generate the primary key

    In the process of the insert frequent and primary key generation strategy is very important.Generated id of a common method is to use the database sequence, usually a table an id, so as to avoid insertion of the conflict between different tables.

    The problem is that if you want to insert 50 records, we hope to get this 50 id, can avoid network calls back and forth, 50 trips to query the database for Java process has been waiting for.

    Hibernate is usually how to solve this problem?

    Hibernate provides the optimization of the ID of the generator in order to avoid this problem.Namely, in sequence, will default to HiLo id generator.The following is the job of the HiLo sequence generator mode:

    ; One call sequence, 1000 (high value)

    ; Calculation of 50 id in the following way

1000 * 50 + 0 = 50000

    1000 * 50 + 1 = 50001


    1000 * 50 + 49 = 50049, 达到低值 (50)

    For the new high value 1001 calling sequence, and so forth

    So called a sequence, can generate 50 key, thus reducing the burden caused by network calls back and forth several times.

    The optimization of the key generator in Hibernate 4 open by default.If you want to disable, but will hibernate. Id. New_generator_mappings set to false.

    Why generate a primary key is still a problem?

    The problem is that if you declare for AUTO key generation strategy, and is not enabled to optimize the key generator, then apply the last call will face a lot of sequence.

    In order to ensure that enable the optimization of the key generator, please change the key generation strategy to SEQUENCE instead of AUTO.


    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "your_key_generator") private Long id;

    Change after setting, application in insert frequently see 10% to 10% performance improvement, and almost no changes in code.

    Quick method of 2 - using the JDBC batch inserts/updates

    For batch program, the JDBC driver provides the aim to reduce the network transmission, a new method for the optimization of back and forth "JDBC batch inserts/updates".After using this method, insert or update will be first in line driver layer, and then sent to the database.

    After reaching the threshold, all line up the statement will be one-time to the database.This can avoid the driver by transfer statement, led to the burden of network transmission back and forth.

    Through the following configuration, to activate a batch inserts/updates: 100



    Just set the JDBC batch size is not enough.Because the JDBC driver will only after receipt of the same table insert/updates batch these statements.

    If received on a new list of insert statement, the JDBC driver will clean up a list of former first batch statements, and then began to batch SQL statements for the new table.

    Spring Batch built-in similar functions.This optimization can bring in insert frequent applications of 30% to 40% performance improvement, without having to change any line of code.

    Quick method 3 -- regular cleaning Hibernate session

    In to the database to add or modify the data, Hibernate will keep a version of the already existing entities in the session, in case of these entities before the session closes be modified again.

    However, in most cases, once the corresponding insert operation has been completed in the database, we can safely discard those entities.This will release the memory in the Java client process, avoid too long Hibernate session lead to performance problems.

    The long session should be avoided.But if for some reason had to use them, here is a way to control the memory consumption:



    Flush touch will make the new entity in the insert statement sent to the database.The clear will release the new entity in the session.

    Quick method of 4 - reducing Hibernate dirty - checking (dirty data check)

    Hibernate internal use a mechanism used to track the modified entity, called dirty - checking.The mechanism of the equals and hashcode methods are not based on the entity class.

    Hibernate as far as possible, keeping the performance cost of dirty - checking in the lowest, only when needed to use dirty - check.But the mechanism also has costs in a lot of the number of columns in the table of the cost is considerable.

    Before any optimization, the most important thing is to use VisualVM measuring dirty - the cost of checking.

    How to avoid the dirty - checking?

    Dirty - checking can be disabled by the following way:


    public void someBusinessMethod() {



    Disable the dirty - checking another way is to use Hibernate stateless session, predict the details, please check the document.

    Quick method of 5 - search for "bad" query plan

    Check the slowest query list, see if there are any good query plan.The most common form of "bad" query plan include:

    A full table search: usually a lack of an index or table statistics for full table search expires.

    The cartesian join: it means calculate the cartesian product of table.Check the connection condition of lack, or broken down into several steps in order to simplify the query.

    Quick method of 6 - interval to check for errors

    If you use a batch program, submit interval will be ten times or even one hundred times the effects on the performance.

    Please make sure to submit interval is in line with expectations (for Spring batch task, usually between 100 and 1000).Often, the parameter configuration is not correct.

    7 - using secondary query cache accelerated method

    If some data can be cached, you can see this article to understand how to set up the Hibernate cache: secondary/Hibernate query cache trap.

Report this document

For any questions or suggestions please email