`
wxb880114
  • 浏览: 683535 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Tomcat7.0配置JDBC Data Sources

 
阅读更多

    0. Introduction

    Many web applications need to access a database via a JDBC driver, to support the functionality required by that application. The Java EE Platform Specification requires Java EE Application Servers to make available a DataSource implementation (that is, a connection pool for JDBC connections) for this purpose. Tomcat offers exactly the same support, so that database-based applications you develop on Tomcat using this service will run unchanged on any Java EE 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://java.sun.com/j2se/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 Java EE Platform Specification (covers the JDBC facilities that all Java EE 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-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>

   
   

    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 <Context> element for the web application as described below.
    4. Configure Tomcat's Resource Factory

    To configure Tomcat's resource factory, add an element like this to the <Context> element for the web application.
   
   

    <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>

   
   

    Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor.

    This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.

    The configuration properties for Tomcat's standard data source resource factory (org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory) are as follows:

        driverClassName - Fully qualified Java class name of the JDBC driver to be used.
        username - Database username to be passed to our JDBC driver.
        password - Database password to be passed to our JDBC driver.
        url - Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
        initialSize - The initial number of connections that will be created in the pool during pool initialization. Default: 0
        maxActive - The maximum number of connections that can be allocated from this pool at the same time. Default: 8
        minIdle - The minimum number of connections that will sit idle in this pool at the same time. Default: 0
        maxIdle - The maximum number of connections that can sit idle in this pool at the same time. Default: 8
        maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. Default: -1 (infinite)

    Some additional properties handle connection validation:

        validationQuery - SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row.
        validationQueryTimeout - Timeout in seconds for the validation query to return. Default: -1 (infinite)
        testOnBorrow - true or false: whether a connection should be validated using the validation query each time it is borrowed from the pool. Default: true
        testOnReturn - true or false: whether a connection should be validated using the validation query each time it is returned to the pool. Default: false

    The optional evictor thread is responsible for shrinking the pool by removing any conections which are idle for a long time. The evictor does not respect minIdle. Note that you do not need to activate the evictor thread if you only want the pool to shrink according to the configured maxIdle property.

    The evictor is disabled by default and can be configured using the following properties:

        timeBetweenEvictionRunsMillis - The number of milliseconds between consecutive runs of the evictor. Default: -1 (disabled)
        numTestsPerEvictionRun - The number of connections that will be checked for idleness by the evitor during each run of the evictor. Default: 3
        minEvictableIdleTimeMillis - The idle time in milliseconds after which a connection can be removed from the pool by the evictor. Default: 30*60*1000 (30 minutes)
        testWhileIdle - true or false: whether a connection should be validated by the evictor thread using the validation query while sitting idle in the pool. Default: false

    Another optional feature is the removal of abandoned connections. A connection is called abandoned if the application does not return it to the pool for a long time. The pool can close such connections automatically and remove them from the pool. This is a workaround for applications leaking connections.

    The abandoning feature is disabled by default and can be configured using the following properties:

        removeAbandoned - true or false: whether to remove abandoned connections from the pool. Default: false
        removeAbandonedTimeout - The number of seconds after which a borrowed connection is assumed to be abandoned. Default: 300
        logAbandoned - true or false: whether to log stack traces for application code which abandoned a statement or connection. This adds serious overhead. Default: false

    Finally there are various properties that allow further fine tuning of the pool behaviour:

        defaultAutoCommit - true or false: default auto-commit state of the connections created by this pool. Default: true
        defaultReadOnly - true or false: default read-only state of the connections created by this pool. Default: false
        defaultTransactionIsolation - This sets the default transaction isolation level. Can be one of NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE. Default: no default set
        poolPreparedStatements - true or false: whether to pool PreparedStatements and CallableStatements. Default: false
        maxOpenPreparedStatements - The maximum number of open statements that can be allocated from the statement pool at the same time. Default: -1 (unlimited)
        defaultCatalog - The name of the default catalog. Default: not set
        connectionInitSqls - A list of SQL statements run once after a Connection is created. Separate multiple statements by semicolons (;). Default: no statement
        connectionProperties - A list of driver specific properties passed to the driver for creating connections. Each property is given as name=value, multiple properties are separated by semicolons (;). Default: no properties
        accessToUnderlyingConnectionAllowed - true or false: whether accessing the underlying connections is allowed. Default: false

    For more details, please refer to the commons-dbcp documentation.
分享到:
评论

相关推荐

    MyEclipse+Tomcat7.0配置图文教程

    MyEclipse+Tomcat7.0配置图文教程 本文主要介绍了MyEclipse和Tomcat7.0的配置过程,包括Tomcat7.0的安装配置和MyEclipse中的Tomcat配置。 一、Tomcat 7.0 的安装配置 Tomcat 7.0 的安装配置可以分为以下几个步骤...

    Tomcat 7.0 官方64位版

    在压缩包中,"Apache Tomcat7.0 v7.0.52 官方64位版_386w.com" 包含了Tomcat的完整安装文件,用户可以通过解压后按照指定步骤进行安装。主要目录包括: - **bin**: 包含启动和停止Tomcat的脚本。 - **conf**: 存放...

    tomcat7.0.109下载

    1. **安装与配置**:下载的文件`apache-tomcat-7.0.109.tar.gz`是一个归档文件,需要解压到适当的目录。在Unix/Linux系统中,可以使用`tar -zxvf apache-tomcat-7.0.109.tar.gz`命令进行解压。然后,配置环境变量如...

    Tomcat7.0 tomcat最新版本

    1. **Servlet 3.0支持**:Tomcat 7.0引入了对Servlet 3.0规范的支持,这意味着开发者可以利用新的特性,如异步处理、注解配置、动态注册Servlet和过滤器,以及初始化参数等,简化开发流程。 2. **JSP 2.2支持**:...

    myeclipse8.6 jdk1.7 tomcat7.0 配置

    本文将详细介绍如何在MyEclipse8.6中配置JDK1.7以及Tomcat7.0,这对于使用Java进行Web开发的开发者来说至关重要。 首先,我们从第一步开始,安装JDK1.7。JDK(Java Development Kit)是Oracle公司提供的Java开发...

    Tomcat_7.0配置

    从百度文库下载的Tomcat7.0的配置

    Tomcat 7.0 配置教程及文件大全套,直接搞定

    【标题】:“Tomcat 7.0 配置教程及文件大全套,直接搞定” 【内容详解】 Tomcat是一款广泛使用的开源Java Servlet容器,由Apache软件基金会开发,它实现了Java Servlet和JavaServer Pages(JSP)规范,是搭建Java...

    apache-tomcat7.0.100windows和linux版本安全版本.zip

    Apache Tomcat 7.0.100 是一个流行的开源软件,用作Java Servlet和JavaServer Pages(JSP)的容器,以及Java EE Web应用程序的标准实现。它由Apache软件基金会维护,是Apache Jakarta项目的一部分。这个版本适用于...

    Tomcat7.0配置教程.doc

    本文主要讲述了 Tomcat7.0 的配置教程,包括安装 JDK、配置环境变量、安装 Tomcat7.0、配置 Tomcat7.0 环境变量、管理员配置等。 一、安装 JDK 在配置 Tomcat7.0 之前,需要先安装 JDK。JDK1.7 可以从官方网站下载...

    tomcat7.0.rar

    2. 解压:将下载的文件解压缩到你选择的目录,例如`/usr/local/tomcat7.0`。 3. 配置环境变量:设置`CATALINA_HOME`指向Tomcat的根目录,并将路径添加到系统的`PATH`环境变量中。 4. 启动与停止:使用`bin/startup....

    在Tomcat 7.0中配置数据库连接池

    ### 在Tomcat 7.0中配置数据库连接池 #### 一、背景介绍 在现代Web应用程序开发过程中,数据库操作是非常频繁的一项任务。为了提高数据库访问效率并减少资源开销,通常会采用数据库连接池技术。Apache Tomcat作为一...

    Tomcat7.0配置教程

    ### Tomcat7.0配置教程:详尽指南与步骤解析 #### 一、环境准备:JDK1.7安装与配置 在配置Tomcat7.0之前,首先需要确保已经正确安装并配置了JDK1.7环境。这是因为Tomcat作为Java应用服务器,依赖于JDK来运行Java...

    tomcat7.0.zip

    tomcat 7.0占用系统软件资源小,拥有非常丰富的扩展性能,当用户在一台电脑上配置好apache服务器,这个软件就可以马上响应对html页面访问请求,是一个使用率非常高的web服务器。 tomcat 7.0功能介绍: 当在一台...

    tomcat7.0安装教程

    本文档旨在指导用户如何安装和配置 Tomcat 7.0,特别是无桌面快捷方式的安装模式。 一、环境准备 在安装 Tomcat 7.0 之前,需要准备好 Java 运行环境,因为 Tomcat 是基于 Java 语言编写的。用户需要确保已经安装...

    Tomcat7.0 64位

    【描述】中提到的Tomcat7.0作为轻量级的服务器,其特点在于资源占用少,启动快速,易于管理和配置。开发者可以利用Tomcat7.0将开发完成的Java Web项目部署并发布到互联网上,使得全球用户能够通过网络访问这些应用。...

    windows版Tomcat 7.0的配置

    windows版Tomcat 7.0的配置

    Tomcat7.0_windows64压缩包下载

    【标题】"Tomcat7.0_windows64压缩包下载" 涉及的主要知识点是Apache Tomcat服务器在Windows 64位操作系统上的部署和使用。Tomcat是一款开源的Java Servlet容器,它实现了JavaServer Pages (JSP)、Java Servlet以及...

    tomcat7.0windows版配置

    本文将基于“tomcat7.0windows版配置”的标题、描述及相关内容,深入解析如何在Windows环境下安装与配置Tomcat7.0,以及确保其正常运行的关键步骤。 ### 一、环境准备:JDK安装与配置 #### 步骤1:JDK安装 - 在...

    tomcat7.0安装admin

    在本篇内容中,我们将详细讨论如何在Tomcat 7.0中安装和配置Admin Web Application,这是一个用于管理Tomcat服务器的应用程序。由于在Tomcat 7.0的官方源中找不到Admin应用的直接下载链接,我们可以采取一种变通方法...

Global site tag (gtag.js) - Google Analytics