配置数据库连接池

类别:网站制作 点击:0 评论:0 推荐:

配置Tomcat连接池的前提:
1:java运行环境
2:数据库服务器
3:jdbc的jar包放到$CATALINA_HOME/common/lib:如果是sqlserver,
   包含3个jar包:msbase.jar,mssqlserver.jar,msutil.jar;
   如果是db2数据库:包含db2jcc.jar,db2jcc_license_cu.jar
4: 在$CATALINA_HOME/conf/server.xml设置连接池:
   下面是配置的代码:必须放到<Host>和</Host>间:
          <!-- use jndi to conection  -->
        <Context path="/tiles" docBase="/tiles_example" debug="0" reloadable="true" crossContext="true">
           <Resource name="jdbc/db2sql" auth="Container"   type="javax.sql.DataSource"/>
            <ResourceParams name="jdbc/db2sql">
             <parameter>
               <name>username</name>
               <value>admin</value>
             </parameter>
             <parameter>
               <name>password</name>
               <value>lxc</value>
             </parameter> 
             <parameter>  
               <name>driverClassName</name>
               <value>com.ibm.db2.jcc.DB2Driver</value>
             </parameter>   
             <parameter>
                <!-- name = driverName -->     
               <name>url</name>
               <value>jdbc:db2://localhost:50000/testDB</value>
             </parameter>
             <parameter>
              <name>maxActive</name>
                <!-- Maximum number of DB connections in pool.Set to 0 for no limit. -->
     <value>100</value>
       </parameter>
       <parameter>
    <name>maxIdle</name>
    <!-- Maximum number of idle DB connections to retain in pool.Set to 0 for no limit. -->
    <value>30</value>
       </parameter>
       <parameter>
    <name>maxWait</name>
    <!-- Maximum time to wait for a DB connection to become available in ms.An exception is thrown if this timeout is exceeded.Set to -1 to wait indefinitely. -->
    <value>10000</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>
                 
            </ResourceParams>     
        </Context>
5: 在$CATALINA_HOME/webapps/quality/WEB-INF/web.xml里设置被引用的资源:
 下面是配置代码,必须放在<web-app>和</web-app>里。
 <!-- Database Config start -->
 <resource-ref>
  <description>connectDB test</description>
  <res-ref-name>jdbc/db2sql</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
 <!-- Database Config end -->
6: 在java文件中通过jndi name来连接数据库,简单写法如下:
   import javax.naming.Context;
   import javax.naming.InitialContext;
   import javax.sql.DataSource;
   ...............................
   private Connection conn = null;
   private Context   ctx = null;
  ......................................
   try {
       ctx =new InitialContext();
     if(ctx==null)
       throw new Exception("没有匹配的环境");
     DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/db2sql");
     if(ds==null)
      throw new Exception("没有匹配数据库"); 
      this.conn = ds.getConnection();  //获得一个连接
    }
    catch (Exception e) {
      System.out.println("can 't link database ! " + e);
    }


  同理,在jboss下配置连接池的写法如下:
1:在D:\jboss\server\default\deploy下新增一个文件:db2sql-ds.xml:写法如下:
 <?xml version="1.0" encoding="UTF-8"?>
  <datasources>
     <local-tx-datasource>
        <jndi-name>jdbc/db2sql</jndi-name>
        <connection-url>jdbc:db2://localhost:50000/testDB</connection-url>
       <driver-class>com.ibm.db2.jcc.DB2Driver</driver-class>
        <user-name>admin</user-name>
        <password>lxc</password>
      <!-- The minimum connections in a pool/sub-pool. Pools are lazily constructed on first use -->
          <min-pool-size>20</min-pool-size>
      <!-- The maximum connections in a pool/sub-pool -->
          <max-pool-size>50</max-pool-size>
      <!-- The time before an unused connection is destroyed -->
      <!-- NOTE: This is the check period. It will be destroyed somewhere between 1x and 2x this timeout after last use -->
      <!-- TEMPORARY FIX! - Disable idle connection removal, HSQLDB has a problem with not reaping threads on closed connections -->
          <idle-timeout-minutes>0</idle-timeout-minutes>
     </local-tx-datasource>
  </datasources>
2: 在你所应用的××.war工程包中的WEB-INF\lib目录下增加jdbc连接的jar包:如db2,
    则是:db2jcc.jar,db2jcc_license_cu.jar;
3: 区别的地方在于java调用jndi name的时候,写法为:
    ctx =new InitialContext();
    if(ctx==null)
     throw new Exception("没有匹配的环境");
     /** 注意:寻找jndi name的不同写法 */
    DataSource ds=(DataSource)ctx.lookup("java:/jdbc/db2sql");
    if(ds==null)
     throw new Exception("没有匹配数据库"); 
     this.conn = ds.getConnection();

本文地址:http://com.8s8s.com/it/it31246.htm