本文来自http://blog.youkuaiyun.com/u012972188 ,引用必须注明出处!
数据库连接池
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客
传统的数据库连接
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class DBUtil {
private DBUtil() {
}
private static DBUtil instance = null;
public synchronized static DBUtil getInstance() {
if (instance == null) {
instance = new DBUtil();
}
return instance;
}
String className = "com.mysql.jdbc.Driver";
// "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:mysql://127.0.0.1:3306/chat?useUnicode=true&characterEncoding=gbk";
// "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB";
String username = "root";
String password = "123456";
public java.sql.Connection getConnection() {
java.sql.Connection con = null;
try {
Class.forName(className);
con = (Connection) DriverManager.getConnection(url, username,
password);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
long began = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
java.sql.Connection conn = DBUtil.getInstance().getConnection();
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("直接连接数据库耗时\t" + (end - began) + "\t毫秒");
}
}
C3P0 数据库连接池的使用
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBUtilOfC3P0 {
private DBUtilOfC3P0() {
cpd = new ComboPooledDataSource();
// 设置 jdbc 信息
cpd.setUser(username);
cpd.setPassword(password);
cpd.setJdbcUrl(url);
try {
cpd.setDriverClass(className);
} catch (PropertyVetoException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//连接池的配置
cpd.setInitialPoolSize(30);
cpd.setMaxPoolSize(100);
cpd.setMinPoolSize(10);
}
private static ComboPooledDataSource cpd = null;
private static DBUtilOfC3P0 instance = null;
public synchronized static DBUtilOfC3P0 getInstance() {
if (instance == null) {
instance = new DBUtilOfC3P0();
}
return instance;
}
String className = "com.mysql.jdbc.Driver";
// "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:mysql://127.0.0.1:3306/chat?useUnicode=true&characterEncoding=gbk";
// "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB";
String username = "root";
String password = "123456";
public java.sql.Connection getConnection() {
java.sql.Connection con=null;
try {
con = cpd.getConnection();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
long began = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
java.sql.Connection conn = DBUtilOfC3P0.getInstance().getConnection();
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("C3P0耗时\t" + (end - began) + "\t毫秒");
}
}
DBCP 数据库连接池
import java.sql.SQLException;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.apache.tomcat.dbcp.dbcp.DataSourceConnectionFactory;
import com.mysql.jdbc.Connection;
public class DBUtilOfDBCP {
private DBUtilOfDBCP() {
bds = new BasicDataSource();
//JDBC 相关信息
bds.setUrl(url);
bds.setDriverClassName(className);
bds.setPassword(password);
bds.setUsername(username);
//连接池配置
// 初始连接数
bds.setInitialSize(30);
// 最大的获取连接数
bds.setMaxActive(100);
// 最小可用空闲连接数
bds.setMinIdle(10);
// 最大可用空闲连接数
bds.setMaxIdle(30);
dscf = new DataSourceConnectionFactory(bds);
}
private static BasicDataSource bds = null;
private static DataSourceConnectionFactory dscf = null;
private static DBUtilOfDBCP instance = null;
public synchronized static DBUtilOfDBCP getInstance() {
if (instance == null) {
instance = new DBUtilOfDBCP();
}
return instance;
}
String className = "com.mysql.jdbc.Driver";
// "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:mysql://127.0.0.1:3306/chat?useUnicode=true&characterEncoding=gbk";
// "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB";
String username = "root";
String password = "123456";
public java.sql.Connection getConnection() {
java.sql.Connection con=null;
try {
con= dscf.createConnection();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
long began = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
java.sql.Connection conn = DBUtilOfDBCP.getInstance().getConnection();
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("DBCP连接数据库克耗时\t" + (end - began) + "\t毫秒");
}
}
评价一下这三种数据库的连接方式,第一种常用,没有使用数据库连接池,连接数量较多时,速度慢。第二种是用了数据库连接池,速度居中,第三种耗时最短,但有出错概率较高,建议使用第二种连接方式 ,另附下载地址 http://download.youkuaiyun.com/detail/u012972188/6631021
写在后面,数据库连接需要数据库驱动, DBCP ,C3P0 需要相应的驱动具体下载地址,你可以从网上搜索到,或者直接从上面的下载地址获得