From 10de685d050ce38f3a05d16d3c3f7f78256d47fb Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 25 Aug 2012 15:25:25 +0800 Subject: [PATCH] add testcase & bug fixed --- .../druid/pool/DruidAbstractDataSource.java | 222 ++++++------------ .../DruidDataSourceTest_lastCreateError.java | 60 +++++ .../pool/DruidDataSourceTest_lastError.java | 62 +++++ .../bvt/pool/DruidDataSourceTest_oracle2.java | 8 +- 4 files changed, 202 insertions(+), 150 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastCreateError.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastError.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java index be775705bb..e86956c2d2 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java @@ -241,7 +241,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements this.useOracleImplicitCache = useOracleImplicitCache; boolean isOracleDriver10 = isOracle() && this.driver != null && this.driver.getMajorVersion() == 10; - if (isOracleDriver10) { + if (isOracleDriver10 && useOracleImplicitCache) { this.getConnectProperties().setProperty("oracle.jdbc.FreeMemoryOnEnterImplicitCache", "true"); } else { this.getConnectProperties().remove("oracle.jdbc.FreeMemoryOnEnterImplicitCache"); @@ -274,7 +274,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements } public Date getLastCreateErrorTime() { - if (lastErrorTimeMillis <= 0) { + if (lastCreateErrorTimeMillis <= 0) { return null; } @@ -1329,162 +1329,96 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements map.put("RawDriverMinorVersion", getRawDriverMinorVersion()); map.put("Properties", getProperties()); - if (stat != null) { - // 0 - 4 - map.put("ConnectionActiveCount", stat.getConnectionActiveCount()); - map.put("ConnectionActiveCountMax", stat.getConnectionStat().getActiveMax()); - map.put("ConnectionCloseCount", stat.getConnectionStat().getCloseCount()); - map.put("ConnectionCommitCount", stat.getConnectionStat().getCommitCount()); - map.put("ConnectionRollbackCount", stat.getConnectionStat().getRollbackCount()); - - // 5 - 9 - map.put("ConnectionConnectLastTime", stat.getConnectionStat().getConnectLastTime()); - map.put("ConnectionConnectErrorCount", stat.getConnectionStat().getConnectErrorCount()); - Throwable lastConnectionConnectError = stat.getConnectionStat().getConnectErrorLast(); - if (lastConnectionConnectError != null) { - map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime()); - map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage()); - map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionConnectError)); - } else { - map.put("ConnectionConnectErrorLastTime", null); - map.put("ConnectionConnectErrorLastMessage", null); - map.put("ConnectionConnectErrorLastStackTrace", null); - } - - // 10 - 14 - map.put("StatementCreateCount", stat.getStatementStat().getCreateCount()); - map.put("StatementPrepareCount", stat.getStatementStat().getPrepareCount()); - map.put("StatementPreCallCount", stat.getStatementStat().getPrepareCallCount()); - map.put("StatementExecuteCount", stat.getStatementStat().getExecuteCount()); - map.put("StatementRunningCount", stat.getStatementStat().getRunningCount()); - - // 15 -24 - map.put("StatementConcurrentMax", stat.getStatementStat().getConcurrentMax()); - map.put("StatementCloseCount", stat.getStatementStat().getCloseCount()); - map.put("StatementErrorCount", stat.getStatementStat().getErrorCount()); - Throwable lastStatementError = stat.getStatementStat().getLastException(); - if (lastStatementError != null) { - map.put("StatementLastErrorTime", stat.getStatementStat().getLastErrorTime()); - map.put("StatementLastErrorMessage", lastStatementError.getMessage()); - - map.put("StatementLastErrorStackTrace", IOUtils.getStackTrace(lastStatementError)); - } else { - map.put("StatementLastErrorTime", null); - map.put("StatementLastErrorMessage", null); - - map.put("StatementLastErrorStackTrace", null); - } - map.put("StatementExecuteMillisTotal", stat.getStatementStat().getMillisTotal()); - map.put("StatementExecuteLastTime", stat.getStatementStat().getExecuteLastTime()); - map.put("ConnectionConnectingCount", stat.getConnectionStat().getConnectingCount()); - map.put("ResultSetCloseCount", stat.getResultSetStat().getCloseCount()); - - // 25 - 29 - map.put("ResultSetOpenCount", stat.getResultSetStat().getOpenCount()); - map.put("ResultSetOpenningCount", stat.getResultSetStat().getOpenningCount()); - map.put("ResultSetOpenningMax", stat.getResultSetStat().getOpenningMax()); - map.put("ResultSetFetchRowCount", stat.getResultSetStat().getFetchRowCount()); - map.put("ResultSetLastOpenTime", stat.getResultSetStat().getLastOpenTime()); - - // 30 - 34 - map.put("ResultSetErrorCount", stat.getResultSetStat().getErrorCount()); - map.put("ResultSetOpenningMillisTotal", stat.getResultSetStat().getAliveMillisTotal()); - map.put("ResultSetLastErrorTime", stat.getResultSetStat().getLastErrorTime()); - Throwable lastResultSetError = stat.getResultSetStat().getLastError(); - if (lastResultSetError != null) { - map.put("ResultSetLastErrorMessage", lastResultSetError.getMessage()); - map.put("ResultSetLastErrorStackTrace", IOUtils.getStackTrace(lastResultSetError)); - } else { - map.put("ResultSetLastErrorMessage", null); - map.put("ResultSetLastErrorStackTrace", null); - } - - // 35 - 39 - map.put("ConnectionConnectCount", stat.getConnectionStat().getConnectCount()); - Throwable lastConnectionError = stat.getConnectionStat().getErrorLast(); - if (lastConnectionError != null) { - map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage()); - map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionError)); - } else { - map.put("ConnectionErrorLastMessage", null); - map.put("ConnectionErrorLastStackTrace", null); - } - map.put("ConnectionConnectMillisTotal", stat.getConnectionStat().getConnectMillis()); - map.put("ConnectionConnectingCountMax", stat.getConnectionStat().getConnectingMax()); - - // 40 - 44 - map.put("ConnectionConnectMillisMax", stat.getConnectionStat().getConnectMillisMax()); - map.put("ConnectionErrorLastTime", stat.getConnectionStat().getErrorLastTime()); - map.put("ConnectionAliveMillisMax", stat.getConnectionConnectAliveMillisMax()); - map.put("ConnectionAliveMillisMin", stat.getConnectionConnectAliveMillisMin()); - - map.put("ConnectionHistogram", stat.getConnectionHistogramValues()); - map.put("StatementHistogram", stat.getStatementStat().getHistogramValues()); + // 0 - 4 + map.put("ConnectionActiveCount", stat.getConnectionActiveCount()); + map.put("ConnectionActiveCountMax", stat.getConnectionStat().getActiveMax()); + map.put("ConnectionCloseCount", stat.getConnectionStat().getCloseCount()); + map.put("ConnectionCommitCount", stat.getConnectionStat().getCommitCount()); + map.put("ConnectionRollbackCount", stat.getConnectionStat().getRollbackCount()); + // 5 - 9 + map.put("ConnectionConnectLastTime", stat.getConnectionStat().getConnectLastTime()); + map.put("ConnectionConnectErrorCount", stat.getConnectionStat().getConnectErrorCount()); + Throwable lastConnectionConnectError = stat.getConnectionStat().getConnectErrorLast(); + if (lastConnectionConnectError != null) { + map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime()); + map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage()); + map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionConnectError)); } else { - // 0 - 4 - map.put("ConnectionActiveCount", null); - map.put("ConnectionActiveCountMax", null); - map.put("ConnectionCloseCount", null); - map.put("ConnectionCommitCount", null); - map.put("ConnectionRollbackCount", null); - - // 5 - 9 - map.put("ConnectionConnectLastTime", null); - map.put("ConnectionConnectErrorCount", null); map.put("ConnectionConnectErrorLastTime", null); map.put("ConnectionConnectErrorLastMessage", null); map.put("ConnectionConnectErrorLastStackTrace", null); + } - // 10 - 14 - map.put("StatementCreateCount", null); - map.put("StatementPrepareCount", null); - map.put("StatementPreCallCount", null); - map.put("StatementExecuteCount", null); - map.put("StatementRunningCount", null); + // 10 - 14 + map.put("StatementCreateCount", stat.getStatementStat().getCreateCount()); + map.put("StatementPrepareCount", stat.getStatementStat().getPrepareCount()); + map.put("StatementPreCallCount", stat.getStatementStat().getPrepareCallCount()); + map.put("StatementExecuteCount", stat.getStatementStat().getExecuteCount()); + map.put("StatementRunningCount", stat.getStatementStat().getRunningCount()); - // 15 - 19 - map.put("StatementConcurrentMax", null); - map.put("StatementCloseCount", null); - map.put("StatementErrorCount", null); + // 15 -24 + map.put("StatementConcurrentMax", stat.getStatementStat().getConcurrentMax()); + map.put("StatementCloseCount", stat.getStatementStat().getCloseCount()); + map.put("StatementErrorCount", stat.getStatementStat().getErrorCount()); + Throwable lastStatementError = stat.getStatementStat().getLastException(); + if (lastStatementError != null) { + map.put("StatementLastErrorTime", stat.getStatementStat().getLastErrorTime()); + map.put("StatementLastErrorMessage", lastStatementError.getMessage()); + + map.put("StatementLastErrorStackTrace", IOUtils.getStackTrace(lastStatementError)); + } else { map.put("StatementLastErrorTime", null); map.put("StatementLastErrorMessage", null); - // 20 - 24 map.put("StatementLastErrorStackTrace", null); - map.put("StatementExecuteMillisTotal", null); - map.put("ConnectionConnectingCount", null); - map.put("StatementExecuteLastTime", null); - map.put("ResultSetCloseCount", null); + } + map.put("StatementExecuteMillisTotal", stat.getStatementStat().getMillisTotal()); + map.put("StatementExecuteLastTime", stat.getStatementStat().getExecuteLastTime()); + map.put("ConnectionConnectingCount", stat.getConnectionStat().getConnectingCount()); + map.put("ResultSetCloseCount", stat.getResultSetStat().getCloseCount()); - // 25 - 29 - map.put("ResultSetOpenCount", null); - map.put("ResultSetOpenningCount", null); - map.put("ResultSetOpenningMax", null); - map.put("ResultSetFetchRowCount", null); - map.put("ResultSetLastOpenTime", null); + // 25 - 29 + map.put("ResultSetOpenCount", stat.getResultSetStat().getOpenCount()); + map.put("ResultSetOpenningCount", stat.getResultSetStat().getOpenningCount()); + map.put("ResultSetOpenningMax", stat.getResultSetStat().getOpenningMax()); + map.put("ResultSetFetchRowCount", stat.getResultSetStat().getFetchRowCount()); + map.put("ResultSetLastOpenTime", stat.getResultSetStat().getLastOpenTime()); - // 30 - 34 - map.put("ResultSetErrorCount", null); - map.put("ResultSetOpenningMillisTotal", null); - map.put("ResultSetLastErrorTime", null); + // 30 - 34 + map.put("ResultSetErrorCount", stat.getResultSetStat().getErrorCount()); + map.put("ResultSetOpenningMillisTotal", stat.getResultSetStat().getAliveMillisTotal()); + map.put("ResultSetLastErrorTime", stat.getResultSetStat().getLastErrorTime()); + Throwable lastResultSetError = stat.getResultSetStat().getLastError(); + if (lastResultSetError != null) { + map.put("ResultSetLastErrorMessage", lastResultSetError.getMessage()); + map.put("ResultSetLastErrorStackTrace", IOUtils.getStackTrace(lastResultSetError)); + } else { map.put("ResultSetLastErrorMessage", null); map.put("ResultSetLastErrorStackTrace", null); + } - map.put("ConnectionConnectCount", null); + // 35 - 39 + map.put("ConnectionConnectCount", stat.getConnectionStat().getConnectCount()); + Throwable lastConnectionError = stat.getConnectionStat().getErrorLast(); + if (lastConnectionError != null) { + map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage()); + map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionError)); + } else { map.put("ConnectionErrorLastMessage", null); map.put("ConnectionErrorLastStackTrace", null); - map.put("ConnectionConnectMillisTotal", null); - map.put("ConnectionConnectingCountMax", null); - - map.put("ConnectionConnectMillisMax", null); - map.put("ConnectionErrorLastTime", null); - map.put("ConnectionAliveMillisMax", null); - map.put("ConnectionAliveMillisMin", null); - - map.put("ConnectionHistogram", new long[0]); - map.put("StatementHistogram", new long[0]); } + map.put("ConnectionConnectMillisTotal", stat.getConnectionStat().getConnectMillis()); + map.put("ConnectionConnectingCountMax", stat.getConnectionStat().getConnectingMax()); + + // 40 - 44 + map.put("ConnectionConnectMillisMax", stat.getConnectionStat().getConnectMillisMax()); + map.put("ConnectionErrorLastTime", stat.getConnectionStat().getErrorLastTime()); + map.put("ConnectionAliveMillisMax", stat.getConnectionConnectAliveMillisMax()); + map.put("ConnectionAliveMillisMin", stat.getConnectionConnectAliveMillisMin()); + + map.put("ConnectionHistogram", stat.getConnectionHistogramValues()); + map.put("StatementHistogram", stat.getStatementStat().getHistogramValues()); return new CompositeDataSupport(JdbcStatManager.getDataSourceCompositeType(), map); } @@ -1518,16 +1452,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements JdbcUtils.close(stmtHolder.getStatement()); } - public static boolean equals(Object object1, Object object2) { - if (object1 == object2) { - return true; - } - if ((object1 == null) || (object2 == null)) { - return false; - } - return object1.equals(object2); - } - protected void cloneTo(DruidAbstractDataSource to) { to.defaultAutoCommit = this.defaultAutoCommit; to.defaultReadOnly = this.defaultReadOnly; diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastCreateError.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastCreateError.java new file mode 100644 index 0000000000..0ad76b6282 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastCreateError.java @@ -0,0 +1,60 @@ +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; + +/** + * 这个场景测试minIdle > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_lastCreateError extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + + dataSource.getProxyFilters().add(new FilterAdapter() { + + public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException { + throw new SQLException(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_error() throws Exception { + Assert.assertNull(dataSource.getLastCreateError()); + Assert.assertNull(dataSource.getLastCreateErrorTime()); + Assert.assertEquals(0, dataSource.getLastCreateErrorTimeMillis()); + + Exception error = null; + try { + dataSource.getConnection(100); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertNotNull(dataSource.getLastCreateError()); + Assert.assertNotNull(dataSource.getLastCreateErrorTime()); + Assert.assertEquals(true, dataSource.getLastCreateErrorTimeMillis() > 0); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastError.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastError.java new file mode 100644 index 0000000000..f058464fe9 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_lastError.java @@ -0,0 +1,62 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.SQLException; + +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; + +/** + * 这个场景测试minIdle > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_lastError extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + + dataSource.getProxyFilters().add(new FilterAdapter() { + + public void connection_setAutoCommit(FilterChain chain, ConnectionProxy connection, boolean autoCommit) + throws SQLException { + throw new SQLException(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_error() throws Exception { + Assert.assertNull(dataSource.getLastError()); + Assert.assertNull(dataSource.getLastErrorTime()); + Assert.assertEquals(0, dataSource.getLastErrorTimeMillis()); + + Connection conn = dataSource.getConnection(); + + Exception error = null; + try { + conn.setAutoCommit(false); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertNotNull(dataSource.getLastError()); + Assert.assertNotNull(dataSource.getLastErrorTime()); + Assert.assertEquals(true, dataSource.getLastErrorTimeMillis() > 0); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_oracle2.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_oracle2.java index bb9007ce00..06828f04d4 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_oracle2.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_oracle2.java @@ -35,10 +35,16 @@ public class DruidDataSourceTest_oracle2 extends TestCase { dataSource.close(); } - public void test_error() throws Exception { + public void test_oracle() 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")); + + dataSource.setUseOracleImplicitCache(false); + Assert.assertNull(dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); + + dataSource.setUseOracleImplicitCache(true); + Assert.assertEquals("true", dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); } }