mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@620 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -37,30 +37,40 @@ import com.alibaba.druid.mock.handler.MySqlMockExecuteHandlerImpl;
|
||||
|
||||
public class MockDriver implements Driver {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(MockDriver.class);
|
||||
private final static Log LOG = LogFactory.getLog(MockDriver.class);
|
||||
|
||||
public final static MockExecuteHandler DEFAULT_HANDLER = new MySqlMockExecuteHandlerImpl();
|
||||
public final static MockExecuteHandler DEFAULT_HANDLER = new MySqlMockExecuteHandlerImpl();
|
||||
|
||||
private String prefix = "jdbc:fake:";
|
||||
private String mockPrefix = "jdbc:mock:";
|
||||
private String prefix = "jdbc:fake:";
|
||||
private String mockPrefix = "jdbc:mock:";
|
||||
|
||||
private MockExecuteHandler executeHandler = DEFAULT_HANDLER;
|
||||
private MockExecuteHandler executeHandler = DEFAULT_HANDLER;
|
||||
|
||||
public final static MockDriver instance = new MockDriver();
|
||||
public final static MockDriver instance = new MockDriver();
|
||||
|
||||
private final AtomicLong connectCount = new AtomicLong();
|
||||
private final AtomicLong connectionCloseCount = new AtomicLong();
|
||||
private final AtomicLong connectCount = new AtomicLong();
|
||||
private final AtomicLong connectionCloseCount = new AtomicLong();
|
||||
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong(1000L);
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong(1000L);
|
||||
|
||||
private final List<MockConnection> connections = new CopyOnWriteArrayList<MockConnection>();
|
||||
private final List<MockConnection> connections = new CopyOnWriteArrayList<MockConnection>();
|
||||
|
||||
private long idleTimeCount = 1000 * 60 * 3;
|
||||
private long idleTimeCount = 1000 * 60 * 3;
|
||||
|
||||
private boolean logExecuteQueryEnable = true;
|
||||
|
||||
static {
|
||||
registerDriver(instance);
|
||||
}
|
||||
|
||||
public boolean isLogExecuteQueryEnable() {
|
||||
return logExecuteQueryEnable;
|
||||
}
|
||||
|
||||
public void setLogExecuteQueryEnable(boolean logExecuteQueryEnable) {
|
||||
this.logExecuteQueryEnable = logExecuteQueryEnable;
|
||||
}
|
||||
|
||||
public long getIdleTimeCount() {
|
||||
return idleTimeCount;
|
||||
}
|
||||
@@ -134,7 +144,7 @@ public class MockDriver implements Driver {
|
||||
}
|
||||
|
||||
MockConnection conn = new MockConnection(this, info);
|
||||
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("connect, url " + url + ", id " + conn.getId());
|
||||
}
|
||||
@@ -196,7 +206,7 @@ public class MockDriver implements Driver {
|
||||
}
|
||||
|
||||
protected ResultSet executeQuery(MockStatement stmt, String sql) throws SQLException {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
if (logExecuteQueryEnable && LOG.isDebugEnabled()) {
|
||||
LOG.debug("executeQuery " + sql);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
* @author ljw<ljw2083@alibaba-inc.com>
|
||||
*/
|
||||
public abstract class DruidAbstractDataSource implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable {
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private final CountDownLatch initedLatch = new CountDownLatch(2);
|
||||
|
||||
|
||||
private boolean enable = true;
|
||||
private boolean enable = false;
|
||||
|
||||
public DruidDataSource(){
|
||||
}
|
||||
@@ -116,6 +116,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
this.id = DruidDriver.createDataSourceId();
|
||||
|
||||
enable = true;
|
||||
|
||||
if (maxActive <= 0) {
|
||||
throw new IllegalArgumentException("illegal maxActive " + maxActive);
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class TestConcurrent extends TestCase {
|
||||
|
||||
private MockDriver driver;
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
driver = new MockDriver();
|
||||
driver.setLogExecuteQueryEnable(false);
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setDriver(driver);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.setMaxActive(100);
|
||||
dataSource.setMaxIdle(100);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(1 * 100);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(1 * 10);
|
||||
dataSource.setTestWhileIdle(true);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(false, dataSource.isEnable());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
// 第一次建立连接
|
||||
{
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(false, dataSource.isEnable());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getActiveCount());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(true, dataSource.isEnable());
|
||||
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
Assert.assertEquals(true, dataSource.isEnable());
|
||||
|
||||
// 连续打开关闭单个连接
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getActiveCount());
|
||||
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(true, dataSource.isEnable());
|
||||
}
|
||||
|
||||
// 使用单个线程模拟并发打开10个连接
|
||||
for (int i = 0; i < 1000 * 10; ++i) {
|
||||
final int COUNT = 10;
|
||||
Connection[] connections = new Connection[COUNT];
|
||||
|
||||
for (int j = 0; j < connections.length; ++j) {
|
||||
connections[j] = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(j + 1, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
Assert.assertEquals(COUNT, dataSource.getActiveCount());
|
||||
Assert.assertEquals(COUNT, dataSource.getCreateCount());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
|
||||
for (int j = 0; j < connections.length; ++j) {
|
||||
connections[j].close();
|
||||
Assert.assertEquals(j + 1, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(COUNT, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
concurrent(10);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并发执行10000次
|
||||
* @param threadCount
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private void concurrent(final int threadCount) throws InterruptedException {
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch endLatch = new CountDownLatch(threadCount);
|
||||
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threadCount; ++i) {
|
||||
threads[i] = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
startLatch.await();
|
||||
|
||||
for (int i = 0; i < 1000 * 1; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1");
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
dataSource.shrink();
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(dataSource.getMinIdle(), dataSource.getPoolingCount());
|
||||
|
||||
System.out.println(threadCount + "-threads start");
|
||||
startLatch.countDown();
|
||||
endLatch.await();
|
||||
System.out.println(threadCount + "-threads complete");
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertTrue(threadCount >= dataSource.getPoolingCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user