Java Web----C3P0

Source: Internet
Author: User

1 C3P0 Introduction

C3P0 is also open source free connection pool! C3P0 is favored by many people!

2 Use of C3P0

Package Com.cug.c3p0;import Java.beans.propertyvetoexception;import Java.sql.connection;import Java.sql.sqlexception;import Org.junit.test;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3P0Demo {@Testpublic void Fun1 () throws Propertyvetoexception, Sqlexception{combopooleddatasource DataSource = new Combopooleddatasource ();d atasource.setuser ("root");d Atasource.setpassword ("123");d Atasource.setjdbcurl ("JDBC: MYSQL://LOCALHOST:3306/MYDB1 ");d atasource.setdriverclass (" Com.mysql.jdbc.Driver ");d Atasource.setmaxpoolsize (30 );d atasource.setminpoolsize;d atasource.setinitialpoolsize;d atasource.setmaxidletime (3000); Datasource.setacquireincrement (5); Connection conn = Datasource.getconnection ();//[email PROTECTED]SYSTEM.OUT.PRINTLN (conn); Conn.close ();}}

Configuration file Requirements:

    • File name: Must be called C3p0-config.xml
    • File location: Must be under SRC

C3P0 can also specify a configuration file, and the configuration file can be either properties or close XML. Of course, XML is a bit more advanced. However, the configuration file name for C3P0 must be c3p0-config.xml and must be placed under the classpath.

<?xml version= "1.0" encoding= "UTF-8"? ><c3p0-config><default-config><property name= "JdbcUrl" >jdbc:mysql://localhost:3306/mydb1</property><property name= "Driverclass" >com.mysql.jdbc.Driver </property><property name= "user" >root</property><property name= "password" >123</ Property><property name= "acquireincrement" >3</property><property name= "InitialPoolSize" >10 </property><property name= "minpoolsize" >2</property><property name= "MaxPoolSize" >10</ Property></default-config><named-config name= "Oracle-config" ><property name= "JdbcUrl" &GT;JDBC: Mysql://localhost:3306/mydb1</property><property name= "Driverclass" >com.mysql.jdbc.driver</ Property><property name= "user" >root</property><property name= "password" >123</property ><property name= "acquireincrement" >3</property><property name= "InitialPoolSize" >10</ Property>&lT;property name= "minpoolsize" >2</property><property name= "Maxpoolsize" >10</property></ Named-config></c3p0-config>

public void Fun2 () throws Propertyvetoexception, SQLException {combopooleddatasource ds = new Combopooleddatasource (); Connection con = ds.getconnection (); System.out.println (con); Con.close ();} public void Fun2 () throws Propertyvetoexception, SQLException {combopooleddatasource ds = new Combopooleddatasource (" Orcale-config "); Connection con = ds.getconnection (); System.out.println (con); Con.close ();}




Java Web----C3P0

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.