SPRING+C3P0 Database Connection Pool configuration

Source: Internet
Author: User

One, the XML file reads the. properties file to connect to the database

1. Configuration in XML file

<bean id= "datasourcelocal" name= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<!--Specify the drive to connect to the database
<property name= "Driverclass" value= "${jdbc.driverclassname}"/>
<!--specify url--> to connect to the database
<property name= "Jdbcurl" value= "${jdbc.url}"/>
<!--Specify the user name to connect to the database--
<property name= "user" value= "${jdbc.username}"/>
<!--specify a password to connect to the database--
<property name= "Password" value= "${jdbc.password}"/>
<!--Specifies the maximum number of connections that are kept in the connection pool. Default:15-->
<property name= "maxpoolsize" value= "${jdbc.maxpoolsize}"/>
<!--Specify the minimum number of connections that are kept in the connection pool--
<property name= "minpoolsize" value= "${jdbc.minpoolsize}"/>
<!--the number of initialization connections for the specified connection pool should be between Minpoolsize and Maxpoolsize. Default:3-->
<property name= "initialpoolsize" value= "${jdbc.initialpoolsize}"/>
<!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. Default:0-->
<property name= "MaxIdleTime" value= "${jdbc.maxidletime}"/>
<!--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" value= "${jdbc.acquireincrement}"/>
<!--JDBC Standard 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, setting this parameter takes into account a number of factors. If both maxstatements and maxstatementsperconnection are 0, The cache is closed. Default:0-->
<property name= "maxstatements" value= "${jdbc.maxstatements}"/>
<!--check for idle connections in all connection pools every 60 seconds. Default:0--
<property name= "Idleconnectiontestperiod" value= "${jdbc.idleconnectiontestperiod}"/>
</bean>

2. The Properties Property file

Jdbc.initialpoolsize=20
jdbc.maxpoolsize=100
jdbc.minpoolsize=10
jdbc.maxidletime=600
Jdbc.acquireincrement=5
Jdbc.maxstatements=5
Jdbc.idleconnectiontestperiod=60

Second, directly set up the data connection database in the XML file

<bean id= "DataSource"
Class= "Com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method= "Close" >
<property name= "Driverclass" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name= "Jdbcurl" >
<value>
Jdbc:mysql://localhost:3306/dbname?useunicode=true&characterencoding=gbk
</value>
</property>
<!--the user name of the MySQL database--
<property name= "User" >
<value>root</value>
</property>
<!--mysql Database password--
<property name= "Password" >
<value>root</value>
</property>
<!--the minimum number of connections that are kept in the connection pool. -
<property name= "Minpoolsize" >
<value>5</value>
</property>
<!--The maximum number of connections that are kept in the connection pool. Default:15--
<property name= "Maxpoolsize" >
<value>30</value>
</property>
<!--the number of connections obtained when initializing, the value should be between Minpoolsize and Maxpoolsize. Default:3--
<property name= "Initialpoolsize" >
<value>10</value>
</property>
<!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. Default:0--
<property name= "MaxIdleTime" >
<value>60</value>
</property>
<!--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" >
<value>5</value>
</property>
<!--the standard parameters of JDBC 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" >
<value>0</value>
</property>
<!--check for idle connections in all connection pools every 60 seconds. Default:0--
<property name= "Idleconnectiontestperiod" >
<value>60</value>
</property>
<!--defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30--
<property name= "Acquireretryattempts" >
<value>30</value>
</property>
<!--getting a connection failure will cause any thread that waits for the connection pool to get the connection to throw an exception. But the data source is still valid
Reserved and continue trying to get the connection the next time you call Getconnection (). If set to true, then try to
When a connection failure is obtained, the data source declares that it has been fractured and closed permanently. Default:false-->
<property name= "Breakafteracquirefailure" >
<value>true</value>
</property>
<!--use it only when you need it, due to its high performance consumption. If set to true then each connection commits the
Officer the validity of the test. It is recommended to use Idleconnectiontestperiod or automatictesttable
and other methods to improve the performance of the connection test. Default:false--
<property name= "Testconnectiononcheckout" >
<value>false</value>
</property>
</bean>

SPRING+C3P0 Database Connection Pool configuration

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.