mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add testcase & bug fixed
This commit is contained in:
@@ -1297,7 +1297,10 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
public void initPhysicalConnection(Connection conn) throws SQLException {
|
||||
conn.setAutoCommit(isDefaultAutoCommit());
|
||||
if (conn.getAutoCommit() != defaultAutoCommit) {
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
}
|
||||
|
||||
if (getDefaultReadOnly() != null) {
|
||||
if (conn.isReadOnly() != getDefaultReadOnly()) {
|
||||
conn.setReadOnly(getDefaultReadOnly());
|
||||
|
||||
@@ -347,7 +347,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (getInitialSize() > maxActive) {
|
||||
throw new IllegalArgumentException("illegal initialSize");
|
||||
throw new IllegalArgumentException("illegal initialSize " + this.initialSize + ", maxActieve " + maxActive);
|
||||
}
|
||||
|
||||
if (this.driverClass != null) {
|
||||
@@ -402,9 +402,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// 初始化连接
|
||||
for (int i = 0, size = getInitialSize(); i < size; ++i) {
|
||||
Connection conn = createPhysicalConnection();
|
||||
if (defaultAutoCommit != conn.getAutoCommit()) {
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
}
|
||||
connections[poolingCount++] = new DruidConnectionHolder(this, conn);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -18,7 +18,7 @@ import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest6 extends TestCase {
|
||||
public class DruidDataSourceTest_autocommit extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
@@ -27,6 +27,7 @@ public class DruidDataSourceTest6 extends TestCase {
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(true);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy conn = chain.connection_connect(info);
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_autocommit2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(false);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy conn = chain.connection_connect(info);
|
||||
conn.setAutoCommit(true);
|
||||
return conn;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertFalse(conn.getAutoCommit());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_autocommit3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDefaultAutoCommit(false);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy conn = chain.connection_connect(info);
|
||||
conn.setAutoCommit(false);
|
||||
return conn;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertFalse(conn.getAutoCommit());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_autocommit4 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);
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy conn = chain.connection_connect(info);
|
||||
conn.setAutoCommit(true);
|
||||
return conn;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_autoCommit() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertTrue(conn.getAutoCommit());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
/**
|
||||
* 这个场景测试initialSize > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_getPoolingPeakTime extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
Assert.assertNull(dataSource.getPoolingPeakTime());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
|
||||
Assert.assertNotNull(dataSource.getPoolingPeakTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
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_maxActive1 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setInitialSize(3);
|
||||
dataSource.setMaxActive(1);
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.init();
|
||||
} catch (IllegalArgumentException e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest4 extends TestCase {
|
||||
public class DruidDataSourceTest_maxActive2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
+3
-2
@@ -10,7 +10,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest5 extends TestCase {
|
||||
public class DruidDataSourceTest_maxActive3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
@@ -19,7 +19,8 @@ public class DruidDataSourceTest5 extends TestCase {
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setInitialSize(100);
|
||||
dataSource.setMinIdle(100);
|
||||
dataSource.setMaxActive(10);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
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_maxActive4 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setMinIdle(4);
|
||||
dataSource.setMaxActive(8);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
dataSource.init();
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.setMaxActive(2);
|
||||
} catch (IllegalArgumentException e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
/**
|
||||
* 这个场景测试maxActive < 0
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_notEmptyWait extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setMaxActive(1);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
Assert.assertEquals(1, dataSource.getNotEmptyWaitCount());
|
||||
}
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
Assert.assertEquals(1, dataSource.getNotEmptyWaitCount()); // notEmptyWaitCount没有增长
|
||||
}
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
final int THREAD_COUNT = 10;
|
||||
final CountDownLatch startLatch = new CountDownLatch(THREAD_COUNT);
|
||||
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
|
||||
Thread[] threads = new Thread[THREAD_COUNT];
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads[i] = new Thread("thread-" + i) {
|
||||
|
||||
public void run() {
|
||||
startLatch.countDown();
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
startLatch.await(100, TimeUnit.MILLISECONDS);
|
||||
|
||||
Assert.assertEquals(10, dataSource.getNotEmptyWaitThreadCount());
|
||||
Assert.assertEquals(10, dataSource.getNotEmptyWaitThreadPeak());
|
||||
|
||||
conn.close();
|
||||
|
||||
endLatch.await(100, TimeUnit.MILLISECONDS);
|
||||
Assert.assertEquals(0, dataSource.getNotEmptyWaitThreadCount());
|
||||
Assert.assertEquals(10, dataSource.getNotEmptyWaitThreadPeak());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
/**
|
||||
* 这个场景测试minIdle > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_oracle extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setDbType("oracle");
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 11;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
dataSource.init();
|
||||
Assert.assertTrue(dataSource.isOracle());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
/**
|
||||
* 这个场景测试minIdle > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_oracle2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setDbType("oracle");
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 10;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
Assert.assertNull(dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache"));
|
||||
dataSource.init();
|
||||
Assert.assertTrue(dataSource.isOracle());
|
||||
Assert.assertEquals("true", dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
|
||||
|
||||
/**
|
||||
* 这个场景测试minIdle > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_oracle3 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:oracle:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
dataSource.init();
|
||||
Assert.assertTrue(dataSource.isOracle());
|
||||
Assert.assertTrue(dataSource.getValidConnectionChecker() instanceof OracleValidConnectionChecker);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
/**
|
||||
* 这个场景测试minIdle > maxActive
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSourceTest_oracle_error extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setDbType("oracle");
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 9;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.init();
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user