This commit is contained in:
Zhanming Qi
2012-08-26 10:59:02 +08:00
7 changed files with 237 additions and 22 deletions
@@ -444,13 +444,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
private void createAndStartDestroyThread() {
DestroyConnectionThread thread = new DestroyConnectionThread("Druid-ConnectionPool-Destory");
thread.start();
destoryConnectionThread = new DestroyConnectionThread("Druid-ConnectionPool-Destory");
destoryConnectionThread.start();
}
protected void createAndStartCreatorThread() {
CreateConnectionThread thread = new CreateConnectionThread("Druid-ConnectionPool-Create");
thread.start();
createConnectionThread = new CreateConnectionThread("Druid-ConnectionPool-Create");
createConnectionThread.start();
}
private void loadFilterFromSystemProperty() throws SQLException {
@@ -815,7 +815,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
boolean neadDestory = false;
lock.lockInterruptibly();
try {
activeCount--;
@@ -826,11 +825,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
} finally {
lock.unlock();
}
if (neadDestory) {
destroyCount.incrementAndGet();
JdbcUtils.close(physicalConnection);
}
} catch (Throwable e) {
JdbcUtils.close(physicalConnection);
@@ -894,7 +888,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
connHolder.getStatementPool().getMap().clear();
JdbcUtils.close(connHolder.getConnection());
Connection physicalConnection = connHolder.getConnection();
physicalConnection.close();
connections[i] = null;
destroyCount.incrementAndGet();
} catch (Exception ex) {
@@ -1348,12 +1343,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public String getProperties() {
if (this.connectProperties == null) {
return null;
}
Properties properties = new Properties(this.connectProperties);
if (properties.contains("password")) {
Properties properties = new Properties();
properties.putAll(connectProperties);
if (properties.containsKey("password")) {
properties.put("password", "******");
}
return properties.toString();
@@ -1538,7 +1530,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
int entryIndex = 0;
try {
for (Map.Entry<PreparedStatementKey, PreparedStatementHolder> entry : pool.getMap().entrySet()) {
if (entryIndex++ != 0) {
if (entryIndex != 0) {
buf.append(",");
}
buf.append("\n\t\t{hitCount:");
@@ -1547,6 +1539,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
buf.append(entry.getKey().getSql());
buf.append("\"");
buf.append("\t}");
entryIndex++;
}
} catch (ConcurrentModificationException e) {
// skip ..
@@ -0,0 +1,41 @@
package com.alibaba.druid.bvt.pool;
import java.sql.SQLException;
import junit.framework.TestCase;
import com.alibaba.druid.filter.FilterAdapter;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
/**
* 这个场景测试initialSize > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_closeError extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setInitialSize(1);
dataSource.getProxyFilters().add(new FilterAdapter() {
public void connection_close(FilterChain chain, ConnectionProxy connection) throws SQLException {
throw new SQLException();
}
});
dataSource.init();
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_closeError() throws Exception {
}
}
@@ -29,6 +29,7 @@ public class DruidDataSourceTest_getPoolingPeakTime extends TestCase {
public void test_error() throws Exception {
Assert.assertNull(dataSource.getPoolingPeakTime());
Assert.assertNull(dataSource.getActivePeakTime());
Connection conn = dataSource.getConnection();
conn.close();
@@ -0,0 +1,34 @@
package com.alibaba.druid.bvt.pool;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 这个场景测试initialSize > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_getProperties extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.getConnectProperties().put("user", "jobs");
dataSource.getConnectProperties().put("password", "xxx");
dataSource.init();
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_error() throws Exception {
Assert.assertEquals(-1, dataSource.getProperties().indexOf("xxx"));
Assert.assertEquals(true, dataSource.getProperties().indexOf("******") != -1);
}
}
@@ -0,0 +1,79 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 这个场景测试initialSize > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_maxWait extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setMaxActive(1);
dataSource.setMaxWait(30);
dataSource.setInitialSize(1);
dataSource.init();
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_maxWait() throws Exception {
Connection conn = dataSource.getConnection();
final AtomicInteger errorCount = new AtomicInteger();
final CountDownLatch endLatch = new CountDownLatch(2);
Thread t1 = new Thread("t-1") {
public void run() {
try {
dataSource.getConnection();
} catch (Exception e) {
errorCount.incrementAndGet();
} finally {
endLatch.countDown();
}
}
};
Thread t2 = new Thread("t-2") {
public void run() {
try {
dataSource.getConnection();
} catch (Exception e) {
errorCount.incrementAndGet();
} finally {
endLatch.countDown();
}
}
};
t1.start();
t2.start();
Thread.sleep(10);
t1.interrupt();
endLatch.await();
Assert.assertEquals(2, errorCount.get());
conn.close();
}
}
@@ -2,11 +2,14 @@ package com.alibaba.druid.bvt.pool;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Assert;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.filter.FilterAdapter;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.pool.DruidDataSource;
@@ -43,7 +46,7 @@ public class DruidDataSourceTest_recycle2 extends TestCase {
Statement stmt = conn.createStatement();
stmt.execute("select 1");
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
@@ -60,7 +63,7 @@ public class DruidDataSourceTest_recycle2 extends TestCase {
Statement stmt = conn.createStatement();
stmt.execute("select 1");
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
@@ -75,4 +78,54 @@ public class DruidDataSourceTest_recycle2 extends TestCase {
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(0, dataSource.getActiveCount());
}
public void test_recycle_error_interrupt() throws Exception {
final AtomicReference<Exception> errorRef = new AtomicReference<Exception>();
final CountDownLatch closeBeforeLatch = new CountDownLatch(1);
final CountDownLatch lockLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(1);
Thread thread = new Thread() {
public void run() {
try {
DruidPooledConnection conn = dataSource.getConnection();
conn.setAutoCommit(false);
conn.setReadOnly(false);
Statement stmt = conn.createStatement();
stmt.execute("select 1");
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
closeBeforeLatch.countDown();
lockLatch.await();
conn.close();
} catch (Exception e) {
errorRef.set(e);
} finally {
endLatch.countDown();
}
}
};
thread.start();
Assert.assertTrue(closeBeforeLatch.await(1, TimeUnit.SECONDS));
dataSource.getLock().lock();
lockLatch.countDown();
Thread.sleep(10);
thread.interrupt();
Assert.assertTrue(endLatch.await(1, TimeUnit.MINUTES));
Exception error = errorRef.get();
Assert.assertNotNull(error);
Assert.assertTrue(error.getCause() instanceof InterruptedException);
}
}
@@ -60,6 +60,19 @@ public class DumpTest extends TestCase {
}
public void testToString() throws Exception {
{
String sql = "select ?";
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "xxx");
ResultSet rs = stmt.executeQuery();
rs.next();
rs.close();
conn.close();
}
String sql = "select ?, ?";
Connection conn = dataSource.getConnection();