Basic hibernate configurations

Source: Internet
Author: User

Basic hibernate configurations
The Hibernate configuration file can be in either hibernate. properties or hibernate. cfg. XML format.

The latter is a little more convenient. When adding an HBM ing file, you can add it directly in hibernate. cfg. xml without adding it to the initialization code like hibernate. properties.

However, in any case, the two configuration items are the same. the following details:

There is a hibernate. properties Template under the src directory of hibernate. We don't have to write it from the beginning, just modify the template :)

Hibernate. query. Substitutions true 1, false 0, yes 'y', no 'n'

This configuration means that when you input true in hibernate, Hibernate will convert to 1 to insert the database. When you input false in hibernate, Hibernate will convert to 0 to insert 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.

Hibernate. dialect net. SF. hibernate. dialect. mysqldialect
Hibernate. Connection. driver_class com. MySQL. JDBC. Driver
Hibernate. Connection. url JDBC: mysql: // Test
Hibernate. Connection. Username Root
Hibernate. Connection. Password

This is an example of connecting to the MySQL database. It is intuitive and does not need to be explained. All the connection parameter templates for different databases are provided.

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 default. Meaning is very intuitive, not much explanation.

Just to remind you that the hibernate connection pool is very primitive and simple. If you use hibernate in your project, we recommend that you first select the app server connection pool and the DBCP connection pool with hibernate. The built-in connection pool should be selected as the final choice.

If you use the DBCP connection pool, in addition to configuring the DBCP connection pool, you also need to cancel the comment of the following line:

Hibernate. Connection. provider_class net. SF. hibernate. Connection. dbcpconnectionprovider

The same applies to other connection pools.

If the connection pool of APP server is used and the datasource name of the connection pool of APP server is "mypool", the configuration should be as follows:

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.

If you are not using hibernate in the app server environment, such as a remote client program, but you want to use the database connection pool of the app server, you also need to configure the JNDI parameter, for example, Hibernate connects to the database connection pool on the remote WebLogic:

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/

Finally, if you need to use hibernate in EJB or JTA, you need to cancel the following comments:

Hibernate. transaction. factory_class net. SF. hibernate. transaction. jtatransactionfactory

Miscellaneous Configuration:

Hibernate. show_ SQL false

Whether to display the SQL statement that hibernate sends to the database is very useful. When debugging hibernate, letting hibernate print SQL statements can help you solve the problem quickly.

# Hibernate. Connection. Isolation 4

When specifying the database isolation level, different databases usually have their own defined isolation level, which may not be changed by hibernate settings, so you don't have to worry about it.

Hibernate. JDBC. fetch_size 50
Hibernate. JDBC. batch_size 25

These two options are very important !!! It will seriously affect the crud performance of hibernate!

C = create, r = read, u = Update, D = Delete

Fetch size is the number of records retrieved from the database each time the JDBC Statement reads data.

For example, if you Query 10 thousand records at a time, the JDBC driver of Oracle will not obtain 10 thousand records at a time, but will only retrieve the number of fetch size records, after these records are traversed by the record 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, the faster the speed. The smaller the fetch size, the more reads the database, and the slower the speed.

This is a bit like writing a program to write a hard disk file, setting up a buffer, writing a buffer each time, and writing a hard disk once after the buffer is full. The same principle applies.

The default fetch size of the JDBC driver of Oracle Database is 10, which is a very conservative setting. According to my test, when fetch size is 50, the performance will be doubled, when fetch size = 100, the performance can continue to increase by 20%, and the fetch size will continue to increase, so the performance improvement will not be significant.

Therefore, we recommend that you set the fetch size to 50 for Oracle.

However, not all databases support the fetch size feature. For example, MySQL does not.

MySQL is like the worst case I mentioned above. He always extracts 10 thousand records at once, and the memory consumption will be very amazing! There is no good way to do this.

Batch size is the batch size set for database batch deletion, batch update and batch insertion, which is a bit equivalent to setting the buffer size.

The larger the batch size, the less times the batch operation sends SQL statements to the database, the faster the speed. One test result I made was that when batch size = 0, it would take 25 seconds to delete 10 thousand records from the Oracle database using hibernate. When batch size = 50, it takes only 5 seconds to delete it !!!

We can see how much performance is improved! Many people may find that the speed of Hibernate is at least twice that of JDBC, because hibernate uses batch insert, the JDBC they wrote did not use batch.

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.

# 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.

# Hibernate. cglib. use_reflection_optimizer false

By default, cglib reflection optimization is enabled. Cglib is used to dynamically generate Po bytecode in hibernate. Enabling optimization can speed up the construction of bytecode.

However, when you encounter code errors during program debugging, especially in applications related to proxy and lazy loading, but the error message is ambiguous, you can disable cglib optimization, in this way, Hibernate will output more detailed debugging information to help you debug
 
 

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.