From 841534ed432a16cfee89d5d4b62e8e30cd05a03b Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 09:45:27 +0800 Subject: [PATCH] add testcase & bug fixed for DruidConnectionHolderInitError --- .../druid/pool/DruidConnectionHolder.java | 5 +- .../alibaba/druid/pool/DruidDataSource.java | 12 +++- .../bvt/pool/DruidConnectionHolderTest.java | 7 +- .../bvt/pool/DruidConnectionHolderTest2.java | 46 +++++++++++++ .../bvt/pool/DruidConnectionHolderTest3.java | 62 ++++++++++++++++++ .../bvt/pool/PreparedStatementKeyTest.java | 65 +++++++++++++++++++ 6 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest2.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest3.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/PreparedStatementKeyTest.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java b/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java index f8dba8ef5b..035d52fdf6 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java +++ b/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java @@ -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; diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 88c2883f57..148732e33f 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -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 { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest.java index 371499c352..b7431245ff 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest.java @@ -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(); } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest2.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest2.java new file mode 100644 index 0000000000..a490bd1ae6 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest2.java @@ -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); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest3.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest3.java new file mode 100644 index 0000000000..c3c1d28082 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest3.java @@ -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 { + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/PreparedStatementKeyTest.java b/src/test/java/com/alibaba/druid/bvt/pool/PreparedStatementKeyTest.java new file mode 100644 index 0000000000..62d79ffffc --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/PreparedStatementKeyTest.java @@ -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))); + + } +}