用proxool配置数据库连接池步骤(自写管理类配置)

用proxool配置数据库连接池步骤(自写管理类配置)
2007-09-13 08:56

1、下载proxool 地址:http://proxool.sourceforge.net
2、解压缩proxool-0.9.0RC2.zip,拷贝lib/proxool-0.9.0RC2.jar到web-info/lib
    拷贝jdbc驱动到web-info/lib
3、在WEB-INF/classes/下建立文件:Proxool.properties
文件内容如下:
//网上查车数据库
jdbc-1.proxool.alias=WEBGIS
jdbc-1.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc-1.proxool.driver-url=jdbc:oracle:thin:@192.168.0.1:1521:ORCL
jdbc-1.user=gis
jdbc-1.password=1
jdbc-1.proxool.maximum-connection-count=20
jdbc-1.proxool.prototype-count=4
jdbc-1.proxool.house-keeping-test-sql=select sysdate from DUAL
jdbc-1.proxool.verbose=true
jdbc-1.proxool.statistics=10s,1m,1d
jdbc-1.proxool.statistics-log-level=ERROR
//标准版数据库
jdbc-2.proxool.alias=Navtech
jdbc-2.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc-2.proxool.driver-url=jdbc:oracle:thin:@192.168.0.2:1521:orcl
jdbc-2.user=Nav
jdbc-2.password=1
jdbc-2.proxool.maximum-connection-count=20
jdbc-2.proxool.prototype-count=4
jdbc-2.proxool.house-keeping-test-sql=select sysdate from DUAL
jdbc-2.proxool.verbose=true
jdbc-2.proxool.statistics=10s,1m,1d
jdbc-2.proxool.statistics-log-level=ERROR
//计费数据库
jdbc-3.proxool.alias=Fee
jdbc-3.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc-3.proxool.driver-url=jdbc:oracle:thin:@192.168.0.3:1521:ORCL
jdbc-3.user=fee
jdbc-3.password=1
jdbc-3.proxool.maximum-connection-count=20
jdbc-3.proxool.prototype-count=4
jdbc-3.proxool.house-keeping-test-sql=select sysdate from DUAL
jdbc-3.proxool.verbose=true
jdbc-3.proxool.statistics=10s,1m,1d
jdbc-3.proxool.statistics-log-level=ERROR

4、创建连接管理类
import java.sql.*;
import java.io.*;
import java.util.*;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
import org.logicalcobwebs.proxool.ProxoolException;

public class ConnectionManager {
     static private ConnectionManager instance;

     static synchronized public ConnectionManager getInstance() {
         if (instance == null) {
             instance = new ConnectionManager();
         }
         return instance;
     }

     private ConnectionManager() {
         this.init();
     }

     public void freeConnection(String name, Connection con) {
         try{
             if(con!=null) con.close();
         }catch(Exception ex){ex.printStackTrace();}
     }

     public Connection getConnection(String proxoolname) {
         try {
             return DriverManager.getConnection(proxoolname);
         } catch (SQLException ex) {
             return null;
         }
     }

     public void release() {}

     private void init(){
         try {
             InputStream is = getClass().getResourceAsStream("/Proxool.properties");
             Properties properties = new Properties();
             properties.load(is);
             PropertyConfigurator.configure(properties);
         } catch (IOException ex) {
             ex.printStackTrace();
         } catch (ProxoolException e1) {
             e1.printStackTrace();
         }
     }
}

5、使用示例(节选)
     public Vector SqlExcute(String poolname,String sqlString) {
         Vector rc = new Vector();
         int colCnt = 0;
         try {
             connmanager = ConnectionManager.getInstance();
             conn = connmanager.getConnection(poolname); //此处应为你要使用的连接池的名称,如:proxool.WEBGIS或proxool.Navtech或proxool.Fee
             pstam = conn.prepareStatement(sqlString);
             rs = pstam.executeQuery();
             colCnt = rs.getMetaData().getColumnCount();
             while (rs.next()) {
                 String[] tempStr = new String[colCnt];
                 for (int i = 0; i < colCnt; i++) {
                     tempStr[i] = rs.getString(i + 1);
                     if (tempStr[i] == null) {
                         tempStr[i] = "";
                     }
                 }
                 rc.add(tempStr);
             }
         } catch (Exception e) {
         } finally {
             try {
                 rs.close();
                 pstam.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
             if (conn != null) {
                 try {
                     connmanager.freeConnection(poolname, conn);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
         }
         return rc;
     }

你可能感兴趣的:(数据库,数据库连接池)