Proxool的配置用法

1. Proxool的配置文件proxool.xml

<?xml version="1.0" encoding="UTF-8"?> 

      <!-- the proxool configuration can be embedded within your own application's.

      Anything outside the "proxool" tag is ignored. -->

  <conn-pool>

  <proxool>

    <alias>db_master</alias>

    <driver-url>jdbc:mysql://localhost/db_jtt?charset=utf8</driver-url>

    <driver-class>org.gjt.mm.mysql.Driver</driver-class>

    <driver-properties>

      <property name="user" value="root"/>

      <property name="password" value="admin"/>

    </driver-properties>

    <maximum-connection-count>500</maximum-connection-count>

    <simultaneous-build-throttle>50</simultaneous-build-throttle>

    <house-keeping-test-sql>select 0</house-keeping-test-sql>

    <trace>true</trace>

  </proxool>

</conn-pool>

2.新建DBConnection.java

package cn.com.sztele.util;  
import java.sql.*; 

public class DBConnection {

    /*使用单例模式获取数据库连接池配置*/

    public static Connection getConn()throws Exception{

       return DriverManager.getConnection("proxool.db_master");

    }    

    /*关闭连接*/

    public static void closeDB(Connection conn){

       try{

           if(conn!=null)conn.close();

       }catch(Exception e){}

    }

    /*事务提交*/

    public static void commit (Connection conn){

       try{

           if (conn!=null) conn.commit();

       }catch (Exception e) {           

       }

    }   

    /*事务回滚*/

    public static void rollback(Connection conn) {

       try {

           if (conn!=null) conn.rollback();

       } catch (Exception e){           

       }   

    }

}
 

3.写一个初始化配置文件

package cn.com.sztele.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.logicalcobwebs.proxool.ProxoolFacade;

/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 public void init() throws ServletException {
  String dir = getServletContext().getRealPath("/");
  dir = dir.replace('\\', '/');

  String configFile = getInitParameter("log4j.init.filename");
  System.out.println("[系统初始化][初始化日志组件]初始参数log4j.init.filename=" + dir + configFile);
  try {
   org.apache.log4j.PropertyConfigurator.configure(dir + configFile);
  } catch (Exception e) {
   e.printStackTrace();
   throw new ServletException(e);
  }

  configFile = getInitParameter("proxool.init.filename");
  System.out.println("[系统初始化][初始化连接池组件]初始参数proxool.init.filename=" + dir + configFile);
  try {
   org.logicalcobwebs.proxool.configuration.JAXPConfigurator.configure(dir+configFile, false);
  } catch (Exception e) {
   e.printStackTrace();
   throw new ServletException(e);
  }
 }
 public void destroy() {
  //org.apache.log4j.Logger.getInstance("piclog").info("resin停止");
  //System.err.println("resin 停止");
  ProxoolFacade.shutdown(); 
 }
}

 

4.在web.xml中进行配置

<servlet>
          <servlet-name>InitServlet</servlet-name>
          <servlet-class>cn.com.sztele.servlet.InitServlet</servlet-class>
       <init-param>
          <param-name>log4j.init.filename</param-name>
          <param-value>WEB-INF/classes/cn/com/sztele/config/log4j.properties</param-value>
       </init-param>
       <init-param>
          <param-name>proxool.init.filename</param-name>
          <param-value>WEB-INF/classes/cn/com/sztele/config/proxool.xml</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
      </servlet>   





你可能感兴趣的:(proxool)