TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一、需要的JAR包:
commons-dbcp-1.4.jar
commons-pool-1.6.jar
mysql-connector-java-5.1.20-bin.jar
二、配置文件:dbcp.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/cwb
username=root
password=8345660 #<!-- 初始化连接 -->
dataSource.initialSize=20 #<!-- 最大空闲连接 -->
dataSource.maxIdle=20 #<!-- 最小空闲连接 -->
dataSource.minIdle=5
#最大连接数量
dataSource.maxActive=100
#是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true
#是否自动回收超时连接
dataSource.removeAbandoned=true
#超时时间(以秒数为单位)
#设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间,
#如果 maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。
dataSource.removeAbandonedTimeout=180
#<!-- 超时等待时间以毫秒为单位 -->
#maxWait代表当Connection用尽了,多久之后进行回收丢失连接
dataSource.maxWait=1000 三、使用连接池,获取数据库连接的代码
四、JSP页面中可以这样用:
<%@ page contentType="text/HTML; charset=GB2312" %>
<%@ page import="org.apache.log4j.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.cwb.tool.DBCPManager" %> <%
Logger logger = Logger.getLogger("mysqltestjsp");
logger.debug("测试DBCP连接池的使用!!");
%>
<h1> Test DBCP连接池!!!</h1>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
//主要是这两句
DBCPManager db=DBCPManager.getInstance();
conn=db.getConnection();
try {
} catch(SQLException e) {
logger.info(e.toString());
} finally {
db.free(rset,stmt,conn);
}
%>
源码下载:http://file.javaxxz.com/2014/10/11/030841109.zip |
|