package com.shen.dbcp.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class DBCPTestRunnable implements Runnable { public int flag = 0; public int selectcount = 0; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final Log log = LogFactory.getLog(DBCPTestRunnable.class); @Override public void run() { flag = TestExecutorService.getFlag(); log.info("线程flag:" + flag + "----run时间:" + df.format(new Date())); while (true) { Connection conn = null; try { conn = TestExecutorService.bds.getConnection(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); log.error(e1); } log.info("flg:" + flag + "-----获取连接时间" + df.format(new Date())); Statement stmt = null; ResultSet rs = null; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); log.error(e); } try { selectcount++; String sql = "select * from dept t where DEPTNO = "; sql += "'" + String.valueOf(selectcount) + "'"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); log.info("thread flg:" + flag + "----" + "当前选择ID:" + selectcount + "----查询时间" + df.format(new Date())); while (rs.next()) { log.info("thread flg:" + flag + "----" + "当前选择ID:" + selectcount + "结果集:" + rs.getInt(1) + "\t" + rs.getString(2) + "\t\t" + rs.getString(3)); } } catch (Exception e) { e.printStackTrace(); log.error(e); } finally {// 释放连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ex) { ex.printStackTrace(); log.error(ex); } } } }}
package com.shen.dbcp.test;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import org.apache.commons.dbcp.BasicDataSource; public class TestExecutorService { public static BasicDataSource bds = new BasicDataSource(); public static int count = 0; public static synchronized int getFlag() { count++; return count; } public static void main(String[] args) { //初始化连接池属性 bds.setUsername("scott"); bds.setPassword("tiger"); bds.setUrl("jdbc:oracle:thin:@10.1.2.133:1521:test"); bds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); int initsize = 4; int maxactive = 10; int maxidle = 5; int minidle = 3; int maxwait = 60000; bds.setInitialSize(initsize); bds.setMaxActive(maxactive); bds.setMaxIdle(maxidle); bds.setMaxWait(maxwait); // bds.setRemoveAbandoned(true); // bds.setLogAbandoned(true); // bds.setRemoveAbandonedTimeout(180); // bds.setTestOnBorrow(true); // bds.setTestOnReturn(true); // bds.setTestWhileIdle(true); // bds.setValidationQuery("SELECT COUNT(*) FROM person"); // bds.setValidationQueryTimeout(60); Thread thread = new Thread(new Runnable() { public void run() { ScheduledExecutorService seserver = Executors.newScheduledThreadPool(100); for(int i=0;i<30;i++){ seserver.scheduleAtFixedRate(new DBCPTestRunnable(), 0, 1, TimeUnit.SECONDS); } } }); thread.start(); }}