日期:2014-05-16  浏览次数:20456 次

数据库连接池的三种方式
(1) DBCP连接池(参数详解http://www.ask3.cn/a/jingcaibowen/shujuku/mysql/2011/1025/25419.html)
DBCP连接池是Apache软件基金组织下的一个开源连接池实现。
需要: commons-dbcp-1.2.1.jar //连接池的实现
commons-pool.jar //连接池实现的倚赖库
commons-collection.jar //连接池实现的倚赖库


代码:

package wyd.spring.datasource.dbcp;

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

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

public class DBCPDataSource {
private static BasicDataSource dataSource=null;
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/wyd";
private static final String userName="root";
private static final String password="root";

public static DataSource getDataSource(){
if(dataSource==null){
dataSource=new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
}

return dataSource;
}

public static Connection getConnection() throws SQLException{

return DBCPDataSource.getDataSource().getConnection();
}

}


(2) C3PO 连接池具体参数参考网址(http://zdq0426.blog.163.com/blog/static/221690942010123105810188/)
C3PO 连接池是一个优秀的连接池,推荐使用。C3PO实现了JDBC3.0规范的部分功能,因而性能更加突出。
需要的jar包: c3po0.902.jar

代码:
package wyd.spring.datasource.c3po;

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

import javax.sql.DataSource;


import wyd.spring.datasource.dbcp.DBCPDataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3PODataSource {
private static ComboPooledDataSource dataSource=null;
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/wyd";
private static final String userName="root";
private static final String password="root";

public static DataSource getDataSource(){
if(dataSource==null){
dataSource=new ComboPooledDataSource();
try {
dataSource.setDriverClass(driver);
} catch (PropertyVetoException e) {
System.out.println("DataSource Load Driver Exception!!");
e.printStackTrace();
}
dataSource.setJdbcUrl(url);
dataSource.setUser(userName);
dataSource.setPassword(password);
//设置连接池最大连接容量
dataSource.setMaxPoolSize(20);
//设置连接池最小连接容量
dataSource.setMinPoolSize(2);
//设置连接池最大statements对象容量
dataSource.setMaxStatements(100);

}

return dataSource;
}


public static Connection getConnection() throws SQLException{

return DBCPDataSource.getDataSource().getConnection();
}
}

(3)bonecp连接池(参数详解:http://www.blogjava.net/sxyx2008/archive/2011/03/16/346386.html)
public static BoneCP getConnectionPool(){
if (connectionPool == null) {
try {
Class.forName(driver);
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl(url); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername(userName);
config.setPassword(password);
//数据库连接池的最小连接数 
config.setMinConnectionsPerPartition(5); 
//数据库连接池的最大连接数 
config.setMaxConnectionsPerPartition(10); 
// 
config.setPartitionCount(1); 
//设置数据库连接池 
connectionPool = new BoneCP(config); 
           
} catch (Exception e) {
e.printStackTrace();
}
}
return connectionPool;
}
<