Some points to note about C3P0 connection pooling connection to MySQL database

Source: Internet
Author: User
Tags connection pooling manage connection mysql version

What is a database connection pool:

Use pools to manage connection, which can be reused with connection. With the pool, we don't have to create connection ourselves, but instead we get the connection object through the pool.

When connection is finished, calling connection's close () method does not actually close the connection, but instead connection "returns" to the pool. The pool will be able to use this connection object again.

Import dbutils Toolkit: Commons-dbutils-1.6.jar

Commons-dbutils is an open source JDBC Tool class library provided by Apache, which is a simple encapsulation of JDBC, very low learning costs, and the use of dbutils can greatly simplify the work of JDBC coding without compromising the performance of the program. As a result, Dbutils is the first choice for many companies that do not like hibernate .

Import C3P0 Toolkit: C3p0-0.9.1.2.jar

To configure C3P0 first, most of the current uses are configured with the C3p0-config.xml file:

  The ① name must be c3p0-config.xml.

② must be placed in the SRC root directory, remember not to put together with Web. Xml.

This is the C3P0 configuration:

<default-config> As the default configuration, you can also add <named-config name= "Mysqlconfig", where Mysqlconfig is your own configuration name.

In this case, create the Combopooleddatasource instance in the C3p0 tool class to add your configuration name, namely: New Combopooleddatasource ("mysqlconfig")

Ii

#Initialpoolsize: Number of connections created when connection pooling is initialized, Default:3, value should be between Minpoolsize and Maxpoolsize c3p0.initialpoolsize= 10

#Minpoolsize: The minimum number of connections maintained by the connection pool, Default:3 c3p0.minpoolsize=10

#Maxpoolsize: The maximum number of connections that are available in a connection pool, and if a new connection is made, the total number of connections exceeds this value no longer acquires new connections, but waits for other connections to be released, so this value may be designed to be large, default:15 C3p0.maxpoolsize=50

#acquireincrement: Connection pool The number of new database connections created one time when no idle connection is available, Default:3 c3p0.acquireincrement=5

Driverclass represents your database driver class, what database do you use

Jdbcurl represents the database you want to connect to, automotic_sign the name of the database you created here.

The following parameters ? Usessl=true Indicates whether to connect to the database with SSL, this parameter is not necessary, when the MySQL version is relatively high, the connection will be prompted when the information.

User and password are the username and password for your database

<?XML version= "1.0" encoding= "UTF-8"?><C3p0-config>  <Default-config>     < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>      < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/automotic_sign?usessl=true</ Property>      < Propertyname= "User">Root</ Property>      < Propertyname= "Password">Root</ Property>      < Propertyname= "Initialpoolsize">5</ Property>      < Propertyname= "Minpoolsize">2</ Property>     < Propertyname= "Acquireincrement">3</ Property>     < Propertyname= "Maxpoolsize">10</ Property>   </Default-config></C3p0-config>

To create the C3p0 tool class and the Dbutils tool class:

 PackageCom.jdbc;Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.sql.DataSource;ImportOrg.apache.commons.dbutils.QueryRunner;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {Private StaticCombopooleddatasource DataSource;//Create a C3P0 connection, the entire project has a connection pool on it, set to static as long as you instantiate it once    Static{DataSource=NewCombopooleddatasource (); }         Public StaticDataSource Getdatasource () {returnDataSource; }     Public StaticQueryrunner Getqueryrunner () {//creating objects for dbutils Common tool class Queryrunner        return NewQueryrunner (DataSource); }       Public StaticConnection getconnection () {Try{Connection Connection=datasource.getconnection (); returnconnection; } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); Throw Newruntimeexception (); }    }    }

Some points to note about C3P0 connection pooling connection to MySQL database

Related Article

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.