Spring C3P0 Connection Pool configuration

Source: Internet
Author: User

Database Connection Pool
The basic idea of a database connection pool is to establish a "buffer pool" for database connections. A certain number of connections are pre-placed in the buffer pool, and when a database connection needs to be established, simply take one out of the buffer pool and put it back when you are finished. We can prevent the system from endlessly connecting to the database by setting the maximum number of connections to the connection pool.

Get a connection, the system to do a lot of resources in the back of things, most of the time, the creation of a connection is longer than the time to execute the SQL statement.
Each time a user requests a link to the database, the database creation connection typically consumes a relatively large amount of resources and is created longer.

The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing one.

C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. C3P0 is generally used with a frame such as hibernate,spring, and of course it can be used alone.
DBCP does not automatically reclaim idle connections, C3P0 has the ability to automatically reclaim idle connections.

The first way: Configure with an XML file

<?XML version= "1.0" encoding= "UTF-8"?>  <C3p0-config>    <Default-config>        < Propertyname= "Driverclass" >Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Jdbcurl" >Jdbc:mysql://localhost:3306/mydb?characterencoding=gbk</ Property>        < Propertyname= "User" >Root</ Property>        < Propertyname= "Password" ></ Property>                < Propertyname= "Maxpoolsize" >15</ Property>        < Propertyname= "Minpoolsize" >5</ Property>        < Propertyname= "Initialpoolsize" >5</ Property>    </Default-config></C3p0-config>  

The second way: Configure with the properties file

C3p0.jdbcurl=jdbc:mysql://localhost:3306/mydb  c3p0.driverclass=com.mysql.jdbc.driver    c3p0.user=root    c3p0.password=     c3p0.acquireincrement=3     c3p0.idleconnectiontestperiod=60        c3p0.initialpoolsize= Ten     c3p0.maxidletime=60     c3p0.maxpoolsize=20     c3p0.maxstatements=100      c3p0.minpoolsize=5    

Call the main function to read the configuration

 Public Static void throws Exception {                                new  combopooleddatasource ();                Connection conn=cb.getconnection ();        System.out.println (conn.isclosed ());        Conn.close ();                    }

Return the result to False

Use the following experiment to verify the role of the connection pool:

 Packagecom.test;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.util.Calendar;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classTest { Public Static voidmain1111 (string[] args)throwsException {LongStart =calendar.getinstance (). Gettimeinmillis ();  for(inti=0; i<100;i++) {Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb", "Root", "" ");        System.out.println (conn.isclosed ());        Conn.close (); }        LongEnd =calendar.getinstance (). Gettimeinmillis (); System.out.println (End-start); }     Public Static voidMain (string[] args)throwsException {LongStart =calendar.getinstance (). Gettimeinmillis (); Combopooleddatasource CB=NewCombopooleddatasource ();  for(inti=0; i<100;i++) {Connection conn=cb.getconnection ();        System.out.println (conn.isclosed ());        Conn.close (); }        LongEnd =calendar.getinstance (). Gettimeinmillis (); System.out.println (End-start); }}

The main1111 function is the most basic way to connect to the database, the main function is to connect the database with a connection pool, the same loop 100 times the first result is 8000 milliseconds, the second is about 1000 milliseconds.

Since the connection pool can be configured through the XML file and the properties file, we can also configure it in another way:

(1) defined directly in the main function

 public  static  void  main11 (string[] args) throws   Exception {combopooleddatasource cd  = new   Combopooleddatasource ();        Cd.setdriverclass ( "Com.mysql.jdbc.Driver"  "Jdbc:mysql://localhost:3306/mydb"  "root"  ""  5 10 = cd.getconnection (); System.out.println (conn.isclosed ()); Conn.close (); }

(2) Read in XML by reading the properties configuration using ${}

driverclass=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/mydb? CHARACTERENCODING=GBKuser=rootpassword=minpoolsize=5maxpoolsize =15  Initialpoolsize=5
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.3.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd " >        <Context:property-placeholder Location= "Classpath:db.properties"/>        <Beanclass= "Com.mchange.v2.c3p0.ComboPooledDataSource"ID= "DataSource">        < Propertyname= "Driverclass"value= "${driverclass}"></ Property>        < Propertyname= "Jdbcurl"value= "${jdbcurl}"></ Property>        < Propertyname= "User"value= "${user}"></ Property>        < Propertyname= "Password"value= "${password}"></ Property>                < Propertyname= "Minpoolsize"value= "${minpoolsize}"></ Property>        < Propertyname= "Maxpoolsize"value= "${maxpoolsize}"></ Property>        < Propertyname= "Initialpoolsize"value= "${initialpoolsize}"></ Property>    </Bean>    </Beans>
 Public Static void throws Exception {        new classpathxmlapplicationcontext ("Beans.xml");         = (DataSource) context.getbean ("DataSource");         = d.getconnection ();        System.out.println (conn.isclosed ());        Conn.close ();    }

Spring C3P0 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.