您的位置:首页 > 其它

DBCP与C3P0第三方插件的使用

2019-05-19 17:59 267 查看

DBCP与C3P0第三方插件的使用

DBCP(DataBase Connection Pool)数据库连接池,也叫作BDCP数据源,是Java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开。
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。
BDCP与C3P0最大的区别就是C3P0有自动回收空闲连接的功能,而BDCP却没有。

1. DBCP的下载及使用
百度搜索DBCP,选择DBCP-Download Apache Commons DBCP点击进入。

选择与自己jdk版本最匹配的版本下载(我用的jdk1.8,选择java8 的版本下载),选择二进制形式的压缩包进行下载。

下载之后双击压缩包进行查看,可以看到以下列表:

红色框出来的jar包就是我们要使用的jar包。
在项目中新建一个folder,命名为lib,将红框框出来的jar包复制粘贴到lib下,并右键单击jar包,选择Build Path,选择Add to Build Path,完成之后在Referenced Libraries下可以看到创建好路径的dbcp的jar包。

dbcp的初始化代码、连接的获取及归还连接的代码:

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

import org.apache.commons.dbcp2.BasicDataSource;

public class DbcpDataSource {

//设置url,user及password
private static final String url = "jdbc:mysql://localhost:3306/demo_user?useUnicode=true&characterEncoding=UTF-8";
private static final String user = "root";
private static final String password = "********";

private static BasicDataSource bDataSource;

// 对DBCP数据源的初始化,放在静态代码块中(初始化数据源只执行一次)
static {
bDataSource = new BasicDataSource();
//设置驱动
bDataSource.setDriverClassName("com.mysql.jdbc.Driver");
bDataSource.setUrl(url);
bDataSource.setUsername(user);
bDataSource.setPassword(password);
//设置初始化连接数目
bDataSource.setInitialSize(5);
//设置最大连接个数
bDataSource.setMaxTotal(20);
//设置最小的空闲连接数目(备用连接)
bDataSource.setMinIdle(3);

}

public static Connection getConnection() {
try {
return bDataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

//使用dbcp不用归还连接,直接调用close方法即可
public static void returnConnection(Connection connection) {
try {
if(connection == null)connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

dbcp在java程序中运行时,会依赖commons pool和commons logging这两个jar包(如下图),在百度上搜索关键字,选择带有appach的结果点击进入,下载和jdk版本相同或相近的版本即可,使用时也是将jar包粘贴到lib下,再建立路径即可。


dbcp一个简单的使用:

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

public class DbcpDemo {

public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement1 = null;
PreparedStatement preparedStatement2 = null;

try {

connection = DbcpDataSource.getConnection();
connection.setAutoCommit(false);
// 开启事务
// mysql默认自动提交事务,此语句将自动提交设置为false
String sql1 = "update user set balance = balance + ? where id = ?";
preparedStatement1 = connection.prepareStatement(sql1);
preparedStatement1.setDouble(1, 100.0);
preparedStatement1.setInt(2, 1);
int record1 = preparedStatement1.executeUpdate();

String sql2 = "update user set balance = balance - ? where id = ?";
preparedStatement2 = connection.prepareStatement(sql2);
preparedStatement2.setDouble(1, 100.0);
preparedStatement2.setInt(2, 2);
int record2 = preparedStatement2.executeUpdate();

connection.commit();
if ((record1 == 1) && (record2 == 1)) {
System.out.println("交易成功!");
} else {
System.out.println("交易取消");
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
if (preparedStatement2 == null)
preparedStatement2.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
if (preparedStatement1 == null)
preparedStatement1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 归还连接
DbcpDataSource.returnConnection(connection);

}

}

}

2. C3P0的下载及使用
c3p0下载地址:https://sourceforge.net/projects/c3p0/files/latest/download
下载之后双压缩包进行查看,可以看到以下列表:

进入lib目录下,便可以看到jar包列表:

红色框出来的jar包就是我们要使用的jar包。
在项目中新建一个folder,命名为lib,将红框框出来的jar包复制粘贴到lib下,并右键单击jar包,选择Build Path,选择Add to Build Path,完成之后在Referenced Libraries下可以看到创建好路径的c3p0的jar包。

c3p0的初始化代码、连接的获取及归还连接的代码:

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0DataSource {

// 设置url,user及password
private static final String url = "jdbc:mysql://localhost:3306/demo_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
private static final String user = "root";
private static final String password = "myroot";

private static ComboPooledDataSource cSource;

// 初始化c3p0数据源
static {
try {
cSource = new ComboPooledDataSource();
cSource.setDriverClass("com.mysql.jdbc.Driver");
cSource.setJdbcUrl(url);
cSource.setUser(user);
cSource.setPassword(password);
// 设置连接池相关规则
cSource.setInitialPoolSize(5);
cSource.setMaxPoolSize(20);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 获取connection
public static Connection getConnection() {
try {
return cSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

// 归还connection
public static void returnConnevtion(Connection connection) {
try {
if (connection == null)
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

c3p0的使用和dbcp的使用方法类似,在这里就不详细说明了。

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