tomcat 5.X 的mysql DBCP配置指南及相关问题小结

类别:Java 点击:0 评论:0 推荐:

1.下载Mysql JDBC驱动

Connector/J 3.0.14-stable (官方的 JDBC Driver)
下载地址:http://dev.mysql.com/downloads/connector/j/3.0.html
mm.mysql 2.0.14 (比较老的第三方 JDBC Driver,其实就是上面的官方驱动的前身,呵呵,不过现在有以前的很多文章介绍这个)
下载地址:http://prdownloads.sourceforge.net/mmmysql/mm.mysql-2.0.14-you-must-unjar-me.jar

2.安装(以Connector/J 3.0.14-stable为例说明)

解压后将mysql-connector-java-3.0.14-stable-bin.jar复制到%TOMCAT_HOME%\common\lib下!

NOTE:第三方驱动必须是jar文件,不能是zip文件;不要把jar驱动放到你web应用的/WEB-INF/lib下,也不要放在$JAVA_HOME/jre/lib/ext下,或者其它什么地方。

3.配置tomcat的相关文件;

假设:数据库:testDb 用户名:test 密码:test

(第一步)http://127.0.0.1:8080,进入tomcat页,用tomcat的Tomcat Administration--->Resources--->Data Sources页面添加,参数如下:

JNDI Name: jdbc/mysql
Data Source URL: jdbc:mysql://localhost:3306/testDb?autoReconnect=true&useUnicode=true&characterEncoding=GB2312
JDBC Driver Class: com.mysql.jdbc.Driver
User Name:test
Password:test
Max. Active Connections:4
Max. Idle Connections:2
Max. Wait for Connection:5000
Validation Query ://不添

(第二步)测试页内加入代码

你自己在mysql里建个表检单测试一下吧,我这里以userinfo表为例,在你的应用下做一个测试用的.jsp,如test.jsp,然后http访问这个jsp页,test.jsp代码如下

<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");?
DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from userinfo";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));?
}
}
catch(Exception ex){
ex.printStackTrace();
}
%>

NOTE:当然数据库里得有userinfo这个表

4.问题及解决

问题1:上面的步骤我认为应是最正常的步骤,但是我按上面的做,却发现并不能将DBCP配制成功,出现org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for onnect URL 'null',这是为什么?

解决:在%TOMCAT_HOME%\conf\Catalina\localhost下找到你的web应用对应的.xml文件,如test.xml,并在此文件的下添入代码:

<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>

重启tomcat。

原因:Web界面配DBCP时,生成的是服务器的全局JNDI资源,查看%TOMCAT_HOME%\conf\server.xml可以得知tomcat修改了server.xml,在<server>下的<GlobalNamingResources>下添入了:

<Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>502</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/502?useUnicode=true&amp;characterEncoding=GB2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>502</value>
</parameter>
</ResourceParams>

而用InitialContext去找server的resource当然找不到了,要想找到server的resource就得在web application中的context环境里加入一个指向该全局resource的ResourceLink。

global -->The name of the linked global resource in the global JNDI context.
name -->The name of the resource link to be created, relative to the java:comp/env context.?
type -->The fully qualified Java class name expected by the web application when it performs a lookup for this resource link.

所以,第一步+第二步+问题1的解决也就构成了一种mysql的DBCP的完成配置过程!

问题2:为什么不在web.xml里配置
<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>
?很多文章不是说要配的吗?

原因:嗯,这个问题我是偶尔发现的,不加这个数据库照样连接成功,但是tomcat的文档里也是让这么配,但是我同样在文档里发现了这么一段话:

·<Resource> - Configure the name and data type of a resource made available to the application (equivalent to the inclusion of a <resource-ref> element in the web application deployment descriptor).配置resource的name和type使其针对application可用(等价于在web application deployment descriptor中包含<resource-ref>)

Resource Definitions

You can declare the characteristics of the resource to be returned for JNDI lookups of <resource-ref> and <resource-env-ref> elements in the web application deployment descriptor. You MUST also define Resource Parameters for the same resource name, to configure the object factory to be used (if not known to Tomcat already), and the properties used to configure that object factory.

For example, you can create a resource definition like this:

<Context ...>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"
description="Employees Database for HR Applications"/>
...
</Context>

This is equivalent to (等价于)the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):

<resource-ref>
<description>Employees Database for HR Applications</description>
<res-ref-name>jdbc/EmployeeDB</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>

于是我将web.xml内的相关元素去除,对于获取DataSource没有任何影响。

问题3:我看了许多关于DBCP的配置,里面都是说在<Context>下配,那么如何配制DBCP只应用于指定的web application呢?

解决:tomcat 5.x以后将web application的从server.xml里分离了出来,放在了%TOMCAT_HOME%\conf\Catalina\localhost下,如你的应用为test,那么在这个目录下就会有一个相应的test.xml与之对应,如:

<Context displayName="Welcome to Tomcat" docBase="G:/test" path="/test" reloadable="true">
......
</Context>

所以,我们可以直接在这里为此应用配置DBCP,在和之间加入上面原因1中的代码即可。

问题4:我如何解决中乱码问题?

解决:在配制mysql的数据库url时我们加入了useUnicode=true&characterEncoding=GB2312参数,指定数据库编码方式为GB2312,其它还需要做的就是常用的一些方式,如指定<%@ page contentType="text/html; charset=gb2312">、使用filter等,这方面的资料很多,这里不再累述。

NOTE:这里需要注意的是url中的“&“符号,如果手动在.xml内配制时,需将其转换为“&amp;”

小结:

成功的配制mysql的DBCP的方法有二种:

1. 可以按照 第一步+第二步+问题1中的解决1 配制

2.可以按照 第二步+问题3+问题1中的原因1 配制

本文地址:http://com.8s8s.com/it/it15776.htm