druid连接池的使用

druid的配置

# cat pom.xml

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.26</version>
</dependency>
# cat config.xml

<bean id="atlasdruid" class="com.alibaba.druid.pool.DruidDataSource"
	init-method="init" destroy-method="close">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://XXX.XXX.XXX.XXX:3306/user?useUncoding=true&amp;characterEncoding=utf-8"/>
    <property name="username" value="user"/>
    <property name="password" value="XXX"/>
    <property name="initialSize" value="1"/>
    <property name="maxWait" value="30000"/>
    <property name="maxActive" value="100"/>
    <property name="minIdle" value="1"/>
    <property name="validationQuery" value="select 1"/>
    <property name="testOnBorrow" value="false"/>
    <property name="testOnReturn" value="false"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="removeAbandoned" value="true"/>
    <property name="removeAbandonedTimeout" value="1800"/>
    <property name="logAbandoned" value="true"/>
    <property name="poolPreparedStatements" value="false"/>
    <property name="filters" value="mergeStat"/>
</bean>

<bean id="singleConn" class="spring.s7.singleConn">
	<property name="dataSource">
		<ref bean="atlasdruid"/>
	</property>
</bean>
# singleConn

public class singleConn {
	private javax.sql.DataSource dataSource;
	public void setDataSource(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
    }
	public void create_connect() {
		try {
			Connection conn = dataSource.getConnection();
			Statement st = conn.createStatement();
			st.setQueryTimeout(2);
			String sql = "select * from test";
			st.executeQuery(sql);
			//sql1 = "insert into test values (1, "ght");";
			//st.executeUpdate(sql1);
		} catch() {
			e.printStackTrace();
		}
	}	
}

# cat driver.java
public class driver {
	public static void main(String[] args) throws InterruptedException {
		ApplicationContext actx;
		actx = new FileSystemXmlApplicationContext("src/main/webapp/config.xml");
		singleConn sc = (singleConn)actx.getBean("singleConn");
		sc.create_connect();
	}
}

druid简单测试

# case 1:
Connection conn = dataSource.getConnection();
Statement st = conn.createStatement();
st.setQueryTimeout(2);
String sql = "select * from test";
st.executeQuery(sql);

# 测试结果
会多建一个线程,用于连接超时时发送KILL pid结束连接。且多建立的该连接并不受限于连接池的配置。

# case 2:
public void create_mult() throws InterruptedException {
	int i = 0;
	for(i=0; i<10; i++) {
		TestThread t = new TestThread(dataSource);
		t.start();
	}
	sleep(10000);
}
public class TestThread extends Thread {
	private javax.sql.DataSource dataSource;
	public TestThread(javax.sql.DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public void run(0 {
		try {	
			System.out.println("thread start...");
			Connection conn = dataSource.getConnection();
			Statement st = conn.createStatement();
			st.setQueryTimeout(2);
			String sql = "select * from test";
			st.executeQuery(sql);
			sleep(10000);
		} catch(Exception e) {
			e.printStackTrace();
		} 
	}
}

# 测试结果
多线程时,也只建立一个额外的连接对多个Statement进行统一管理。

参考