Hibernate5.0 Configuring C3P0 connection pooling Steps

Source: Internet
Author: User
Tags connection pooling

HIBERNATE5.0 has its own database connection pool, but regardless of performance and function is not as good as c3p0 (i novice, check the information learned), because before the bitter search resources are not willing, so write this article for like I suffer from search novice, for reference.

The steps to implement connection pooling for Hibernate5.0 integration c3p0 are as follows:

The first Step first

1). To https://sourceforge.net/projects/hibernate/to the website you can download the jar required for hibernate5.0.

2). Then implement all the Hibernate jar packages in the import hibernate-release-5.0.12.final\lib\required directory,



All jar packages for c3p0 in the HIBERNATE-RELEASE-5.0.12.FINAL\LIB\OPTIONAL\C3P0 directory


And the packages required by hibernate to load the C3P0 service in the Hibernate-release-5.0.12.final\lib\optional\ehcache directory


3). In the Hibernate.cfg.xml configuration file, configure the C3p0 property:

 

Be sure to write the following line configuration, which specifies hibernate connection, otherwise hibernate will not use the C3P0 connection pool.

<property name= "Hibernate.connection.provider_class" >org.hibernate.connection.c3p0connectionprovider</ Property>

4). Check that the connection pool is working. In general, it is difficult for users to feel the gap before using connection pooling and after using connection pooling.

Here is an easy way to invoke the DoWork method of the session in the text class and output the connection in the method if the console displays

Com.mchange.v2.c3p0.impl.NewProxyConnection, your C3P0 configuration is successful and the code is as follows.


Additionally, the configuration properties of c3p0 are described below:


<!--c3p0 The number of connections that are fetched at the same time when the connection in the connection pool is exhausted. Default:3--<property name= "Acquireincrement" >3</property> <!--defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30---<property name= "acquireretryattempts" >30</property> <!--the interval between two connections, in milliseconds. default:1000---<property name= "Acquireretrydelay" >1000</property> < rollback of all uncommitted operations by default when the connection is closed. Default:false--<property name= "Autocommitonclose" >false</property> <!--c3p0 will build a blank table named Test, and test it with its own query statement. If this parameter is defined, then the attribute preferredtestquery will be ignored. You cannot do anything on this test sheet, it will be used only for C3P0 testing. default:null--> <property name= "automatictesttable" >Test</property> <!-- Getting a connection failure will cause any thread that waits for the connection pool to get the connection to throw an exception. However, the data source is still valid and continues to try to get the connection the next time you call Getconnection (). If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. default:false--> <property name= "Breakafteracquirefailure" >false</property> <!-- When the connection pool runs out, the client calls getconnection () to wait for a new connection, and after the timeout, the SQLException is thrown and, if set to 0, waits indefinitely. Unit milliseconds. default:0--<property name= "Checkouttimeout" >100</property> <!--test the connection by implementing a class of Connectiontester or Queryconnectiontester.
The class name needs to be set to the full path. default:com.mchange.v2.c3p0.impl.defaultconnectiontester--> <property name= "ConnectionTesterClassName" > </property> <!--Specify the path to the C3P0 libraries, if (usually it is) locally you can get it without setting, default NULL to Default:null--> <property name= "Factoryclasslocation" >null</property> <!--strongly disrecommended.
Setting this to true may leads to subtle and bizarre bugs. Author strongly recommends not to use one of the properties--<property name= "Forceignoreunresolvedtransactions" >false</property> <!- -Check for idle connections in all connection pools every 60 seconds. default:0---<property name= "Idleconnectiontestperiod" >60</property> <!--get three connections when initializing, The value should be between Minpoolsize and Maxpoolsize. Default:3---<property name= "Initialpoolsize" >3</property> <!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. default:0---<property name= "MaxIdleTime" >60</property> <!--the maximum number of connections left in the connection pool. Default:15--<property name= "Maxpoolsize" >15</property> <!--JDBStandard parameter of C to control the number of preparedstatements loaded within the data source. However, because the pre-cached statements belong to a single connection instead of the entire connection pool.
So setting this parameter takes into account a variety of factors. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0--> <property name= "maxstatements" >100</property> <!-- Maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. default:0--<property name= "Maxstatementsperconnection" ></property> <!--c3p0 is asynchronous operation, Slow JDBC operations are done through the help process. Extending these operations can effectively improve performance by implementing multiple operations at the same time through multithreading. default:3--> <property name= "Numhelperthreads" >3</property> <!--when user calls getconnection () The root user becomes the user who is going to get the connection. Used primarily when connection pooling is connected to a non-c3p0 data source. default:null--> <property name= "Overridedefaultuser" >root</property> <!-- A parameter that corresponds to the Overridedefaultuser parameter. default:null--> <property name= "Overridedefaultpassword" >password</property> <!--password. default:null--> <property name= "password" ></property> <!--define test statements that are executed by all connection tests. This one significantly improves the test speed in the case of connection testing. Note: The test table must exist at the time of the initial data source. default:null--> <property name= "preFerredtestquery ">select ID from test where id=1</property> <!--users to wait up to 300 seconds before modifying system configuration parameters to execute. default:300--<property name= "propertycycle" >300</property> <!--because of the high performance consumption please use it only when you need it. If set to true then the validity of each connection submission is officer. We recommend using methods such as Idleconnectiontestperiod or automatictesttable to improve the performance of your connectivity tests. Default:false--<property name= "Testconnectiononcheckout" >false</property> <!-- If set to true then the validity of the officer connection is obtained while the connection is made. Default:false--<property name= "Testconnectiononcheckin" >true</property> <!--user name. default:null--> <property name= "user" >root</property> configuration in Hibernate (Spring Administration): <bean id= " DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "> <property name=" Driverclass "><value>oracle.jdbc.driver.OracleDriver</value></property> <property name=" Jdbcurl "><value>jdbc:oracle:thin: @localhost:1521:test</value></property> <property name= "User" ><value>Kay</value></property> <property name= "password" ><value>root</value></property> < !--the minimum number of connections that are kept in the connection pool. --<property name= "minpoolsize" value= "/> <!--the maximum number of connections left in the connection pool. Default:15---<property name= "maxpoolsize" value= "Up"/> <!--maximum idle time, unused in 1800 seconds, the connection is discarded. If 0, it will never be discarded. default:0--<property name= "MaxIdleTime" value= "1800"/> <!--when the connection in the connection pool runs out, c3p0 the number of connections fetched at the same time. Default:3--<property name= "Acquireincrement" value= "3"/> <property name= "maxstatements" value= "1000"/&
Gt <property name= "Initialpoolsize" value= "ten"/> <!--check all connection pools for idle connections every 60 seconds. default:0--<property name= "Idleconnectiontestperiod" value= "$"/> <!--defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30--<property name= "acquireretryattempts" value= ""/> <property name= " Breakafteracquirefailure "value=" true "/> <property name=" Testconnectiononcheckout "value=" false "/> </ Bean> Add the following configuration in the Hibernate.cfg.xml file: <!--maximum number of connections--> <property name= "Hibernate.c3p0.max_size" >20</property> <!--minimum number of connections and <property name= " Hibernate.c3p0.min_size ">5</property> <!--get the connection timeout, if it exceeds this time, an exception will be thrown, in milliseconds--and <property name=" Hibernate.c3p0.timeout ">120</property> <!--the largest number of PreparedStatement and <property name=" Hibernate.c3p0.max_statements ">100</property> <!--check the connection pool for idle connections every 120 seconds, in seconds--<property name=" Hibernate.c3p0.idle_test_period ">120</property> <!--when the connection pool is out of use, c3p0 get the new number of connections--<property Name= "Hibernate.c3p0.acquire_increment" >2</property> <!--Verify that the connection is available every time-<property name= " Hibernate.c3p0.validate ">true</property>



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.