您的位置:首页 > 其它

按照网上的文章写了份c3p0连接池求各位批评指点

2016-05-05 14:57 253 查看
项目结构如图



在src下游一个c3p0的配置文件:c3p0-config.xml     配置文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- This is default config! -->
<default-config>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>

<!-- This is my config for mysql-->
<named-config name="mysql">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl">jdbc:mysql://xxx.xx.xxx.xx:3307/golvon?characterEncoding=utf-8</property>
<!-- 指定连接数据库的用户名 -->
<property name="user">xxxx</property>
<!-- 指定连接数据库的密码 -->
<property name="password">xxxx</property>
<!-- 指定连接池中保留的最大连接数. Default:15 -->
<property name="maxPoolSize">100</property>
<!-- 指定连接池中保留的最小连接数 -->
<property name="minPoolSize">10</property>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 -->
<property name="initialPoolSize">20</property>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 -->
<property name="maxIdleTime">600</property>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 -->
<property name="acquireIncrement">5</property>
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 -->
<property name="maxStatements">5</property>
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod">60</property>
</named-config>
</c3p0-config> 还有一个工具类C3P0Util    源码如下:
package com.cn.piaoju.db.util;

import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class C3P0Util {
static ComboPooledDataSource cpds=null;
static{
//这里有个优点,写好配置文件,想换数据库,简单
//cpds = new ComboPooledDataSource("oracle");//这是oracle数据库
cpds = new ComboPooledDataSource("mysql");//这是mysql数据库
}
/**
* 获得数据库连接
* @return Connection
*/
public static Connection getConnection(){
try {
cpds.getConnection();
return cpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

/**
* 数据库关闭操作
* @param conn
* @param st
* @param pst
* @param rs
*/
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 测试DBUtil类
* @param args
*/
public static void main(String[] args) {
Connection conn=getConnection();
System.out.println(conn.getClass().getName());
close(conn,null,null);
}
}
运行main方法结果如下:
com.mchange.v2.c3p0.impl.NewProxyConnection
请各位批评指点,可以这样写么?哪里需要改进?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c3p0