C3p0

来源:互联网 发布:重庆管家婆软件代理商 编辑:程序博客网 时间:2024/06/02 13:03

所需文件:

1、 c3p0-0.9.1.2.jar       http://sourceforge.net/projects/c3p0/

2、  mysql.jar       http://dev.mysql.com/downloads/connector/j/5.0.html

3、  c3p0-0.9.1.2http://nchc.dl.sourceforge.net/sourceforge/c3p0/c3p0-0.9.1.2.src.zip(可选)

拥有以上三样东西就可以开始c3p0之旅了,把mysql.jar和c3p0-0.9.1.2.jar放到classpath中就可以开始编写我们的代码了。

 

C3p0最简单的使用方式就如其官网下所说的一样,只需提供driverName,url,user,password,程序就可以跑起来。

 

第一种获取数据源的方式:

 

Java代码  收藏代码
  1. ComboPooledDataSource cpds = new ComboPooledDataSource();  
  2.       String driverClass = "com.mysql.jdbc.Driver";  
  3.       String jdbcURL = "jdbc:mysql://localhost:3306/test";  
  4.       String user = "root";  
  5.       String password = "";  
  6.       cpds.setDriverClass(driverClass);  
  7.       cpds.setJdbcUrl(jdbcURL);  
  8.       cpds.setUser(user);  
  9.       cpds.setPassword(password);  
  10.       cpds.setMaxStatements(100);  
  11.       Connection conn = cpds.getConnection();  
 

正如简单的jdbc连接数据库一样仅仅只需要这些参数而已。

 

对于这种配置,如果classpath中有c3p0.properties的配置文件,代码中不需要设置连接信息,直接new ComboPooledDataSource(),他会自动读取配置文件中的配置。当然也可以使用c3p0-config.xml文件配置连接信息,使用xml作为配置信息的话,comboPoolDataSource还可以接受一个String参数,这个参数的名称是在c3p0-config.xml文件中配置,这就意味着我们可以在xml文件中可有都多个数据库源连接信息,比如可以是mysql,oracle的。

 

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>    <default-config>        <property name="initialPoolSize">10</property>        <property name="maxPoolSize">30</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/day14</property>        <property name="user">root</property>        <property name="password">root</property>    </default-config>        <named-config name="test">        <property name="initialPoolSize">10</property>        <property name="maxPoolSize">30</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/day14</property>        <property name="user">root</property>        <property name="password">root</property>    </named-config></c3p0-config> 这是c3p0的配置文件,其中上面那个config是默认的,下面的是你自己配置的,c3p0有两种创建方式,第一是:无参的,直接new;第二种,它的参数就是你配置文件中起的名字,如要用到下面那个,就new ComboPooledDataSource(“test”);这种方式下完全不需要你自己读取配置文件。

Java代码  收藏代码
  1. ComboPooledDataSource cpds = new ComboPooledDataSource(“test”);  
 

使用配置文件的方式连接时,c3p0默认在classpath根目录读取配置文件,如果想把配置文件放在自己想放得地方只需设置系统属性

 

Java代码  收藏代码
  1. System.setProperties(“ com.mchange.v2.c3p0.cfg.xml”,”config/c3p0-config.xml”);  

程序就在指定的目录下读取该配置文件。


第二种方式:

获取数据源,使用数据源工厂类DataSources

 

Java代码  收藏代码
  1. DataSource ds = DataSources.unpooledDataSource(jdbcURL, user, password);  
  2. DataSource pooledDateSource = DataSources.pooledDataSource(ds);  
  3.       System.out.println(pooledDateSource.getConnection());  
 

 

 

第三种获取数据源的方式:

 

Java代码  收藏代码
  1. PoolBackedDataSource backedDataSource = new PoolBackedDataSource();  
  2. backedDataSource.setConnectionPoolDataSource(new ConnectionPoolDataSource() );  
  3. //实现自己的connectionpooldatasource即可  
 

参数配置:

除了以上连接数据库必要的参数外,提供以下最基本的参数配置信息才能形成数据库连接池

1、    acquireIncrement  每次连接增加数量

2、    initalPoolSize 初始连接数

3、    maxPoolSize   最大连接数

4、    maxIdleTime   最大空闲数

5、    minPoolSize   池中连接最小数量

 

Tomcat中配置c3p0的方法:

1、server.xml中配置

 

Xml代码  收藏代码
  1. <GlobalNamingResources>  
  2.     <!-- Editable user database that can also be used by  
  3.         UserDatabaseRealm to authenticate users  
  4.    -->  
  5.    <Resource  name="jdbc/test"  
  6.                               auth="Container"  
  7.               description="User database that can be updated and saved"  
  8.       factory="org.apache.naming.factory.BeanFactory"  
  9.                               driverClass="com.mysql.jdbc.Driver"  
  10.                               maxPoolSize="4"  
  11.                               minPoolSize="2"  
  12.             acquireIncrement="1"  
  13.                       user="root"  
  14.                               password=""  
  15.                               type="com.mchange.v2.c3p0.ComboPooledDataSource"  
  16.                               jdbcUrl="jdbc:mysql://localhost:3306/test"  
  17.                    />  
  18.   </GlobalNamingResources>  
 

 

2、  conf目录下Context.xml

      <ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSource"/>

 

3、  web.xml

 

Xml代码  收藏代码
  1. <resource-ref>  
  2. <res-ref-name>jdbc/ test</res-ref-name>  
  3. <res-type>javax.sql.DataSource</res-type>  
  4.     <res-auth>Container</res-auth>  
  5. </resource-ref>  

 测试:

 

Java代码  收藏代码
  1. InitialContext context = new InitialContext();  
  2. return (DataSource) context.lookup("java:comp/env/jdbc/test");  
0 0
原创粉丝点击