Hibernate using C3P0 data sources

Source: Internet
Author: User

If you configure Hibernate to connect to the database environment, we can set up the environment for using C3P0 data source in hibernate in the following operation. 1). Import the jar package:
Hibernate-release-4.2.4.final\lib\optional\c3p0\*.jar (There are usually 3 jar packages in it .) C3p0-0.9.2.1.jar
Hibernate-c3p0-4.2.15.final.jar
Mchange-commons-java-0.2.3.4.jar

2). Join the configuration (The Hibernate.cfg.xml file is added):
Hibernate.c3p0.max_size: Maximum number of connections to the database connection pool hibernate.c3p0.min_size: The minimum number of connections for the database connection pool hibernate.c3p0.acquire_increment: How many database connections are obtained at the same time when the connection in the database connection pool is exhausted Hibernate.c3p0.timeout: The database connection pool should be destroyed if the connection object has not been used for a long time hibernate.c3p0.idle_test_ Period Indicates how long the connection pool detection thread is detecting whether all linked objects in the pool are timed out. The connection pool itself does not remove itself from the connection pool, but rather a thread that does this at a certain interval by comparing the last time the connection object was used and the current time difference Timeout to compare and decide whether to destroy the connection object. Hibernate.c3p0.max_statements: Number of cached Statement objects
Here is the original code to add the configuration: The following code is added in the Hibernate.cfg.xml file
</pre><pre name= "code" class= "HTML" ><!--configuration c3p0 data source--><property name= "Hibernate.c3p0.max_size" >10</property><property name= "hibernate.c3p0.min_size" >2</property><property name= " C3p0.acquire_increment ">2</property><property name=" C3p0.idle_test_period ">2000</property> <property name= "C3p0.timeout" >2000</property><property name= "c3p0.max_statements" >10</ Property>

We can write test cases to see if the build is successful: Create JUnit test Cases, replicate DoWork methods in test cases to invoke connection objects in JDBC, and print them out
@Testpublic void Testdowork () {session.dowork (new work () {@Overridepublic void execute (Connection Connection) throws SQLException {System.out.println (connection);//Call stored procedure.}});

If printed, the following code with C3P0 indicates that it has been successfully built.
[Email protected]


Hibernate using C3P0 data sources

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.