JNDI配置Tomcat的数据库连接池

JDBC Data Sources

0、介绍
许多的web应用程序需要通过jdbc驱动去访问数据库,以支持应用所需的功能。J2EE的平台特性要求J2EE应用服务器提供数据源的安装,也就是说,需要一个JDBC连接池来实现。Tomcat 6恰好提供了相同的支持,以便于你在Tomcat 平台上开发的基于数据库的应用程序不用改变任何J2EE的服务器就可以运行。

注意:Tomcat中支持的默认数据源来自开源项目的DBCP连接池。然而,通过你自己定制的资源工厂,使用其他的连接池来实现数据源也是合理的,描述如下:

1、安装JDBC驱动
使用JDBC数据源JNDI资源工厂要求安装合适的JDBC驱动程序提供给Tomcat内部的类和Web应用程序。最容易实现的一种方法就是将驱动的一个或多个jar文件放入Tomcat的安装目录里的lib目录下,使得驱动能够访问资源工厂和你的应用程序。

2、声明资源需求
下一步,修改web应用程序的部署描述符,也就是 /WEB-INF目录下的web.xml文件,在查找到的、预先配置好的数据源下声明一个JNDI的名称。
按照约定,所有的JNDI名称都应该被分解成JDBC上下文的格式。(和标准java相关:comp/env 命名上下文是所有被提供的资源工厂的根目录。)一个典型的 web.xml文件记录如下:

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the <Context>
    configurartion for the web application.
  </description>
  <res-ref-name>
    jdbc/EmployeeDB
  </res-ref-name>
  <res-type>
    javax.sql.DataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

提醒:确定你遵守了web应用程序部署描述符的DTD规则,查看Servlet的详细说明。

3、使用资源编码你的应用程序
一种典型的资源参考用法如下:

 Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

注意应用程序和我们在web应用程序描述符中声明的资源名称引用的相同。为web应用程序配置资源工厂的元素描述如下。

4、配置Tomcat的资源工厂

<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxActive="8"
            maxIdle="4"/>
  ...
</Context>

注意:资源名称必须和web.xml文件中的指定的值一致。

 <res-ref-name>
    jdbc/EmployeeDB
  </res-ref-name>
Resource name="jdbc/EmployeeDB"

上述事例假设的你使用的是HypersonicSQL 数据库的JDBC驱动。自定义驱动类名和驱动名参数与你实际所用的数据库JDBC驱动和连接的路径相匹配。

以下是原文:

  1. Introduction
    Many web applications need to access a database via a JDBC driver, to support the functionality required by that application. The J2EE Platform Specification requires J2EE Application Servers to make available a DataSource implementation (that is, a connection pool for JDBC connections) for this purpose. Tomcat 6 offers exactly the same support, so that database-based applications you develop on Tomcat using this service will run unchanged on any J2EE server.
    For information about JDBC, you should consult the following:

http://java.sun.com/products/jdbc/ - Home page for information about Java Database Connectivity.
http://docs.oracle.com/javase/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html - The JDBC 2.1 API Specification.
http://java.sun.com/products/jdbc/jdbc20.stdext.pdf - The JDBC 2.0 Standard Extension API (including the javax.sql.DataSource API). This package is now known as the “JDBC Optional Package”.
http://java.sun.com/j2ee/download.html - The J2EE Platform Specification (covers the JDBC facilities that all J2EE platforms must provide to applications).

NOTE - The default data source support in Tomcat is based on the DBCP connection pool from the Commons project. However, it is possible to use any other connection pool that implements javax.sql.DataSource, by writing your own custom resource factory, as described below.

  1. Install Your JDBC Driver
    Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver’s JAR file(s) into the $CATALINA_HOME/lib directory, which makes the driver available both to the resource factory and to your application.

  2. Declare Your Resource Requirements
    Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this:



    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the
    configurartion for the web application.


    jdbc/EmployeeDB


    javax.sql.DataSource


    Container

    WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.

  3. Code Your Application’s Use Of This Resource
    A typical use of this resource reference might look like this:

    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup(“java:comp/env”);
    DataSource ds = (DataSource)
    envCtx.lookup(“jdbc/EmployeeDB”);

Connection conn = ds.getConnection();
… use this connection to access the database …
conn.close();

Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in the element for the web application as described below.

  1. Configure Tomcat’s Resource Factory
    To configure Tomcat’s resource factory, add an element like this to the element for the web application.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值