日期:2014-05-16 浏览次数:20624 次
dbcp可以直接在Tomcat中配置,如下:
1、配置Tomcat的conf下的server.xml中Host节点下的Context
<Context path="/myapp" docBase="...项目绝对路径\WebRoot" debug="5" reloadable="true" crossContext="true">
<Resource auth="Container" name="jdbc/myapp" type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myapp?useUnicode=true&characterEncoding=utf- 8&autoReconnect=true"
username="root"
password="passwd"
maxActive="1000"
maxIdle="10"
maxWait="100"
removeAbandoned="true"
removeAbandonedTimeOut="10"
logAbandoned="true"/>
</Context>
?
2、然后在WEB项目中这样引用:?
?
Context context = null; private DataSource ds; context = new InitialContext(); ds = (DataSource) context.lookup(Configuration.DATA_SOURCE);
??
其中Configuration.DATA_SOURCE 就是Resource?下的name="jdbc/myapp"属性值
?
这样有一个好处就是操作简单,快捷,但是不便于调试,Tomcat在重启时有点慢,而且不便于项目的部署和更新,
如果项目中要对数据库进行备份的话,数据库配置就得修改多个地方,麻烦...
***************************************************************************
下面介绍一下吧dbcp集成到项目当中的方法:
1、配置:
#...前面的driver、url等就根据自己的数据库设置
#************DBCP连接池设置************ #<!-- 初始化连接 --> dbcp.initialSize=50 #<!-- 最大空闲连接 --> dbcp.maxIdle=50 #<!-- 最小空闲连接 --> dbcp.minIdle=5 #最大连接数量(并发量) dbcp.maxActive=1000 #是否在自动回收超时连接的时候打印连接的超时错误 dbcp.logAbandoned=true #是否自动回收超时连接 dbcp.removeAbandoned=true #超时时间(以秒数为单位) dbcp.removeAbandonedTimeout=600 #最大等待时间60秒(以毫秒为单位) dbcp.maxWait=60000
?实现Datasource类:
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import com.zkht.steeltrade.dao.databackup.DBConfig;
/**
* DBCP 数据源
* @author wjm
*
*/
public class DBCPDataSourceFactory {
private static DataSource ds = null;
// private static ConnectionFactory factory = null;
private static DBCPDataSourceFactory instance = new DBCPDataSourceFactory();
private DBCPDataSourceFactory(){
if(ds==null){
ds=setupDataSource();
}
}
public static DBCPDataSourceFactory getInstance(){
return instance;
}
public DataSource lookupDataSource(){
return ds;
}
public void distoryDataSource(){
try {
if(!((BasicDataSource)ds).isClosed()){
((BasicDataSource)ds).close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ds!=null){
ds=null;
}
}
}
private static DataSource setupDataSource() {
int intInitalSize = 50;
try {
intInitalSize = Integer.parseInt(DBConfig.getDBCP_InitialSize());
} catch (Exception ex) {
}
int intMaxIdle = 50;
try {
intMaxIdle = Integer.parseInt(DBConfig.getDBCP_MaxIdle());
} catch (Exception ex) {
}
int intMinIdle = 5;
try {
intMinIdle = Integer.parseInt(DBConfig.getDBCP_MinIdle());
} catch (Exception ex) {
}
int intMaxActive = 100;
try {
intMaxActive = Integer.parseInt(DBConfig.getDBCP_MaxActive());
} catch (Exception ex) {
}
int removeAbandonedTimeout=1000;
try {
removeAbandonedTimeout = Integer.parseInt(DBConfig.getDBCP_RemoveAbandonedTimeout());
} catch (Exception ex) {
}
long maxWait=1000;
try {
maxWait = Long.parseLong(DBConfig.getDBCP_MaxWait());
} catch (Exception ex) {
}
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(DBConfig.getDriverClassName());
bds.setUrl(