按照网上的文章写了份c3p0连接池求各位批评指点
2016-05-05 14:57
253 查看
项目结构如图
![](https://img-blog.csdn.net/20160505145714214)
在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
请各位批评指点,可以这样写么?哪里需要改进?
在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
请各位批评指点,可以这样写么?哪里需要改进?
相关文章推荐
- 解决Communications link failure due to underlying exception
- 简化SSH配置+c3po连接池配置
- C3P0的详细配置说明(com.mchange.v2.c3p0.ComboPooledDataSource)
- c3p0数据库连接池不能取到连接
- c3p0详细配置
- C3P0 详细配置及问题解决大全
- c3p0的oracle数据库配置
- C3P0 数据库连接池之一
- jfinal使用c3p0连接mysql数据库,连接线程time_wait问题
- C3P0连接池泄露,C3p0报错
- Hibernate4之配置详解
- Spring 结合Hibernate 配置 C3P0 连接池(转)
- MySQL 数据库名大小写问题 导致的c3p0连接池 出错
- 数据库连接池的使用(C3P0实现多数据源的数据库连接池)
- com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本
- 数据库连接池配置
- c3p0详细配置。
- 【参考并修改】sql 2008通过sql输出表结构到EXCEL【全】
- Struts2+spring2+hibernate3实例源码-java源码
- MySQL与Hibernate的整合使用