[Conclusion] performance tuning of Hibernate

Source: Internet
Author: User

 

Today, when I write a hibernate application, I found that it takes 27 seconds for a program to be loaded and the CPU usage is large. max = 27890kb, mix = 13469kb
At first, I wanted to use a synchronized jprogressbar to implement interaction with users and use a thread pool to load data in the database. I thought it would be faster to use multiple threads. Unfortunately, I am wrong, originally, swing was not thread-safe. As a result, as I did not expect, it had to re-enable a scheme to achieve performance tuning because it had to generate n major exceptions and all of them were unrecoverable exceptions. We strive to load the database data within 10 seconds.
I have summarized some points. The sorting performance in MSSQL is different from that in MySQL. Although top and limit have their own merits, I still like top, in the final analysis, it is still a thread problem. The performance tuning in hibernate mainly involves the following aspects: connection pool optimization, batch optimization, and Cache Mechanism Optimization during search, the most important thing is the most easily overlooked-the flexible use of hql and multi-parameter constructor methods. This is very common in practice. In fact, many people prefer to directly operate with their own defined persistent layer object classes during design, in addition, if you do not pay attention to the details and read and process the relevant data when obtaining a data, the IO operation on the entire database server will be abnormal.
1. It is best to disable lazy when reading only one or a small amount of data, which not only speeds up loading, but also improves user experience.
2. when reading a large amount of associated data and only associating one or several data items in another class, I think the best way is to use hql and custom read classes, you can also use multi-parameter constructor to achieve this, which is a good choice for performance.
3. when dealing with databases with high concurrency and low isolation levels, we usually reject long-time access and set the controllability of Multi-Point checks. This is achieved through the server.
4. Detail Tuning
(1) hibernate. query. Substitutions true 1, false 0, yes 'y', no 'n'

This configuration means that when you input true in hibernate, Hibernate will convert it to 0 and insert it into the database. When you input false in hibernate, Hibernate will convert it to 1 and insert it into the database, the same applies to Y and N. For some databases, such as Oracle, if there is no boolean data type, 1 indicates true, 0 indicates false. Therefore, using this configuration in hibernate directly using True/false is very intuitive.
(2) Selection of connection pools
A: There are two types of connection pools in hibernate: app server and DBCP connection pool. The default connection pool is DBCP. However, it is best to use appserver in the project and select the DBCP connection pool that comes with hibernate. The built-in connection pool should be selected as the final choice. Below are the optimization methods:
Hibernate. Connection. pool_size 1

Hibernate. statement_cache.size 25

This is the configuration parameter of the connection pool that comes with hibernate. It will be used by hibernate by default.

B: If you use the DBCP connection pool, in addition to configuring the DBCP connection pool, you also need to cancel the comment for dropping the line:

Code: hibernate. Connection. provider_class net. SF. hibernate. Connection. dbcpconnectionprovider

C: If the connection pool of the app server is used, assuming that the JNDI name of the datasource of the app server connection pool is "mypool", the configuration should

This is as follows:

Code: hibernate. dialect net. SF. hibernate. dialect. mysqldialect

Hibernate. Connection. datasource mypool

Hibernate. Connection. provider_class net. SF. hibernate. Connection. performanceconnectionprovider

You do not need to write other parameters because they have been specified when the app server configures the connection pool.

D: hibernate Optimization in EJB or JTA
If you are not using hibernate in the app server environment, such as a remote client program, but you want to use the app server data

Database connection pool, you also need to configure the JNDI parameters, such as Hibernate connecting to the database connection pool on the remote WebLogic:

Code: hibernate. dialect net. SF. hibernate. dialect. mysqldialect
Hibernate. Connection. datasource mypool

Hibernate. Connection. provider_class net. SF. hibernate. Connection. performanceconnectionprovider

Hibernate. JNDI. Class weblogic. JNDI. wlinitialcontextfactory

Hibernate. JNDI. url T3: // SERVERNAME: 7001/

Note: To use hibernate in EJB or JTA, you need to cancel the following comments:

Code: hibernate. transaction. factory_class net. SF. hibernate. transaction. jtatransactionfactory

E: Performance Tuning for databases with high concurrency and high crud frequencies is mainly for Oracle and MySQL clusters.
Hibernate. JDBC. fetch_size 50

Hibernate. JDBC. batch_size 25

These two options are exceptionally important-will seriously affect the crud performance of hibernate!

Specific understanding and explanation: Fetch size is the number of records retrieved from the database each time the JDBC Statement reads data. For example, a query of 10 thousand
For Oracle's JDBC driver, 10 thousand records will not be retrieved once, but the fetch size will only be retrieved.

After these records are traversed by the set, the database will fetch size data. Therefore, the unnecessary memory consumption is greatly reduced. Of course

The larger the fetch size is, the smaller the number of reads to the database, and the faster the speed is. The smaller the fetch size, the more reads the database.

Slow. This is a bit like writing a program to write a hard disk file, set up a buffer, and write the buffer every time. When the buffer is full,

Write Data to the hard disk at one time.
I will not explain batch in detail. I believe anyone who has used addbactch () will know it.

The default fetch size of the JDBC driver for Oracle database is 10, which is a very conservative setting. According to my tests, when fetch

When the size is 50, the performance will be doubled. When the fetch size is 100, the performance will continue to increase by 20%, and the fetch size will continue to increase.

, Performance improvement is not significant. Therefore, we recommend that you set the fetch size to 50 for Oracle. (Mainly for large data and multi-format data)

It is worth noting that not all databases support the fetch size feature, for example, MySQL does not. MySQL always extracts 10 thousand records from the above query, and the memory consumption will be very astonishing. But who is it that t is open source.

If you have written JDBC batch processing in detail, you will find that the insertion of large data volumes consumes a lot of time when executing the insert operation. After one of my tests: insert 100000 data records to MySQL. The speed of Hibernate is at least twice that of JDBC. In my last research, it turns out that hibernate uses batch insert, I did not use batch for jdbc I wrote. In my experience, the batch size of the Oracle database is suitable when it is set to 30. The value 50 is also good, and the performance will continue to improve. The value 50 is above and the performance improvement is very weak, instead, there is no need to consume more memory.
Note: It is worth noting that when using batch in JDBC, performance may not be significantly changed. It depends on the machine and database and cannot be easily concluded.

F: You can perform optimization by rolling the result set and cglib.
First, describe what is a scrollable result set: Generally, the result is returned after the code is executed in statement. Generally, the result is returned after the iteration, which is unnecessary sometimes, for example, if I want to extract the last 1000 records from the first 500th records, the next () iteration will waste the first 300 iterations, so it is necessary to scroll.

Code: # hibernate. JDBC. use_scrollable_resultset true

Set whether the jdbc2.0 standard rolling result set can be used, which has a certain effect on the paging display of hibernate. By default, the result set can be used.

Code: # hibernate. cglib. use_reflection_optimizer false

Tests show that cglib in spring can also be used for performance tuning. cglib and reflection editing in hibernate are enabled by default, and cglib reflection optimization is enabled. Cglib is used to dynamically generate Po bytecode in hibernate. Enabling optimization can speed up the construction of bytecode and improve the database speed.

G: Miscellaneous configurations

Code: hibernate. show_ SQL false

Whether to display the SQL statement that hibernate sends to the database is very useful. After debugging hibernate

So that hibernate can quickly find the problem by printing SQL statements.

Code: # hibernate. Connection. Isolation 4

Specify the database isolation level. Different databases usually have their own defined isolation levels, which may not be changed by hibernate settings.

So you don't have to worry about it. I will briefly introduce the isolation level issue: there are four types: uncommitted read, committed read, Repeatable read, and serializable, with the level increasing in turn.

To sum up, I think the performance tuning of the database depends on the actual situation. In the design, the time and space are balanced, even the database has the problem of space and time interchange to improve efficiency, so sometimes we cannot write a few hundred lines of code because we have to write too many lines of code. This is performance tuning or optimization, do not be lazy and be rigorous.

At last, my program debugging was much better than I expected: Start the factory 6.3 second, open the session 47 ms, and perform the first tuning from 27 seconds to 4 seconds, the second time I read it again for 1.2 seconds, I saw the dawn of progress.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.