proxool.properties:
jdbc-1.proxool.alias=test #jdbc-1.proxool.driver-class=com.mysql.jdbc.Driver #jdbc-1.proxool.driver-url=jdbc:mysql://127.0.0.1:3306?useUnicode=true&characterEncoding=utf8 jdbc-1.proxool.driver-class=oracle.jdbc.OracleDriver jdbc-1.proxool.driver-url=jdbc:oracle:thin:@127.0.0.1:1521:orcl jdbc-1.user=SCOTT jdbc-1.password=scott jdbc-1.proxool.maximum-connection-count=100 jdbc-1.proxool.minimum-connection-count=5 jdbc-1.proxool.prototype-count=4 jdbc-1.proxool.verbose=true jdbc-1.proxool.statistics=10s,1m,1d jdbc-1.proxool.statistics-log-level=error
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- web项目中配置proxool,并自动加载 --> <servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class> org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param> <param-name>propertyFile</param-name> <param-value>WEB-INF/classes/proxool.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 监控 proxool 连接池 --> <servlet> <servlet-name>AdminServlet</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/admin.jsp</url-pattern> </servlet-mapping> </web-app>
获取连接:
public class ConnectPoolFactory { public synchronized Connection getConnect(){ Connection conn=null; try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); conn = DriverManager.getConnection("proxool.test"); } catch (Exception e) { e.printStackTrace(); } return conn; } }
下面是遇到的问题:
开始在网上配置proxool连接池,使用了工厂类来实例化,开始是这样写的:
package cn.tedu.util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; public class ConnectPoolFactory { private static ConnectPoolFactory connectPoolFactory=null; private ConnectPoolFactory() {//构造方法 init(); } public void init(){//把properties文件加载到链接对象 InputStream is = ConnectPoolFactory.class.getResourceAsStream("/proxool.properties"); Properties properties = new Properties(); try { properties.load(is); PropertyConfigurator.configure(properties); } catch (Exception e) { e.printStackTrace(); } } public static ConnectPoolFactory getInstance(){//单例 if(null == connectPoolFactory){ connectPoolFactory = new ConnectPoolFactory(); } return connectPoolFactory; } public Connection getConnect(){ Connection conn=null; try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); conn = DriverManager.getConnection("proxool.test"); } catch (Exception e) { e.printStackTrace(); } return conn; } }
但是这种方法可能与web.xml文件重复注册了proxool,会重复注册的错误。
建议使用工具类来获取连接