mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
@@ -416,13 +416,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
connectError = ex;
|
||||
}
|
||||
|
||||
createConnectionThread = new CreateConnectionThread("Druid-ConnectionPool-Create");
|
||||
createConnectionThread.setDaemon(true);
|
||||
destoryConnectionThread = new DestroyConnectionThread("Druid-ConnectionPool-Destory");
|
||||
destoryConnectionThread.setDaemon(true);
|
||||
|
||||
createConnectionThread.start();
|
||||
destoryConnectionThread.start();
|
||||
createAndStartCreatorThread();
|
||||
createAndStartDestroyThread();
|
||||
|
||||
initedLatch.await();
|
||||
|
||||
@@ -448,6 +443,16 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
private void createAndStartDestroyThread() {
|
||||
DestroyConnectionThread thread = new DestroyConnectionThread("Druid-ConnectionPool-Destory");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
protected void createAndStartCreatorThread() {
|
||||
CreateConnectionThread thread = new CreateConnectionThread("Druid-ConnectionPool-Create");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void loadFilterFromSystemProperty() throws SQLException {
|
||||
String property = System.getProperty("druid.filters");
|
||||
|
||||
@@ -1129,6 +1134,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
public CreateConnectionThread(String name){
|
||||
super(name);
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -1222,6 +1228,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
public DestroyConnectionThread(String name){
|
||||
super(name);
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.PooledConnection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.mock.MockStatementBase;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ExceptionSorter;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_exceptionSorter extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setRemoveAbandoned(true);
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
});
|
||||
dataSource.setExceptionSorter(new ExceptionSorter() {
|
||||
|
||||
@Override
|
||||
public boolean isExceptionFatal(SQLException e) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_event_error() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement("select ?");
|
||||
try {
|
||||
stmt.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
|
||||
}
|
||||
|
||||
conn.close();
|
||||
}
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(2, dataSource.getCreateCount());
|
||||
Assert.assertEquals(1, dataSource.getDiscardCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.sql.ConnectionEvent;
|
||||
import javax.sql.ConnectionEventListener;
|
||||
import javax.sql.PooledConnection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.mock.MockStatementBase;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_getPooledConnection extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setRemoveAbandoned(true);
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_conn() throws Exception {
|
||||
PooledConnection conn = dataSource.getPooledConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_conn_1() throws Exception {
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.getPooledConnection(null, null);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
public void test_event_error() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getPooledConnection();
|
||||
|
||||
final AtomicInteger errorCount = new AtomicInteger();
|
||||
conn.addConnectionEventListener(new ConnectionEventListener() {
|
||||
|
||||
@Override
|
||||
public void connectionErrorOccurred(ConnectionEvent event) {
|
||||
errorCount.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionClosed(ConnectionEvent event) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement("select ?");
|
||||
try {
|
||||
stmt.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, errorCount.get());
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
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;
|
||||
|
||||
public class DruidDataSourceTest_interrupt extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource() {
|
||||
|
||||
protected void createAndStartCreatorThread() {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(true);
|
||||
dataSource.setInitialSize(1);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch endLatch = new CountDownLatch(1);
|
||||
final AtomicInteger errorCount = new AtomicInteger();
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
startLatch.countDown();
|
||||
dataSource.init();
|
||||
} catch (Exception e) {
|
||||
errorCount.incrementAndGet();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
startLatch.await();
|
||||
|
||||
Thread.sleep(10);
|
||||
|
||||
Assert.assertEquals(0, errorCount.get());
|
||||
|
||||
thread.interrupt();
|
||||
|
||||
endLatch.await();
|
||||
Assert.assertEquals(1, errorCount.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
|
||||
public class DruidDataSourceTest_interrupt2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(true);
|
||||
dataSource.setInitialSize(1);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
dataSource.getLock().lock();
|
||||
|
||||
try {
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch endLatch = new CountDownLatch(1);
|
||||
final AtomicInteger errorCount = new AtomicInteger();
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
startLatch.countDown();
|
||||
dataSource.getConnection();
|
||||
} catch (Exception e) {
|
||||
errorCount.incrementAndGet();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
startLatch.await();
|
||||
|
||||
Thread.sleep(10);
|
||||
|
||||
Assert.assertEquals(0, errorCount.get());
|
||||
|
||||
thread.interrupt();
|
||||
|
||||
endLatch.await();
|
||||
Assert.assertEquals(1, errorCount.get());
|
||||
} finally {
|
||||
dataSource.getLock().unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidDataSourceTest_interrupt3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(true);
|
||||
dataSource.setMaxActive(1);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch endLatch = new CountDownLatch(1);
|
||||
final AtomicInteger errorCount = new AtomicInteger();
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
startLatch.countDown();
|
||||
dataSource.getConnection();
|
||||
} catch (Exception e) {
|
||||
errorCount.incrementAndGet();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
startLatch.await();
|
||||
|
||||
Thread.sleep(10);
|
||||
|
||||
Assert.assertEquals(0, errorCount.get());
|
||||
|
||||
thread.interrupt();
|
||||
|
||||
endLatch.await();
|
||||
Assert.assertEquals(1, errorCount.get());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.sql.PooledConnection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_testOnBorrowFailed extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private AtomicInteger validCount = new AtomicInteger();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(true);
|
||||
dataSource.setValidationQuery("select 'x'");
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionChecker() {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
int count = validCount.getAndIncrement();
|
||||
|
||||
if (count == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (count == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_conn() throws Exception {
|
||||
PooledConnection conn = dataSource.getPooledConnection();
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(2, dataSource.getCreateCount());
|
||||
Assert.assertEquals(1, dataSource.getDiscardCount());
|
||||
Assert.assertEquals(2, dataSource.getConnectCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.sql.PooledConnection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_testOnWhileIdleFailed extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private AtomicInteger validCount = new AtomicInteger();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setTestWhileIdle(true);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(20);
|
||||
dataSource.setValidationQuery("select 'x'");
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionChecker() {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
int count = validCount.getAndIncrement();
|
||||
|
||||
if (count == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (count == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_conn() throws Exception {
|
||||
{
|
||||
PooledConnection conn = dataSource.getPooledConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, validCount.get()); // createValidate
|
||||
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(1, dataSource.getCreateCount());
|
||||
Assert.assertEquals(0, dataSource.getDiscardCount());
|
||||
Assert.assertEquals(1, dataSource.getConnectCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
|
||||
Thread.sleep(21);
|
||||
|
||||
{
|
||||
PooledConnection conn = dataSource.getPooledConnection();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(3, validCount.get()); // createValidate
|
||||
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(2, dataSource.getCreateCount());
|
||||
Assert.assertEquals(1, dataSource.getDiscardCount());
|
||||
Assert.assertEquals(3, dataSource.getConnectCount());
|
||||
Assert.assertEquals(2, dataSource.getCloseCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user