日期:2014-05-20  浏览次数:20827 次

Tomcat6.0数据库连接池到底应该怎么配置
按照一些论坛的说法
我在 server.xml 的 host 直接加入了
   
  <Context path="/web" docBase="web" debug="0" reloadable="true" crossContext="true">  
  <Resource name="jdbc/bbs"  
  auth="Container" type="javax.sql.DataSource" 
  driverClassName="com.mysql.jdbc.Driver" 
  maxIdle="20" 
  maxWait="5000"
  username="root"
  password="admin" 
  url="jdbc:mysql://localhost:3306/bbs"  
  maxActive="100"  
  removeAbandoned="true"
  removeAbandonedTimeout="60"
  logAbandoned="true"/>
  </Context>
在 web.xml 中加入了
  <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>


<%@ page language="java" contentType="text/html; charset=GB18030"
  pageEncoding="GB18030"%>
   
<%@ page import = "java.sql.*" %>
<%@ page import = "javax.naming.InitialContext"%>
<%@ page import = "javax.sql.DataSource" %>
   
<!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=GB18030">
<title>TestDatabase</title>
</head>

<%
ResultSet rs = null;

InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/bbs");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from article ";
rs = stmt.executeQuery(sql);  
 %>
 

<body>

<%
while(rs.next())
{
 %>
 
 <table>
  <tr>
  <td> <%= rs.getString("title") %> </td>
  </tr>
 </table>
 
 <%
  }
  %>

</body>
</html>

以上是测试程序 报错 500
  我把原因贴在楼下
   


------解决方案--------------------
<Context path="/web" docBase="web" debug="0" reloadable="true" crossContext="true">
path是你的web应用发布名吗?docBase是你的系统的当地目录吗(如:c:\**\你的系统目录)?Exception说"Name jdbc is not bound in this Context"
------解决方案--------------------
数据连接池也可以在项目下的 META-INF 文件夹下 建立一个context.xml 
XML code

<?xml version='1.0' encoding='utf-8'?>

<Context>

    <Resource name="jdbc/mysql"   
       auth="Container"   
           
       type="javax.sql.DataSource"   
       driverClassName="com.mysql.jdbc.Driver"   
       url="jdbc:mysql://localhost/bbs"   
       username="root"   
       password="root"   
       maxActive="50"   
       maxIdle="20"   
       maxWait="10000" />   

</Context>

------解决方案--------------------
配置web.xml
添加
XML code

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mysql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>