程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Tomcat DBCP配置方法

Tomcat DBCP配置方法

編輯:關於JAVA

本文以tomcat5.0和informix數據庫為例,介紹tomcat 單個JNDI數據源和多數據源的配置。

一、Tomcat配置一個jndi數據源方法

第一步:在C:"Tomcat 5.0"conf"server.xml中<Host>….</Host>中增加如下內容,注意:<DefaultContext>屬於<Host>下的一級元素。

<DefaultContext>
                <Resource name = "jdbc/testdb0_cs" auth="Container"
                       type="javax.sql.DataSource"/>
                <ResourceParams name="jdbc/testdb0_cs">
                       <parameter>
                              <name>factory</name>
                              <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                       </parameter>
                       <parameter>
                              <name>driverClassName</name>
                             <value>com.informix.jdbc.IfxDriver</value>
                       </parameter>
                       <parameter>
                              <name>url</name>
                              <value>jdbc:informix-sqli://192.177.191.130:6666/testdb0_cs:INFORMIXSERVER=ccbhr_test</value>
                       </parameter>
                       <parameter>
                              <name>username</name>
                              <value>hrmis</value>
                       </parameter>
                       <parameter>
                              <name>password</name>
                              <value>hrmis</value>
                       </parameter>
                       <parameter>
                              <name>maxActive</name>
                              <value>20</value>
                       </parameter>
                       <parameter>
                              <name>maxIdle</name>
                              <value>10</value>
                       </parameter>
                       <parameter>
                              <name>poolPrepareStatements</name>
                              <value>true</value>
                       </parameter>
                       <parameter>
                           <name>removeAbandoned</name>
                           <!-- Abandoned DB connections are removed and recycled -->
                           <value>true</value>
                     </parameter>
                     <parameter>
                           <name>removeAbandonedTimeout</name>
                           <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
                           <value>60</value>
                     </parameter>
                     <parameter>
                           <name>logAbandoned</name>
                           <!-- Log a stack trace of the code which abandoned -->
                           <value>false</value>
                     </parameter>
                </ResourceParams>
         </DefaultContext>

第二步:修改C:"eclipse-SDK-3.0.1-win32"eclipse"workspace"zlp"WebContent"WEB-INF下web.xml,在<web-app>…..</web-app>中增加一級元素如下:

<!-- Database Config start -->
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- Database Config end -->

說明:藍色的路徑是你的應用所在路徑下的web.xml

第三步:將Informix JDBC Driver拷貝到C:"Tomcat 5.0"common"lib目錄下

第四步:重新啟動tomcat

二、Tomcat中配置多個數據源的方法

第一步:server.xml 藍色和綠色部分是兩個數據源,分別連接兩個不同的數據庫。依次類推,配置多個。

<DefaultContext>
           <Resource name="jdbc/testdb0_cs1" auth="Container" type="javax.sql.DataSource"/>
           <ResourceParams name="jdbc/testdb0_cs1">
             <parameter>
               <name>factory</name>
               <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
             </parameter>
             <parameter>
               <name>driverClassName</name>
               <value>com.informix.jdbc.IfxDriver</value>
             </parameter>
             <parameter>
               <name>url</name>
               <value>jdbc:informix-sqli://192.177.179.208:6666/testdb0_cs1:INFORMIXSERVER=ccbhr_test</value>
             </parameter>
             <parameter>
               <name>username</name>
               <value>informix</value>
             </parameter>
             <parameter>
               <name>password</name>
               <value>google</value>
             </parameter>
             <parameter>
               <name>maxActive</name>
               <value>20</value>
             </parameter>
             <parameter>
               <name>maxIdle</name>
               <value>10</value>
             </parameter>
             <parameter>
               <name>poolPrepareStatements</name>
               <value>true</value>
             </parameter>
             <parameter>
               <name>removeAbandoned</name>
               <!-- Abandoned DB connections are removed and recycled -->
               <value>true</value>
             </parameter>
             <parameter>
               <name>removeAbandonedTimeout</name>
               <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
               <value>60</value>
             </parameter>
             <parameter>
               <name>logAbandoned</name>
               <!-- Log a stack trace of the code which abandoned -->
               <value>false</value>
             </parameter>
           </ResourceParams>
           <Resource name="jdbc/testdb0_cs" auth="Container" type="javax.sql.DataSource"/>
           <ResourceParams name="jdbc/testdb0_cs">
             <parameter>
               <name>factory</name>
               <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
             </parameter>
             <parameter>
               <name>driverClassName</name>
               <value>com.informix.jdbc.IfxDriver</value>
             </parameter>
             <parameter>
               <name>url</name>
               <value>jdbc:informix-sqli://192.177.191.130:6666/testdb0_cs:INFORMIXSERVER=ccbhr_test</value>
             </parameter>
             <parameter>
               <name>username</name>
               <value>informix</value>
             </parameter>
             <parameter>
               <name>password</name>
               <value>informix</value>
             </parameter>
             <parameter>
               <name>maxActive</name>
               <value>20</value>
             </parameter>
             <parameter>
               <name>maxIdle</name>
               <value>10</value>
             </parameter>
             <parameter>
               <name>poolPrepareStatements</name>
               <value>true</value>
             </parameter>
             <parameter>
               <name>removeAbandoned</name>
               <!-- Abandoned DB connections are removed and recycled -->
               <value>true</value>
             </parameter>
             <parameter>
               <name>removeAbandonedTimeout</name>
               <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
               <value>60</value>
             </parameter>
             <parameter>
               <name>logAbandoned</name>
               <!-- Log a stack trace of the code which abandoned -->
               <value>false</value>
             </parameter>
           </ResourceParams>
         </DefaultContext>

第二步:在應用的web.xml中增加新增的數據源,如下所示:

<!-- Database Config start -->
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
  <resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- Database Config end -->

第三步:重啟tomcat

三、多個數據源的訪問方法參考

public Connection getConn(String dsnName)
        {
               Connection conn = null;
               Context ctx = null;
               DataSource ds = null;
               String jndiStr = null;
               try{
                      jndiStr = "java:comp/env/jdbc/" + dsnName.trim();
                      System.out.println("jndiStr=[" + jndiStr + "]");
                      ctx = new InitialContext();
                      ds = (DataSource) ctx.lookup(jndiStr);

                      conn = ds.getConnection();
               }
               catch(Exception e)
               {
                      System.out.println("Connect database [" + dsnName.trim() + "] Error:" + e);
               }
               return conn;
        }

說明:其中dsnName是我們要具體訪問的數據源JNDI,例如:testdb0_cs1

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved