您的位置:首页 > 编程语言 > Java开发

java--开源数据库连接池(c3p0数据源)

2016-11-21 22:28 375 查看
现在很多WEB服务器(Weblogic, WebSphere, Tomcat)都提供了DataSoruce的实现,即连接池的实现。通常我们把DataSource的实现,按其英文含义称之为数据源,数据源中都包含了数据库连接池的实现。

也有一些开源组织提供了数据源的独立实现:

DBCP 数据库连接池

C3P0 数据库连接池

实际应用时不需要编写连接数据库代码,直接从数据源获得数据库的连接。程序员编程时也应尽量使用这些数据源的实现,以提升程序的数据库访问性能。

c3p0数据源

应用程序应在系统中增加如下一个 jar 文件:

c3p0-0.9.1.2.jar:连接池实现的依赖库

c3p0数据库连接池的两种实现方式

package cn.hncu.c3p0;

import java.sql.Connection;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0PoolDemo {

@Test//法1:纯Java
public void demo1() throws Exception{
ComboPooledDataSource pool=new ComboPooledDataSource();
pool.setDriverClass("com.mysql.jdbc.Driver");
pool.setUser("root");
pool.setPassword("1234");
pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8");
//      Connection con=pool.getConnection();
//      System.out.println("con:"+con);//com.mchange.v2.c3p0.impl.NewProxyConnection@491bcb51
pool.setMaxPoolSize(15);//默认可拿到最多的连接是10个,这句可以更改拿到连接的最大数量
for(int i=0;i<20;i++){//默认是10个,输出15个之后阻塞
Connection con=pool.getConnection();
System.out.println("con:"+con);//com.mchange.v2.c3p0.impl.NewProxyConnection@491bcb51
if(i%2==0){//c3p0获取连接都是新的,不管池中存不存在被关了的旧连接
con.close();
}
}

}
//法2:使用配置文件---注意,c3p0的配置文件名及存放位置是固定的(内部写死了): c3p0-config.xml,放在classpath下
@Test//c3p0的资源文件必须放在src目录下,且资源文件的名字是固定的c3p0-config.xml(内部数据库根据需要更改)
public void demo2() throws Exception{
ComboPooledDataSource pool=new ComboPooledDataSource("hncu");
System.out.println("con:"+pool.getConnection().hashCode());
for(int i=0;i<20;i++){
Connection con = pool.getConnection();
System.out.println(con.hashCode());
}
}
}


c3p0-config.xml–c3p0的资源文件必须放在src目录下,且资源文件的名字是固定的c3p0-config.xml(内部数据库根据需要更改)

<c3p0-config>
<!-- 默认配置,如果没有指定则使用这个配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
<![CDATA[jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8]]>
</property>
<property name="user">root</property>
<property name="password">1234</property>
<!-- 初始化池大小 -->
<property name="initialPoolSize">2</property>
<!-- 最大空闲时间 -->
<property name="maxIdleTime">30</property>
<!-- 最多有多少个连接 -->
<property name="maxPoolSize">10</property>
<!-- 最少几个连接 -->
<property name="minPoolSize">2</property>
<!-- 每次最多可以执行多少个批处理语句 -->
<property name="maxStatements">50</property>
</default-config>
<!-- 命名的配置 -->
<named-config name="hncu">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
<![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]>
</property>
<property name="user">root</property>
<property name="password">1234</property>
<property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="initialPoolSize">10</property>
<property name="minPoolSize">3</property>
<property name="maxPoolSize">10</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
</named-config>
</c3p0-config>


开源数据库连接池

package cn.hncu.c3p0;

import java.sql.Connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Utils {
//本地线程管理对象,用于实现: 同一个线程获取的连接是同一个
private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();

private static ComboPooledDataSource pool = null;
static{
try {
//          pool=new ComboPooledDataSource();//使用默认配置项
pool=new ComboPooledDataSource("hncu");
} catch (Exception e) {
e.printStackTrace();
}
}

public static Connection getConn() {
//先从t中拿,如果有就拿出去,如果没有再到池中拿且把该对象放到t中
Connection con = t.get();
if(con==null){
try {
con=pool.getConnection();
t.set(con); //放到t中
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
System.out.println("获取一个连接:"+con);
return con;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息