Tomcat DB2的连接池配置使用.doc

 1       准备工作
  Tomcat 6.0

  Eclipse(我的版本3.4.0,否则Tomcat下的配置文件拷贝不全,有可能出错)

  DB2

  DB2的驱动,在安装目录下可以找到:我的C:/Program Files/IBM/SQLLIB/java下的db2java.zip,在放入Tomcat的E:/Tomcat 6.0/conf目录下,需要将db2java.zip修改为db2java.jar,为什么?(Tomcat这么说的)。

2       首先使用JDBC的jsp文件进行测试
2.1    Jdbc的jsp测试文件代码如下:
<%@page import="java.sql.*"  %>

<%@page import="javax.sql.*"%>

 

<%

Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");

String dbUrl ="jdbc:db2:127.0.0.1:XXXXX";

Connection con = DriverManager.getConnection(dbUrl,"username","password");

Statement stmt = con.createStatement();

 

ResultSet rs = stmt.executeQuery("select * from  username.table_name");

 

 

out.println("<table border=1 width=400>");

while (rs.next())

{

    String col1 = rs.getString(1);

    String col2 = rs.getString(2);

    String col3 = rs.getString(3);

    String col4 = rs.getString(4);

 

out.println("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td></tr>");

}

out.println("</table>");

 

rs.close();

stmt.close();

con.close();

%>
 


测试通过后,说明基本配置是正确的了,下面开始准备JNDI的Tomcat连接池配置。

3       连接池代码及配置
3.1    JNDI的测试代码
<%@page import="java.sql.*"  %>

<%@page import="javax.sql.*"%>

<%@page import="javax.naming.*"%>

<%

 

  //try

{

  Context initCtx = new InitialContext();

  Context envCtx = (Context) initCtx.lookup("java:comp/env");

  out.println("envCtx");

  DataSource ds = (DataSource) envCtx.lookup("jdbc/JNDINAME");

  out.println("ds");

  Connection  conn = ds.getConnection();

  Statement stmt=conn.createStatement();

  String strSql="select * from  username.table_name";

  ResultSet rs=stmt.executeQuery(strSql);

  while(rs.next()){

     out.println(rs.getString(1)+"<br>");               

    }

  //}

  //catch(Exception ex){

  //    out.println("err");

  //    out.println(ex.toString());

  //    ex.printStackTrace();

  //}finally{

     

  }

%>
 


3.2    准备配置开发环境下WEB-INF下的web.xml文件
    <!-- 资源配置 -->

    <resource-ref>

       <description>Database DataSource</description>

       <res-ref-name>jdbc/JNDINAME</res-ref-name>

       <res-type>javax.sql.DataSource</res-type>

       <res-auth>Container</res-auth>

    </resource-ref>
 


3.3    配置Tomcat的文件Context.xml,该文件是全局配置文件
<Context>

  <Resource auth="Container" driverClassName="COM.ibm.db2.jdbc.net.DB2Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/JNDINAME" password="password" type="javax.sql.DataSource" url="jdbc:db2:127.0.0.1:XXXXX" username="username"/>

    <!-- Default set of monitored resources -->

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

      

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <!--

    <Manager pathname="" />

    -->

 

    <!-- Uncomment this to enable Comet connection tacking (provides events

         on session expiration as well as webapp lifecycle) -->

    <!--

    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

    -->

</Context>
 


3.4    配置Tomcat下的server.xml文件,该文件会自动产生
<Context docBase="web" path="/web" reloadable="true" source="org.eclipse.jst.j2ee.server:web"/>

</Host>
 


如果不修改,eclipse调试时候会自动产生,有时也可以在该文件中添加Resource说明。发布时就可以在该处添加,表明该应用程序独享的资源。

 

注意:eclipse的tomcat实际上会在工作目录下创建一个Server,在其中配置Tomcat信息:

D:/eclipseworkspace/Servers/Tomcat v6.0 Server at localhost-config
 


4       成功页面

 


5       参考文献
JDBC Data Sources
 
5.1.1   0. 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://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 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.

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

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

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

5.1.5   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.
maxActive - The maximum number of active instances that can be allocated from this pool at the same time.
maxIdle - The maximum number of connections that can sit idle in this pool at the same time.
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.
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.)
user - Database username to be passed to our JDBC driver.
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.
For more details, please refer to the commons-dbcp documentation.
 

 


 

你可能感兴趣的:(tomcat,jdbc,db2,application,database,Deployment)