add testcase & bug fixed for DruidConnectionHolderInitError

This commit is contained in:
wenshao
2012-08-28 09:45:27 +08:00
parent 0c47d2a38d
commit 841534ed43
6 changed files with 189 additions and 8 deletions
@@ -70,8 +70,9 @@ public final class DruidConnectionHolder {
try {
this.underlyingTransactionIsolation = conn.getTransactionIsolation();
} catch (SQLException e) {
if ("com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException".equals(e.getClass().getName()))
;
if (!"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException".equals(e.getClass().getName())) {
throw e;
}
}
this.defaultHoldability = underlyingHoldability;
@@ -403,7 +403,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
// 初始化连接
for (int i = 0, size = getInitialSize(); i < size; ++i) {
Connection conn = createPhysicalConnection();
connections[poolingCount++] = new DruidConnectionHolder(this, conn);
DruidConnectionHolder holder = new DruidConnectionHolder(this, conn);
connections[poolingCount++] = holder;
}
if (poolingCount > 0) {
@@ -721,7 +722,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
} finally {
lock.unlock();
}
holder.incrementUseCount();
DruidPooledConnection poolalbeConnection = new DruidPooledConnection(holder);
@@ -1175,7 +1176,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.lock();
try {
connections[poolingCount++] = new DruidConnectionHolder(DruidDataSource.this, connection);
DruidConnectionHolder holder = new DruidConnectionHolder(DruidDataSource.this, connection);
connections[poolingCount++] = holder;
if (poolingCount > poolingPeak) {
poolingPeak = poolingCount;
@@ -1209,6 +1211,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
for (;;) {
// 从前面开始删除
try {
if (closed) {
break;
}
if (timeBetweenEvictionRunsMillis > 0) {
Thread.sleep(timeBetweenEvictionRunsMillis);
} else {
@@ -18,7 +18,7 @@ public class DruidConnectionHolderTest extends TestCase {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setPoolPreparedStatements(true);
dataSource.setInitialSize(1);
dataSource.getProxyFilters().add(new FilterAdapter() {
public int connection_getTransactionIsolation(FilterChain chain, ConnectionProxy connection)
@@ -34,7 +34,8 @@ public class DruidConnectionHolderTest extends TestCase {
}
public void test_mysqlSyntaxError() throws Exception {
Connection conn = dataSource.getConnection();
conn.close();
dataSource.init();
dataSource.getConnection();
}
}
@@ -0,0 +1,46 @@
package com.alibaba.druid.bvt.pool;
import java.sql.SQLException;
import junit.framework.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.pool.GetConnectionTimeoutException;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
public class DruidConnectionHolderTest2 extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setPoolPreparedStatements(true);
dataSource.getProxyFilters().add(new FilterAdapter() {
public int connection_getTransactionIsolation(FilterChain chain, ConnectionProxy connection)
throws SQLException {
throw new SQLException();
}
});
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_mysqlSyntaxError() throws Exception {
Exception error = null;
try {
dataSource.getConnection(100);
} catch (GetConnectionTimeoutException e) {
error = e;
}
Assert.assertNotNull(error);
}
}
@@ -0,0 +1,62 @@
package com.alibaba.druid.bvt.pool;
import java.sql.SQLException;
import junit.framework.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.pool.GetConnectionTimeoutException;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
public class DruidConnectionHolderTest3 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 int connection_getTransactionIsolation(FilterChain chain, ConnectionProxy connection)
throws SQLException {
throw new MySQLException();
}
});
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_mysqlSyntaxError() throws Exception {
{
Exception error = null;
try {
dataSource.init();
} catch (MySQLException e) {
error = e;
}
Assert.assertNotNull(error);
}
Assert.assertEquals(0, dataSource.getPoolingCount());
Exception error = null;
try {
dataSource.getConnection(100);
} catch (GetConnectionTimeoutException e) {
error = e;
}
Assert.assertNotNull(error);
}
public static class MySQLException extends SQLException {
}
}
@@ -0,0 +1,65 @@
package com.alibaba.druid.bvt.pool;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
public class PreparedStatementKeyTest extends TestCase {
public void test_0() throws Exception {
PreparedStatementKey k1 = new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103);
Assert.assertEquals(101, k1.getResultSetType());
Assert.assertEquals(102, k1.getResultSetConcurrency());
Assert.assertEquals(103, k1.getResultSetHoldability());
}
public void test_eq() throws Exception {
Assert.assertEquals(new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103),
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103));
}
public void test_not_eq() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 201, 102, 103)));
}
public void test_not_eq_1() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 202, 103)));
}
public void test_not_eq_2() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 203)));
}
public void test_not_eq_3() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M2, 101, 102, 103)));
}
public void test_not_eq_4() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'a'", "c2", MethodType.M1, 101, 102, 103)));
}
public void test_not_eq_5() throws Exception {
Assert.assertFalse( //
new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103).equals( //
new PreparedStatementKey("select 'b'", "c1", MethodType.M1, 101, 102, 103)));
}
}