在TOMCAT5.0.27 中利用数据链接缓冲池

一、把相应的JDBC包放置到TOMCAT安装目录的COMMON/LIB目录中
二、TOMCAT的配置文件的内容(以ORACLE, THIN连接为例)
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:/Workspace/toshiba" path="/toshiba" reloadable="true" workDir="E:\Workspace\toshiba\work">
 <Resource name="jdbc/OracleDB" auth="Container" type="javax.sql.DataSource"/>

 <ResourceParams name="jdbc/OracleDB">

 <parameter>
  <name>factory</name>
  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
 </parameter>

 <parameter>
  <name>driverClassName</name>
  <value>oracle.jdbc.driver.OracleDriver</value>
 </parameter>

 <parameter>
  <name>url</name>
  <value>jdbc:oracle:thin:@UFJ:1521:DRCH1001</value>
 </parameter>

 <parameter>
  <name>username</name>
  <value>scott</value>
 </parameter>

 <parameter>
  <name>password</name>
  <value>tiger</value>
 </parameter>

 <parameter>
  <name>maxActive</name>
  <value>20</value>
 </parameter>

 <parameter>
  <name>maxIdle</name>
  <value>10</value>
 </parameter>

 <parameter>
  <name>maxWait</name>
  <value>-1</value>
 </parameter>

 </ResourceParams>
</Context>

三、WEB。XML文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <resource-ref>
  <description>Oracle Datasource example</description>
  <res-ref-name>jdbc/OracleDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

四、JAVA代码的书写

package com.csk.dm;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


public class ServiceLocator {
 private InitialContext ic = null;
 private Map cache;
 
 private static ServiceLocator me = null;
 
 static {
  try {
   me = new ServiceLocator();
  } catch (ServiceLocatorException se) {
   System.err.println(se);
   se.printStackTrace(System.err);
  }
 }

 /**
  * 
  */
 public ServiceLocator() throws ServiceLocatorException {
  try {
   ic = new InitialContext();
   cache = Collections.synchronizedMap(new HashMap());
  } catch (NamingException ne) {
   throw new ServiceLocatorException(ne);
  } catch (Exception e) {
   throw new ServiceLocatorException(e);
  }
 }
 
 /**
  * @return
  */
 public static ServiceLocator getInstance() {
        return me;
      }
 
    /**
     * @param dataSourceName
     * @return
     * @throws ServiceLocatorException
     */
    public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException {
        DataSource dataSource = null;
        try {
          if (cache.containsKey(dataSourceName)) {
             dataSource = (DataSource) cache.get(dataSourceName);
          } else {
            Context ctx = (Context)ic.lookup("java:comp/env");
              dataSource = (DataSource)ctx.lookup(dataSourceName);
              cache.put(dataSourceName, dataSource );
          }
        } catch (NamingException ne) {
           throw new ServiceLocatorException(ne);
        } catch (Exception e) {
              throw new ServiceLocatorException(e);
        }
        return dataSource;
      }

}

posted on 2004-08-25 17:51  muddle  阅读(1091)  评论(0编辑  收藏  举报

导航