鏈接池使用proxool。 java
1、先寫個輔助類
web
package com.wallimn.util; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; /** * * 數據庫鏈接管理類<br/> * 編碼:wallimn 時間:2009-1-5 下午08:10:52<br/> * 版本:V1.0<br/> */ public class DbManager { private static DbManager manager = new DbManager(); static{ URL url = ClassLoader.getSystemResource("proxool.properties"); try { String fileName = url.getPath(); PropertyConfigurator.configure(fileName); System.out.println("鏈接配置文件:"+fileName); } catch (Exception e) { e.printStackTrace(); } } public static DbManager getInstance() { return manager; } public Connection getConnection() { Connection connection = null; try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); connection = DriverManager.getConnection("proxool.proxool-pool"); } catch (Exception e) { e.printStackTrace(); } return connection; } }2、自已定義一JDBCAppender
package com.wallimn.util; import java.sql.Connection; import java.sql.SQLException; import org.apache.log4j.jdbc.JDBCAppender; public class JDBCPoolAppender extends JDBCAppender { public JDBCPoolAppender() { super(); } @Override protected Connection getConnection() throws SQLException { return DbManager.getInstance().getConnection(); } }3、LOG4J配置文件(log4j.properties)
4、proxool配置文件(proxool.properties)
jdbc-0.proxool.alias=proxool-pool
jdbc-0.proxool.driver-url=jdbc:oracle:thin:@127.0.0.1 :1521:swdb
jdbc-0.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc-0.user=scott
jdbc-0.password=tiger
jdbc-0.proxool.house-keeping-sleep-time=40000
jdbc-0.proxool.house-keeping-test-sql=select 1 from dual
jdbc-0.proxool.minimum-connection-count=10
jdbc-0.proxool.maximum-connection-count=100
jdbc-0.proxool.maximum-connection-lifetime=600000
jdbc-0.proxool.maximum-active-time=600000
jdbc-0.proxool.simultaneous-build-throttle=20
jdbc-0.proxool.recently-started-threshold=40000
jdbc-0.proxool.overload-without-refusal-lifetime=60000
jdbc-0.proxool.verbose=true
jdbc-0.proxool.trace=true
jdbc-0.proxool.fatal-sql-exception=Fatalerror
jdbc-0.proxool.prototype-count=2 sql
package com.wallimn.test; import org.apache.log4j.Logger; import org.apache.log4j.MDC; import junit.framework.TestCase; public class Log4JTest extends TestCase { public void testLog1() { Logger logger = Logger.getLogger("SYSTEM"); // System.out.println(Log4JTest.class); System.out.println("繼承:" + logger.getAdditivity()); System.out.println("級別:" + logger.getLevel()); System.out.println("有效的級別:" + logger.getEffectiveLevel()); MDC.put("userId", "11"); logger.fatal("test_fatal"); logger.error("test_Error"); logger.warn("test_warn"); logger.info("test_Info"); logger.debug("test_debug"); MDC.remove("userId"); } }用到的jar包: