Hibernate integrates C3P0 to implement connection pool

Source: Internet
Author: User

The default connection pool can be used in Hibernate. No matter the function and performance are not as good as C3PO (I have not tested it), C3P0 is an open source JDBC connection pool, it implements data source and JNDI binding and supports standard extensions of JDBC3 specifications and JDBC2. Currently, open-source projects using it include Hibernate and Spring.

C3P0 is a feature enhancement that makes it easy to use the JDBC3 specification and the optional extensions of JDBC2, enabling the traditional JDBC driver "enterprise-ready" library. In particular, C3P0 provides some useful services: to adapt to the traditional scheme of obtaining database connections based on the new javax. SQL. DataSource class of the JDBC driver in DriverManager.

The data sources behind transparency and PreparedStatement can be packaged around the connection pool of traditional drivers or any UNPOOLED data sources. C3p0 data sources are referenced and serialized, so it is suitable for combining various JNDI-based naming services. When Statement and ResultSet are mainly cleaned up, the connection pool and report are checked to prevent resource depletion. when the client is used lazily, but the common resource management policy, only clear your own connections ....

This library uses the JDBC 2 and 3 specification definition methods. All are written in a JavaBean-style data source, providing all required and most important optional attributes (and some nonstandard) without parameter construction. The implementation of all the internal interfaces defined by JDBC (the generation of the poonpooldatasource PooledConnection ConnectionEvent; connection, etc.) is compatible with third-party implementations (although not all c3p0 functions will be implemented externally ), you can mix C3P0 classes. C3P0 wants to provide DataSource for many high-capacity "JEE enterprise applications ".

Requirements:C3P0 requires Java Runtime Environment 1.3.x or later, and javax. SQL libraries JDBC2.x or later. C3P0 works normally. Version 1.4.x and 1.5.x are better.

In Hibernate integration, C3P0 implements the connection pool as follows:

Step 1: Download the source code and help document and jar package at http://sourceforge.net/projects/c3p0/files.

Figure 1

Step 2, add a reference to the project and find the lib/c3p0-0.9.2.1.jar In the downloaded file.

Step 3: Modify the hibernate. cfg. xml configuration file and add attributes. Hibernage will automatically find the content you configured and select c3p0 as the connection pool.

                    org.hibernate.connection.C3P0ConnectionProvider        20        5        120        Test        100        120        1        true        18000        25000        120                        root        root                testDb        true                               

Remember to write <property name = "hibernate. connection. provider_class "> org. hibernate. connection. c3P0ConnectionProvider </property>, which is used to specify the connection Provision Method of Hibernate. If not, c3p0 will not be used as the Hibernate connection pool.

Step 4: Check whether the connection pool works

Generally, the connection pool is not used or used, and it is hard for users to feel that there are two ways to use MySql as an example:

Method 1: Modify log4j. properties of log4j and modify log4j. rootLogger = Info. This reduces the record and display log levels. If you can see the [org. hibernate. connection. C3P0ConnectionProvider] icon when running the program, Hibernate has selected C3P0 when connecting to the database.

Method 2: run the show processlist command in MySQL to view the number of connections. If the connection pool is configured with a minimum of five connections, the following information is displayed:

 

In addition, the attributes of C3P0 configuration are described as follows:

3301000falseTestfalse100nullfalse60360151003rootpasswordselect id from test where id=1300falsetruerootoracle.jdbc.driver.OracleDriverjdbc:oracle:thin:@localhost:1521:TestKayroot          2051201001202true

 

Related Article

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.