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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章