tomcat6连接池配置(备忘)

连接池原理这里我就不赘述了,请参考下面的博文 java连接池原理

tomcat6自带了连接池,废话不多说

1.修改

<Resource name="jdbc/TestDB"
		auth="Container"
		type="javax.sql.DataSource"
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/test"
		username="root"
		password="mysql"
		maxActive="100"
		maxIdle="10"
		maxWait="10000"
	/>

2.项目中的web.xml引用连接池配置


<resource-ref>
  	<description>DB connection</description>
  	<res-ref-name>jdbc/TestDB</res-ref-name>
  	<res-type>javax.sql.DataSource</res-type>
 	<res-auth>Container</res-auth>
  </resource-ref>
3.编写测试类



/**
 * 
 * 项目名称 ConnectionPoolDemo
 * 包          名 linfeng.db
 * 文   件  名 TestDBPool.java
 * 开   发  人 Administrator
 * 描述信息 连接池测试类
 * 发布日期 2013-1-24上午9:58:12
 * 修改日期 
 * 修   改  人        
 * 版本信息 V1.0
 *
 */
public class TestDBPool {
	/**
	 * Connection对象
	 */
	private static Connection conn = null;
	/**
	 * 获得Connection对静态方法
	 * @return conn
	 */
	public static Connection getConn(){
		DataSource ds =null;
		try {
			Context ctx = new InitialContext();
			ds=(DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
			conn=ds.getConnection();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}

4.页面中调用该类

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="linfeng.db.TestDBPool"%>
<%@page import="java.sql.Connection" %>
<%
  String path =request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
  Connection conn = TestDBPool.getConn();
  if(conn!=null){
	  out.println("连接成功!");
  }else{
	  out.println("连接失败!");
  }
%>
</body>
</html>
5.结果

6.小结

maxActive="100"//连接池最大连接数
maxIdle="10"//最大空闲时间,0为无限制
maxWait="10000"//建立连接的最大等待时间毫秒数


转载于:https://my.oschina.net/dlpinghailinfeng/blog/104305

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值