If you configure Hibernate to connect to the database environment, we can set up an environment that uses C3P0 data sources in Hibernate, for example, in the following operations.
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 (added in Hibernate.cfg.xml file):
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 is checking the thread to see if all the linked objects in the pool have timed out. The connection pool itself does not remove itself from the connection pool. Instead, there is a thread that does this at a certain time interval, which is compared to timeout by comparing the last elapsed time of the connection object with the current time, and then decides whether to destroy the connection object. Hibernate.c3p0.max_statements: Number of cached Statement objects
The following is the original code for the added configuration: The following code is added to 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 were able to see if the build was successful by writing a test example: Create a JUnit Test example, make a DoWork method in the test example to invoke the Connection object in JDBC, and then print 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 now been successfully built.
[Email protected]
Hibernate using C3P0 data sources