From 0d4732835a244dba2a9c71f8a9c3618b97a79d51 Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 27 Aug 2012 17:44:54 +0800 Subject: [PATCH 01/55] websession-detail.html addColumn Principal --- .../resources/support/http/resources/websession-detail.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/support/http/resources/websession-detail.html b/src/main/resources/support/http/resources/websession-detail.html index cb4195341a..919811fe9d 100644 --- a/src/main/resources/support/http/resources/websession-detail.html +++ b/src/main/resources/support/http/resources/websession-detail.html @@ -24,6 +24,10 @@ UserAgent + + Principal + + CreateTime From 2db857b5fef307e7df819e39d7bd2063b208a9cb Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 27 Aug 2012 21:38:07 +0800 Subject: [PATCH 02/55] add blob open count stat --- .../alibaba/druid/filter/stat/StatFilter.java | 42 ++++++++++++++++++- .../alibaba/druid/pool/DruidDataSource.java | 3 +- .../druid/stat/JdbcDataSourceStat.java | 11 +++++ .../support/http/resources/datasource.html | 8 +++- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index 3562700a5c..b15ca04a7b 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -507,7 +507,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (index != 0) { out.writeComma(); } - + Object value = parameter.getValue(); if (value == null) { out.writeNull(); @@ -685,6 +685,30 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return clob; } + @Override + public Blob callableStatement_getBlob(FilterChain chain, CallableStatementProxy statement, int parameterIndex) + throws SQLException { + Blob blob = chain.callableStatement_getBlob(statement, parameterIndex); + + if (blob != null) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, blob); + } + + return blob; + } + + @Override + public Blob callableStatement_getBlob(FilterChain chain, CallableStatementProxy statement, String parameterName) + throws SQLException { + Blob blob = chain.callableStatement_getBlob(statement, parameterName); + + if (blob != null) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, blob); + } + + return blob; + } + @Override public Clob callableStatement_getClob(FilterChain chain, CallableStatementProxy statement, int parameterIndex) throws SQLException { @@ -776,6 +800,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { clobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (ClobProxy) obj); } + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (Blob) obj); + } + return obj; } @@ -788,6 +816,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { clobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (ClobProxy) obj); } + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (Blob) obj); + } + return obj; } @@ -801,6 +833,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { clobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (ClobProxy) obj); } + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (Blob) obj); + } + return obj; } @@ -808,6 +844,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { clobOpenAfter(dataSourceStat, rs.getStatementProxy(), clob); } + private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, Blob blob) { + dataSourceStat.incrementBlobOpenCount(); + } + private void clobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, ClobProxy clob) { dataSourceStat.incrementClobOpenCount(); diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index ab3eccbbbb..8189456805 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -815,7 +815,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat discardConnection(holder.getConnection()); return; } - + final long lastActiveTimeMillis = System.currentTimeMillis(); lock.lockInterruptibly(); try { @@ -1774,6 +1774,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat dataMap.put("ConnectionHoldTimeHistogram", this.getDataSourceStat().getConnectionHoldHistogram().toArray()); dataMap.put("RemoveAbandoned", this.isRemoveAbandoned()); dataMap.put("ClobOpenCount", this.getDataSourceStat().getClobOpenCount()); + dataMap.put("BlobOpenCount", this.getDataSourceStat().getBlobOpenCount()); return dataMap; } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java b/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java index a61281c526..3c95692c58 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java @@ -68,6 +68,8 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { private final AtomicLong clobOpenCount = new AtomicLong(); + private final AtomicLong blobOpenCount = new AtomicLong(); + public JdbcDataSourceStat(String name, String url){ this(name, url, null); } @@ -101,6 +103,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { } public void reset() { + blobOpenCount.set(0); clobOpenCount.set(0); connectionStat.reset(); @@ -318,4 +321,12 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { clobOpenCount.incrementAndGet(); } + public long getBlobOpenCount() { + return blobOpenCount.get(); + } + + public void incrementBlobOpenCount() { + blobOpenCount.incrementAndGet(); + } + } diff --git a/src/main/resources/support/http/resources/datasource.html b/src/main/resources/support/http/resources/datasource.html index eaa499d7bb..7170193d88 100644 --- a/src/main/resources/support/http/resources/datasource.html +++ b/src/main/resources/support/http/resources/datasource.html @@ -30,7 +30,9 @@ "NotEmptyWaitMillis", "WaitThreadCount", "StartTransactionCount", "TransactionHistogram", "PoolingCount", "PoolingPeak", "PoolingPeakTime", "ActiveCount", "ActivePeak", "ActivePeakTime", "LogicConnectCount", "LogicCloseCount", "LogicConnectErrorCount", "PhysicalConnectCount", "PhysicalCloseCount", "PhysicalConnectErrorCount", "ExecuteCount", "ErrorCount", "CommitCount", - "RollbackCount", "PSCacheAccessCount", "PSCacheHitCount", "PSCacheMissCount", "ConnectionHoldTimeHistogram"] + "RollbackCount", "PSCacheAccessCount", "PSCacheHitCount", "PSCacheMissCount", "ConnectionHoldTimeHistogram", + "ClobOpenCount", "BlobOpenCount" + ] var descs = ["Specify the username used when creating a new connection.", "The JDBC driver connection URL", "database type", "The fully qualifed name of the JDBC driver class", "All the fully qualifed name of the filter classes", " Test or not when borrow a connection", "Test or not when return a connection", @@ -42,7 +44,9 @@ "The usefull connection peak time", "The current active connection count", "The current active connection peak count", "The active connection peak time", "Total connect times from datasource", "Total close connect times from datasource", "Total connect error times", "Create physical connnection count", "Close physical connnection count", "Total physical connect error times", "", "", "", - "", "PerpareStatement access count", "PerpareStatement hit count", "PerpareStatement miss count", "The histogram values of connection hold time, [0-1 ms, 1-10 ms, 10-100 ms, 100ms-1s, 1-10 s, 10-100 s, 100-1000 s, >1000 s]"] + "", "PerpareStatement access count", "PerpareStatement hit count", "PerpareStatement miss count", "The histogram values of connection hold time, [0-1 ms, 1-10 ms, 10-100 ms, 100ms-1s, 1-10 s, 10-100 s, 100-1000 s, >1000 s]", + "", "" + ] return { init : function() { druid.common.buildHead(1); From 34b13e764f863f3a6659403ad6eb1f70503acf8b Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 27 Aug 2012 21:47:00 +0800 Subject: [PATCH 03/55] add blob open count stat --- .../alibaba/druid/filter/stat/StatFilter.java | 55 +++++++++++++++++++ .../druid/filter/stat/StatFilterContext.java | 7 ++- .../stat/StatFilterContextListener.java | 2 +- .../StatFilterContextListenerAdapter.java | 5 +- .../com/alibaba/druid/stat/JdbcSqlStat.java | 23 +++++++- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index b15ca04a7b..c9bb2dfbf1 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -708,6 +708,28 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return blob; } + + @Override + public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException { + Blob blob = chain.resultSet_getBlob(result, columnIndex); + + if (blob != null) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, blob); + } + + return blob; + } + + @Override + public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException { + Blob blob = chain.resultSet_getBlob(result, columnLabel); + + if (blob != null) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, blob); + } + + return blob; + } @Override public Clob callableStatement_getClob(FilterChain chain, CallableStatementProxy statement, int parameterIndex) @@ -740,6 +762,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); } + + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } return obj; } @@ -752,6 +778,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); } + + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } return obj; } @@ -763,6 +793,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); } + + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } return obj; } @@ -775,6 +809,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); } + + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } return obj; } @@ -787,6 +825,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (ClobProxy) obj); } + + if (obj instanceof Blob) { + blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (Blob) obj); + } return obj; } @@ -839,6 +881,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return obj; } + + private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, ResultSetProxy rs, Blob blob) { + blobOpenAfter(dataSourceStat, rs.getStatementProxy(), blob); + } private void clobOpenAfter(JdbcDataSourceStat dataSourceStat, ResultSetProxy rs, ClobProxy clob) { clobOpenAfter(dataSourceStat, rs.getStatementProxy(), clob); @@ -846,6 +892,15 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, Blob blob) { dataSourceStat.incrementBlobOpenCount(); + + if (stmt != null) { + JdbcSqlStat sqlStat = stmt.getSqlStat(); + if (sqlStat != null) { + sqlStat.incrementBlobOpenCount(); + } + } + + StatFilterContext.getInstance().blob_open(); } private void clobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, ClobProxy clob) { diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContext.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContext.java index ab3162abcc..97c84d5f89 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContext.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContext.java @@ -130,11 +130,12 @@ public class StatFilterContext { listener.clob_open(); } } - - public void clob_free() { + + public void blob_open() { for (int i = 0; i < listeners.size(); ++i) { StatFilterContextListener listener = listeners.get(i); - listener.clob_free(); + listener.blob_open(); } } + } diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListener.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListener.java index 218c24e229..2960431ca7 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListener.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListener.java @@ -43,5 +43,5 @@ public interface StatFilterContextListener { void clob_open(); - void clob_free(); + void blob_open(); } diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListenerAdapter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListenerAdapter.java index 47dcea1061..839fddaab9 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListenerAdapter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilterContextListenerAdapter.java @@ -81,11 +81,12 @@ public class StatFilterContextListenerAdapter implements StatFilterContextListen public void clob_open() { } - + @Override - public void clob_free() { + public void blob_open() { } + } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java index 5dcdfaf0aa..01770643e5 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java @@ -76,6 +76,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { private boolean removed = false; private final AtomicLong clobOpenCount = new AtomicLong(); + private final AtomicLong blobOpenCount = new AtomicLong(); private final Histogram histogram = new Histogram(new long[] { // // @@ -223,6 +224,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { updateCountHistogram.reset(); executeAndResultHoldTimeHistogram.reset(); + blobOpenCount.set(0); clobOpenCount.set(0); } @@ -272,6 +274,14 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { public void incrementClobOpenCount() { clobOpenCount.incrementAndGet(); } + + public long getBlobOpenCount() { + return blobOpenCount.get(); + } + + public void incrementBlobOpenCount() { + blobOpenCount.incrementAndGet(); + } public long getId() { return id; @@ -394,7 +404,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { long current = executeSpanNanoMax.get(); if (current < nanoSpan) { if (executeSpanNanoMax.compareAndSet(current, nanoSpan)) { - // 可能不准确,但是绝大多数情况下都会正确,性能换取一致性 + // 可能不准确,但是绝大多数情况下都会正确,性能换取一致性 executeNanoSpanMaxOccurTime = System.currentTimeMillis(); break; @@ -489,13 +499,15 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { SimpleType.LONG, // new ArrayType(SimpleType.LONG, true), // - // 30 - - + // 30 - 34 new ArrayType(SimpleType.LONG, true), // new ArrayType(SimpleType.LONG, true), // SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // + + // 35 - + SimpleType.LONG, // }; @@ -548,6 +560,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { "EffectedRowCountMax", // "FetchRowCountMax", // "ClobOpenCount", + + // 35 - + "BlobOpenCount", // }; @@ -619,6 +634,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { map.put("EffectedRowCountMax", getUpdateCountMax()); // 32 map.put("FetchRowCountMax", getFetchRowCountMax()); // 33 map.put("ClobOpenCount", getClobOpenCount()); // 34 + + map.put("BlobOpenCount", getBlobOpenCount()); // 35 return map; } From 9b63e9ece2e0b458e775c77b9e7001a483c291ab Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 27 Aug 2012 22:50:25 +0800 Subject: [PATCH 04/55] add stat readStringLength --- .../alibaba/druid/filter/stat/StatFilter.java | 47 ++++++++++++++----- .../druid/proxy/jdbc/ResultSetProxy.java | 4 ++ .../druid/proxy/jdbc/ResultSetProxyImpl.java | 18 +++++-- .../com/alibaba/druid/stat/JdbcSqlStat.java | 23 +++++++-- .../support/http/resources/sql-detail.html | 17 ++++++- 5 files changed, 88 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index c9bb2dfbf1..3698e0760e 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -573,6 +573,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { sqlStat.addFetchRowCount(fetchRowCount); long stmtExecuteNano = resultSet.getStatementProxy().getLastExecuteTimeNano(); sqlStat.addResultSetHoldTimeNano(stmtExecuteNano, nanos); + sqlStat.addStringReadLength(resultSet.getReadStringLength()); } } @@ -761,10 +762,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); - } - - if (obj instanceof Blob) { + } else if (obj instanceof Blob) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } else if (obj instanceof String) { + result.addReadStringLength(((String) obj).length()); } return obj; @@ -777,10 +778,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); - } - - if (obj instanceof Blob) { + } else if (obj instanceof Blob) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } else if (obj instanceof String) { + result.addReadStringLength(((String) obj).length()); } return obj; @@ -792,10 +793,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); - } - - if (obj instanceof Blob) { + } else if (obj instanceof Blob) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } else if (obj instanceof String) { + result.addReadStringLength(((String) obj).length()); } return obj; @@ -808,10 +809,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (ClobProxy) obj); - } - - if (obj instanceof Blob) { + } else if (obj instanceof Blob) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, (Blob) obj); + } else if (obj instanceof String) { + result.addReadStringLength(((String) obj).length()); } return obj; @@ -915,4 +916,26 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { StatFilterContext.getInstance().clob_open(); } + + @Override + public String resultSet_getString(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException { + String value = chain.resultSet_getString(result, columnIndex); + + if (value != null) { + result.addReadStringLength(value.length()); + } + + return value; + } + + @Override + public String resultSet_getString(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException { + String value = chain.resultSet_getString(result, columnLabel); + + if (value != null) { + result.addReadStringLength(value.length()); + } + + return value; + } } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java index f6a6ff7ef2..e7bd92d6ca 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java @@ -43,4 +43,8 @@ public interface ResultSetProxy extends ResultSet, WrapperProxy { void setConstructNano(); int getCloseCount(); + + void addReadStringLength(int length); + + long getReadStringLength(); } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java index 45017f8596..6e8791f065 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java @@ -51,11 +51,13 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro private final StatementProxy statement; private final String sql; - protected int cursorIndex = 0; - protected int fetchRowCount = 0; + protected int cursorIndex = 0; + protected int fetchRowCount = 0; protected long constructNano; protected final JdbcSqlStat sqlStat; - private int closeCount = 0; + private int closeCount = 0; + + private long readStringLength = 0; public ResultSetProxyImpl(StatementProxy statement, ResultSet resultSet, long id, String sql){ super(resultSet, id); @@ -1078,4 +1080,14 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro return closeCount; } + @Override + public void addReadStringLength(int length) { + this.readStringLength += length; + } + + @Override + public long getReadStringLength() { + return readStringLength; + } + } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java index 01770643e5..6a89f230fc 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java @@ -77,6 +77,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { private final AtomicLong clobOpenCount = new AtomicLong(); private final AtomicLong blobOpenCount = new AtomicLong(); + private final AtomicLong stringReadLength = new AtomicLong(); private final Histogram histogram = new Histogram(new long[] { // // @@ -226,6 +227,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { blobOpenCount.set(0); clobOpenCount.set(0); + stringReadLength.set(0); } public long getConcurrentMax() { @@ -274,14 +276,22 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { public void incrementClobOpenCount() { clobOpenCount.incrementAndGet(); } - + public long getBlobOpenCount() { return blobOpenCount.get(); } - + public void incrementBlobOpenCount() { blobOpenCount.incrementAndGet(); } + + public long getStringReadLength() { + return stringReadLength.get(); + } + + public void addStringReadLength(long length) { + this.stringReadLength.addAndGet(length); + } public long getId() { return id; @@ -505,9 +515,10 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // - + // 35 - SimpleType.LONG, // + SimpleType.LONG, // }; @@ -560,9 +571,10 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { "EffectedRowCountMax", // "FetchRowCountMax", // "ClobOpenCount", - + // 35 - "BlobOpenCount", + "StringReadLength", // }; @@ -634,8 +646,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { map.put("EffectedRowCountMax", getUpdateCountMax()); // 32 map.put("FetchRowCountMax", getFetchRowCountMax()); // 33 map.put("ClobOpenCount", getClobOpenCount()); // 34 - + map.put("BlobOpenCount", getBlobOpenCount()); // 35 + map.put("StringReadLength", getStringReadLength()); // 36 return map; } diff --git a/src/main/resources/support/http/resources/sql-detail.html b/src/main/resources/support/http/resources/sql-detail.html index 8cfaf38f1d..d1312510eb 100644 --- a/src/main/resources/support/http/resources/sql-detail.html +++ b/src/main/resources/support/http/resources/sql-detail.html @@ -85,7 +85,7 @@
-

Batch View:

+

Other View:

@@ -95,6 +95,18 @@ + + + + + + + + + + + +
BatchSizeMaxBatchSizeTotal
BlobOpenCount
ClobOpenCount
StringReadLength
Close @@ -141,6 +153,9 @@ $("#BatchSizeMax").text(sqlInfo.BatchSizeMax) $("#BatchSizeTotal").text(sqlInfo.BatchSizeTotal) + $("#BlobOpenCount").text(sqlInfo.BlobOpenCount) + $("#ClobOpenCount").text(sqlInfo.ClobOpenCount) + $("#StringReadLength").text(sqlInfo.StringReadLength) }, dataType: "json" }); From 7feed4ad47dc373de3350cafc15da36504d427d5 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 06:03:05 +0800 Subject: [PATCH 05/55] add readBytes Count --- .../alibaba/druid/filter/stat/StatFilter.java | 48 ++++++++++++++----- .../druid/proxy/jdbc/ResultSetProxy.java | 4 ++ .../druid/proxy/jdbc/ResultSetProxyImpl.java | 11 +++++ .../com/alibaba/druid/stat/JdbcSqlStat.java | 33 +++++++++---- .../support/http/resources/sql-detail.html | 11 +++-- 5 files changed, 81 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index 3698e0760e..d90d475a56 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -709,26 +709,26 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return blob; } - + @Override public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException { Blob blob = chain.resultSet_getBlob(result, columnIndex); - + if (blob != null) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, blob); } - + return blob; } @Override public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException { Blob blob = chain.resultSet_getBlob(result, columnLabel); - + if (blob != null) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), result, blob); } - + return blob; } @@ -826,7 +826,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { if (obj instanceof Clob) { clobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (ClobProxy) obj); } - + if (obj instanceof Blob) { blobOpenAfter(chain.getDataSource().getDataSourceStat(), statement, (Blob) obj); } @@ -882,7 +882,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return obj; } - + private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, ResultSetProxy rs, Blob blob) { blobOpenAfter(dataSourceStat, rs.getStatementProxy(), blob); } @@ -893,7 +893,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, Blob blob) { dataSourceStat.incrementBlobOpenCount(); - + if (stmt != null) { JdbcSqlStat sqlStat = stmt.getSqlStat(); if (sqlStat != null) { @@ -916,26 +916,48 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { StatFilterContext.getInstance().clob_open(); } - + @Override public String resultSet_getString(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException { String value = chain.resultSet_getString(result, columnIndex); - + if (value != null) { result.addReadStringLength(value.length()); } - + return value; } @Override public String resultSet_getString(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException { String value = chain.resultSet_getString(result, columnLabel); - + if (value != null) { result.addReadStringLength(value.length()); } - + + return value; + } + + @Override + public byte[] resultSet_getBytes(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException { + byte[] value = chain.resultSet_getBytes(result, columnIndex); + + if (value != null) { + result.addReadBytesLength(value.length); + } + + return value; + } + + @Override + public byte[] resultSet_getBytes(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException { + byte[] value = chain.resultSet_getBytes(result, columnLabel); + + if (value != null) { + result.addReadBytesLength(value.length); + } + return value; } } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java index e7bd92d6ca..43d5521051 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java @@ -47,4 +47,8 @@ public interface ResultSetProxy extends ResultSet, WrapperProxy { void addReadStringLength(int length); long getReadStringLength(); + + void addReadBytesLength(int length); + + long getReadBytesLength(); } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java index 6e8791f065..3cc53b278b 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java @@ -58,6 +58,7 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro private int closeCount = 0; private long readStringLength = 0; + private long readBytesLength = 0; public ResultSetProxyImpl(StatementProxy statement, ResultSet resultSet, long id, String sql){ super(resultSet, id); @@ -1089,5 +1090,15 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro public long getReadStringLength() { return readStringLength; } + + @Override + public void addReadBytesLength(int length) { + this.readBytesLength += length; + } + + @Override + public long getReadBytesLength() { + return readBytesLength; + } } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java index 6a89f230fc..2cae8a2e21 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java @@ -77,7 +77,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { private final AtomicLong clobOpenCount = new AtomicLong(); private final AtomicLong blobOpenCount = new AtomicLong(); - private final AtomicLong stringReadLength = new AtomicLong(); + private final AtomicLong readStringLength = new AtomicLong(); + private final AtomicLong readBytesLength = new AtomicLong(); private final Histogram histogram = new Histogram(new long[] { // // @@ -227,7 +228,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { blobOpenCount.set(0); clobOpenCount.set(0); - stringReadLength.set(0); + readStringLength.set(0); + readBytesLength.set(0); } public long getConcurrentMax() { @@ -284,13 +286,21 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { public void incrementBlobOpenCount() { blobOpenCount.incrementAndGet(); } - - public long getStringReadLength() { - return stringReadLength.get(); + + public long getReadStringLength() { + return readStringLength.get(); } - + public void addStringReadLength(long length) { - this.stringReadLength.addAndGet(length); + this.readStringLength.addAndGet(length); + } + + public long getReadBytesLength() { + return readBytesLength.get(); + } + + public void addReadBytesLength(int length) { + this.readBytesLength.addAndGet(length); } public long getId() { @@ -519,6 +529,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { // 35 - SimpleType.LONG, // SimpleType.LONG, // + SimpleType.LONG, // }; @@ -573,8 +584,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { "ClobOpenCount", // 35 - - "BlobOpenCount", - "StringReadLength", + "BlobOpenCount", // + "ReadStringLength", // + "ReadBytesLength", // }; @@ -648,7 +660,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { map.put("ClobOpenCount", getClobOpenCount()); // 34 map.put("BlobOpenCount", getBlobOpenCount()); // 35 - map.put("StringReadLength", getStringReadLength()); // 36 + map.put("ReadStringLength", getReadStringLength()); // 36 + map.put("ReadBytesLength", getReadBytesLength()); // 37 return map; } diff --git a/src/main/resources/support/http/resources/sql-detail.html b/src/main/resources/support/http/resources/sql-detail.html index d1312510eb..57fdee1e49 100644 --- a/src/main/resources/support/http/resources/sql-detail.html +++ b/src/main/resources/support/http/resources/sql-detail.html @@ -104,8 +104,12 @@ - StringReadLength - + ReadStringLength + + + + ReadBytesLength +
@@ -155,7 +159,8 @@ $("#BatchSizeTotal").text(sqlInfo.BatchSizeTotal) $("#BlobOpenCount").text(sqlInfo.BlobOpenCount) $("#ClobOpenCount").text(sqlInfo.ClobOpenCount) - $("#StringReadLength").text(sqlInfo.StringReadLength) + $("#ReadStringLength").text(sqlInfo.ReadStringLength) + $("#ReadBytesLength").text(sqlInfo.ReadBytesLength) }, dataType: "json" }); From d7727a0c02a293903a14d56db174a1c656e995dd Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 07:41:35 +0800 Subject: [PATCH 06/55] add sqlStat column, readBytesLength & readStringLength --- .../alibaba/druid/filter/stat/StatFilter.java | 86 ++++++++++++++- .../druid/proxy/jdbc/ResultSetProxy.java | 8 ++ .../druid/proxy/jdbc/ResultSetProxyImpl.java | 37 +++++-- .../com/alibaba/druid/stat/JdbcSqlStat.java | 31 +++++- .../support/http/resources/sql-detail.html | 10 ++ .../filter/StatFilterReadBytesLengthTest.java | 103 ++++++++++++++++++ .../StatFilterReadStringLengthTest.java | 97 +++++++++++++++++ 7 files changed, 362 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadStringLengthTest.java diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index d90d475a56..facb4864bc 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -16,6 +16,7 @@ package com.alibaba.druid.filter.stat; import java.io.InputStream; +import java.io.Reader; import java.sql.Blob; import java.sql.Clob; import java.sql.NClob; @@ -573,7 +574,18 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { sqlStat.addFetchRowCount(fetchRowCount); long stmtExecuteNano = resultSet.getStatementProxy().getLastExecuteTimeNano(); sqlStat.addResultSetHoldTimeNano(stmtExecuteNano, nanos); - sqlStat.addStringReadLength(resultSet.getReadStringLength()); + if (resultSet.getReadStringLength() > 0) { + sqlStat.addStringReadLength(resultSet.getReadStringLength()); + } + if (resultSet.getReadBytesLength() > 0) { + sqlStat.addReadBytesLength(resultSet.getReadBytesLength()); + } + if (resultSet.getOpenInputStreamCount() > 0) { + sqlStat.addInputStreamOpenCount(resultSet.getOpenInputStreamCount()); + } + if (resultSet.getOpenReaderCount() > 0) { + sqlStat.addReaderOpenCount(resultSet.getOpenReaderCount()); + } } } @@ -960,4 +972,76 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { return value; } + + @Override + public InputStream resultSet_getBinaryStream(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + InputStream input = chain.resultSet_getBinaryStream(result, columnIndex); + + if (input != null) { + result.incrementOpenInputStreamCount(); + } + + return input; + } + + @Override + public InputStream resultSet_getBinaryStream(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + InputStream input = chain.resultSet_getBinaryStream(result, columnLabel); + + if (input != null) { + result.incrementOpenInputStreamCount(); + } + + return input; + } + + @Override + public InputStream resultSet_getAsciiStream(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + InputStream input = chain.resultSet_getAsciiStream(result, columnIndex); + + if (input != null) { + result.incrementOpenInputStreamCount(); + } + + return input; + } + + @Override + public InputStream resultSet_getAsciiStream(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + InputStream input = chain.resultSet_getAsciiStream(result, columnLabel); + + if (input != null) { + result.incrementOpenInputStreamCount(); + } + + return input; + } + + @Override + public Reader resultSet_getCharacterStream(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + Reader reader = chain.resultSet_getCharacterStream(result, columnIndex); + + if (reader != null) { + result.incrementOpenReaderCount(); + } + + return reader; + } + + @Override + public Reader resultSet_getCharacterStream(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + Reader reader = chain.resultSet_getCharacterStream(result, columnLabel); + + if (reader != null) { + result.incrementOpenReaderCount(); + } + + return reader; + } } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java index 43d5521051..4b4e327d8d 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxy.java @@ -51,4 +51,12 @@ public interface ResultSetProxy extends ResultSet, WrapperProxy { void addReadBytesLength(int length); long getReadBytesLength(); + + void incrementOpenInputStreamCount(); + + int getOpenInputStreamCount(); + + void incrementOpenReaderCount(); + + int getOpenReaderCount(); } diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java index 3cc53b278b..ab54e36ada 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ResultSetProxyImpl.java @@ -51,14 +51,17 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro private final StatementProxy statement; private final String sql; - protected int cursorIndex = 0; - protected int fetchRowCount = 0; + protected int cursorIndex = 0; + protected int fetchRowCount = 0; protected long constructNano; protected final JdbcSqlStat sqlStat; - private int closeCount = 0; + private int closeCount = 0; - private long readStringLength = 0; - private long readBytesLength = 0; + private long readStringLength = 0; + private long readBytesLength = 0; + + private int openInputStreamCount = 0; + private int openReaderCount = 0; public ResultSetProxyImpl(StatementProxy statement, ResultSet resultSet, long id, String sql){ super(resultSet, id); @@ -1090,15 +1093,35 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro public long getReadStringLength() { return readStringLength; } - + @Override public void addReadBytesLength(int length) { this.readBytesLength += length; } - + @Override public long getReadBytesLength() { return readBytesLength; } + @Override + public void incrementOpenInputStreamCount() { + openInputStreamCount++; + } + + @Override + public int getOpenInputStreamCount() { + return openInputStreamCount; + } + + @Override + public void incrementOpenReaderCount() { + openReaderCount++; + } + + @Override + public int getOpenReaderCount() { + return openReaderCount; + } + } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java index 2cae8a2e21..2ab364dbd2 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java @@ -80,6 +80,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { private final AtomicLong readStringLength = new AtomicLong(); private final AtomicLong readBytesLength = new AtomicLong(); + private final AtomicLong inputStreamOpenCount = new AtomicLong(); + private final AtomicLong readerOpenCount = new AtomicLong(); + private final Histogram histogram = new Histogram(new long[] { // // 1, 10, 100, 1000, 10 * 1000, // @@ -230,6 +233,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { clobOpenCount.set(0); readStringLength.set(0); readBytesLength.set(0); + inputStreamOpenCount.set(0); + readerOpenCount.set(0); } public long getConcurrentMax() { @@ -299,9 +304,25 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { return readBytesLength.get(); } - public void addReadBytesLength(int length) { + public void addReadBytesLength(long length) { this.readBytesLength.addAndGet(length); } + + public long getReaderOpenCount() { + return readerOpenCount.get(); + } + + public void addReaderOpenCount(int count) { + this.readerOpenCount.addAndGet(count); + } + + public long getInputStreamOpenCount() { + return inputStreamOpenCount.get(); + } + + public void addInputStreamOpenCount(int count) { + this.inputStreamOpenCount.addAndGet(count); + } public long getId() { return id; @@ -530,6 +551,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // + SimpleType.LONG, // + SimpleType.LONG, // }; @@ -586,7 +609,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { // 35 - "BlobOpenCount", // "ReadStringLength", // - "ReadBytesLength", + "ReadBytesLength", // + "InputStreamOpenCount", // + "ReaderOpenCount", // // }; @@ -662,6 +687,8 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { map.put("BlobOpenCount", getBlobOpenCount()); // 35 map.put("ReadStringLength", getReadStringLength()); // 36 map.put("ReadBytesLength", getReadBytesLength()); // 37 + map.put("InputStreamOpenCount", getInputStreamOpenCount()); // 38 + map.put("ReaderOpenCount", getReaderOpenCount()); // 39 return map; } diff --git a/src/main/resources/support/http/resources/sql-detail.html b/src/main/resources/support/http/resources/sql-detail.html index 57fdee1e49..b3ad5478b2 100644 --- a/src/main/resources/support/http/resources/sql-detail.html +++ b/src/main/resources/support/http/resources/sql-detail.html @@ -103,6 +103,14 @@ ClobOpenCount + + ReaderOpenCount + + + + InputStreamOpenCount + + ReadStringLength @@ -159,6 +167,8 @@ $("#BatchSizeTotal").text(sqlInfo.BatchSizeTotal) $("#BlobOpenCount").text(sqlInfo.BlobOpenCount) $("#ClobOpenCount").text(sqlInfo.ClobOpenCount) + $("#InputStreamOpenCount").text(sqlInfo.InputStreamOpenCount) + $("#ReaderOpenCount").text(sqlInfo.ReaderOpenCount) $("#ReadStringLength").text(sqlInfo.ReadStringLength) $("#ReadBytesLength").text(sqlInfo.ReadBytesLength) }, diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java new file mode 100644 index 0000000000..2322a13f99 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java @@ -0,0 +1,103 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterReadBytesLengthTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public byte[] resultSet_getBytes(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new byte[6]; + } + + @Override + public byte[] resultSet_getBytes(FilterChain chain, ResultSetProxy result, String columnIndex) + throws SQLException { + return new byte[7]; + } + }); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(0, sqlStat.getReadBytesLength()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBytes(1); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(6, sqlStat.getReadBytesLength()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(0, sqlStat.getReadBytesLength()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(0, sqlStat.getReadBytesLength()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBytes("1"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(7, sqlStat.getReadBytesLength()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReadStringLength()); + Assert.assertEquals(0, sqlStat.getReadBytesLength()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadStringLengthTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadStringLengthTest.java new file mode 100644 index 0000000000..5ff79aab1c --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadStringLengthTest.java @@ -0,0 +1,97 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterReadStringLengthTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public String resultSet_getString(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return "123456"; + } + + @Override + public String resultSet_getString(FilterChain chain, ResultSetProxy result, String columnIndex) + throws SQLException { + return "1234567"; + } + }); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getString(1); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(6, sqlStat.getReadStringLength()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReadStringLength()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReadStringLength()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getString("1"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(7, sqlStat.getReadStringLength()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReadStringLength()); + } +} From 8f06ed2f7e1a264a129e918f9b24d622332ad2ca Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 08:27:53 +0800 Subject: [PATCH 07/55] add StatFilter testcase for : OpenClobCount, OpenBlobCount, OpenInputStreamCount, OpenReaderCount --- .../filter/StatFilterOpenBlobCountTest.java | 104 +++++++++++ .../filter/StatFilterOpenBlobCountTest2.java | 171 +++++++++++++++++ .../filter/StatFilterOpenClobCountTest.java | 104 +++++++++++ .../filter/StatFilterOpenClobCountTest2.java | 174 ++++++++++++++++++ .../StatFilterOpenInputStreamCountTest.java | 100 ++++++++++ .../StatFilterOpenInputStreamCountTest2.java | 101 ++++++++++ .../filter/StatFilterOpenReaderCountTest.java | 102 ++++++++++ .../filter/StatFilterReadBytesLengthTest.java | 4 +- 8 files changed, 858 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest2.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest2.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest2.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenReaderCountTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest.java new file mode 100644 index 0000000000..10bd9e0139 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest.java @@ -0,0 +1,104 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.mock.MockBlob; +import com.alibaba.druid.mock.MockClob; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ClobProxyImpl; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenBlobCountTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new MockBlob(); + } + + @Override + public Blob resultSet_getBlob(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new MockBlob(); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBlob(1); + rs.getBlob(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBlob("1"); + rs.getBlob("2"); + rs.getBlob("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest2.java new file mode 100644 index 0000000000..0196c94cc0 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenBlobCountTest2.java @@ -0,0 +1,171 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Clob; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collections; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.mock.MockBlob; +import com.alibaba.druid.mock.MockClob; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ClobProxyImpl; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenBlobCountTest2 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new MockBlob(); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new MockBlob(); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex, + java.util.Map> map) throws SQLException { + return new MockBlob(); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnIndex, + java.util.Map> map) throws SQLException { + return new MockBlob(); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject(1); + rs.getObject(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject("1"); + rs.getObject("2"); + rs.getObject("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } + + public void test_stat_2() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject(1, Collections.> emptyMap()); + rs.getObject(2, Collections.> emptyMap()); + rs.getObject(3, Collections.> emptyMap()); + rs.getObject(4, Collections.> emptyMap()); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(4, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } + + public void test_stat_4() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject("1", Collections.> emptyMap()); + rs.getObject("2", Collections.> emptyMap()); + rs.getObject("3", Collections.> emptyMap()); + rs.getObject("4", Collections.> emptyMap()); + rs.getObject("5", Collections.> emptyMap()); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(5, sqlStat.getBlobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getBlobOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest.java new file mode 100644 index 0000000000..c156e61b83 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest.java @@ -0,0 +1,104 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Clob; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.mock.MockClob; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ClobProxyImpl; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenClobCountTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public Clob resultSet_getClob(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + + @Override + public Clob resultSet_getClob(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getClob(1); + rs.getClob(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getClob("1"); + rs.getClob("2"); + rs.getClob("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest2.java new file mode 100644 index 0000000000..6d54576ad7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenClobCountTest2.java @@ -0,0 +1,174 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Clob; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collections; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.mock.MockClob; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ClobProxyImpl; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenClobCountTest2 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex, + java.util.Map> map) throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + + @Override + public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnIndex, + java.util.Map> map) throws SQLException { + return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), + result.getStatementProxy().getConnectionProxy(), new MockClob()); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject(1); + rs.getObject(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject("1"); + rs.getObject("2"); + rs.getObject("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } + + public void test_stat_2() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject(1, Collections.>emptyMap()); + rs.getObject(2, Collections.>emptyMap()); + rs.getObject(3, Collections.>emptyMap()); + rs.getObject(4, Collections.>emptyMap()); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(4, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } + + public void test_stat_4() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getObject("1", Collections.>emptyMap()); + rs.getObject("2", Collections.>emptyMap()); + rs.getObject("3", Collections.>emptyMap()); + rs.getObject("4", Collections.>emptyMap()); + rs.getObject("5", Collections.>emptyMap()); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(5, sqlStat.getClobOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getClobOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest.java new file mode 100644 index 0000000000..0dfd9a9b38 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest.java @@ -0,0 +1,100 @@ +package com.alibaba.druid.bvt.filter; + +import java.io.ByteArrayInputStream; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenInputStreamCountTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public java.io.InputStream resultSet_getBinaryStream(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new ByteArrayInputStream(new byte[0]); + } + + @Override + public java.io.InputStream resultSet_getBinaryStream(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new ByteArrayInputStream(new byte[0]); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBinaryStream(1); + rs.getBinaryStream(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getInputStreamOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getBinaryStream("1"); + rs.getBinaryStream("2"); + rs.getBinaryStream("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getInputStreamOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest2.java new file mode 100644 index 0000000000..238a961867 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenInputStreamCountTest2.java @@ -0,0 +1,101 @@ +package com.alibaba.druid.bvt.filter; + +import java.io.ByteArrayInputStream; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +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.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenInputStreamCountTest2 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public java.io.InputStream resultSet_getAsciiStream(FilterChain chain, ResultSetProxy result, + int columnIndex) throws SQLException { + return new ByteArrayInputStream(new byte[0]); + } + + @Override + public java.io.InputStream resultSet_getAsciiStream(FilterChain chain, ResultSetProxy result, + String columnLabel) throws SQLException { + return new ByteArrayInputStream(new byte[0]); + } + + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getAsciiStream(1); + rs.getAsciiStream(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getInputStreamOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getAsciiStream("1"); + rs.getAsciiStream("2"); + rs.getAsciiStream("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getInputStreamOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getInputStreamOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenReaderCountTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenReaderCountTest.java new file mode 100644 index 0000000000..749b46f58b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterOpenReaderCountTest.java @@ -0,0 +1,102 @@ +package com.alibaba.druid.bvt.filter; + +import java.io.ByteArrayInputStream; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.nutz.lang.stream.StringReader; + +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.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterOpenReaderCountTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + dataSource.setTestOnBorrow(false); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public java.io.Reader resultSet_getCharacterStream(FilterChain chain, ResultSetProxy result, int columnIndex) + throws SQLException { + return new StringReader(""); + } + + @Override + public java.io.Reader resultSet_getCharacterStream(FilterChain chain, ResultSetProxy result, String columnLabel) + throws SQLException { + return new StringReader(""); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReaderOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getCharacterStream(1); + rs.getCharacterStream(2); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(2, sqlStat.getReaderOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReaderOpenCount()); + } + + public void test_stat_1() throws Exception { + Connection conn = dataSource.getConnection(); + + String sql = "select 'x'"; + PreparedStatement stmt = conn.prepareStatement("select 'x'"); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertEquals(0, sqlStat.getReaderOpenCount()); + + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.getCharacterStream("1"); + rs.getCharacterStream("2"); + rs.getCharacterStream("3"); + rs.close(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(3, sqlStat.getReaderOpenCount()); + + sqlStat.reset(); + Assert.assertEquals(0, sqlStat.getReaderOpenCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java index 2322a13f99..d6da8f2b88 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterReadBytesLengthTest.java @@ -68,7 +68,7 @@ public class StatFilterReadBytesLengthTest extends TestCase { Assert.assertEquals(0, sqlStat.getReadStringLength()); Assert.assertEquals(6, sqlStat.getReadBytesLength()); - + sqlStat.reset(); Assert.assertEquals(0, sqlStat.getReadStringLength()); Assert.assertEquals(0, sqlStat.getReadBytesLength()); @@ -95,7 +95,7 @@ public class StatFilterReadBytesLengthTest extends TestCase { Assert.assertEquals(0, sqlStat.getReadStringLength()); Assert.assertEquals(7, sqlStat.getReadBytesLength()); - + sqlStat.reset(); Assert.assertEquals(0, sqlStat.getReadStringLength()); Assert.assertEquals(0, sqlStat.getReadBytesLength()); From 33f5603deec537f620a357bf40f23085b3f43739 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 08:35:23 +0800 Subject: [PATCH 08/55] remove forerver true if condition --- .../alibaba/druid/pool/DruidDataSource.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 8189456805..88c2883f57 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -1521,31 +1521,29 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat buf.append(System.identityHashCode(conn.getConnection())); PreparedStatementPool pool = conn.getStatementPool(); - if (pool != null) { - buf.append(", \n\tpoolStatements:["); + buf.append(", \n\tpoolStatements:["); - int entryIndex = 0; - try { - for (Map.Entry entry : pool.getMap().entrySet()) { - if (entryIndex != 0) { - buf.append(","); - } - buf.append("\n\t\t{hitCount:"); - buf.append(entry.getValue().getHitCount()); - buf.append(",sql:\""); - buf.append(entry.getKey().getSql()); - buf.append("\""); - buf.append("\t}"); - - entryIndex++; + int entryIndex = 0; + try { + for (Map.Entry entry : pool.getMap().entrySet()) { + if (entryIndex != 0) { + buf.append(","); } - } catch (ConcurrentModificationException e) { - // skip .. - } + buf.append("\n\t\t{hitCount:"); + buf.append(entry.getValue().getHitCount()); + buf.append(",sql:\""); + buf.append(entry.getKey().getSql()); + buf.append("\""); + buf.append("\t}"); - buf.append("\n\t\t]"); + entryIndex++; + } + } catch (ConcurrentModificationException e) { + // skip .. } + buf.append("\n\t\t]"); + buf.append("\n\t}"); } } From 2689f9392e7864cc5f87f1268c5a1e7494655b22 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 08:59:35 +0800 Subject: [PATCH 09/55] add DruidDataSourceC3P0Adapter.java testcase --- .../pool/DruidDataSourceC3P0Adapter.java | 6 +- .../pool/DruidDataSourceC3P0AdapterTest.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSourceC3P0Adapter.java b/src/main/java/com/alibaba/druid/pool/DruidDataSourceC3P0Adapter.java index 4f6b25ab20..dba9e942e3 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSourceC3P0Adapter.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSourceC3P0Adapter.java @@ -279,8 +279,6 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3 } public void setMaxStatementsPerConnection(int maxStatementsPerConnection) { - dataSource.setPoolPreparedStatements(maxStatementsPerConnection > 0); - dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxStatementsPerConnection); } @@ -289,8 +287,6 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3 } public void setMaxStatements(int maxStatements) { - dataSource.setPoolPreparedStatements(maxStatements > 0); - dataSource.setMaxOpenPreparedStatements(maxStatements); } @@ -493,6 +489,6 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3 } public Logger getParentLogger() throws SQLFeatureNotSupportedException { - throw new SQLFeatureNotSupportedException(); + return dataSource.getParentLogger(); } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceC3P0AdapterTest.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceC3P0AdapterTest.java index d98421fae1..4ada4a1e46 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceC3P0AdapterTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceC3P0AdapterTest.java @@ -1,6 +1,7 @@ package com.alibaba.druid.bvt.pool; import java.sql.Connection; +import java.sql.SQLFeatureNotSupportedException; import org.junit.Assert; @@ -46,4 +47,84 @@ public class DruidDataSourceC3P0AdapterTest extends TestCase { public void test_getJdbcUrl() throws Exception { Assert.assertEquals("jdbc:mock:xxx", dataSource.getJdbcUrl()); } + + public void test_getParentLogger_err() throws Exception { + Exception error = null; + try { + dataSource.getParentLogger(); + } catch (SQLFeatureNotSupportedException e) { + error = e; + } + Assert.assertNotNull(error); + } + + public void test_propertyCycle() throws Exception { + dataSource.setPropertyCycle(3); + Assert.assertEquals(3, dataSource.getPropertyCycle()); + } + + public void test_acquireIncrement() throws Exception { + dataSource.setAcquireIncrement(4); + Assert.assertEquals(4, dataSource.getAcquireIncrement()); + } + + public void test_overrideDefaultPassword() throws Exception { + dataSource.setOverrideDefaultPassword("x2"); + Assert.assertEquals("x2", dataSource.getOverrideDefaultPassword()); + } + + public void test_overrideDefaultUser() throws Exception { + dataSource.setOverrideDefaultUser("x1"); + Assert.assertEquals("x1", dataSource.getOverrideDefaultUser()); + } + + public void test_factoryClassLocation() throws Exception { + dataSource.setFactoryClassLocation("x3"); + Assert.assertEquals("x3", dataSource.getFactoryClassLocation()); + } + + public void test_maxConnectionAge() throws Exception { + dataSource.setMaxConnectionAge(123); + Assert.assertEquals(123, dataSource.getMaxConnectionAge()); + } + + public void test_connectionCustomizerClassName() throws Exception { + dataSource.setConnectionCustomizerClassName("x4"); + Assert.assertEquals("x4", dataSource.getConnectionCustomizerClassName()); + } + + public void test_maxIdleTimeExcessConnections() throws Exception { + dataSource.setMaxIdleTimeExcessConnections(101); + Assert.assertEquals(101, dataSource.getMaxIdleTimeExcessConnections()); + } + + public void test_maxAdministrativeTaskTime() throws Exception { + dataSource.setMaxAdministrativeTaskTime(102); + Assert.assertEquals(102, dataSource.getMaxAdministrativeTaskTime()); + } + + public void test_userOverridesAsString() throws Exception { + dataSource.setUserOverridesAsString("x5"); + Assert.assertEquals("x5", dataSource.getUserOverridesAsString()); + } + + public void test_usesTraditionalReflectiveProxies() throws Exception { + dataSource.setUsesTraditionalReflectiveProxies(true); + Assert.assertEquals(true, dataSource.isUsesTraditionalReflectiveProxies()); + } + + public void test_forceIgnoreUnresolvedTransactions() throws Exception { + dataSource.setForceIgnoreUnresolvedTransactions(true); + Assert.assertEquals(true, dataSource.isForceIgnoreUnresolvedTransactions()); + } + + public void test_automaticTestTable() throws Exception { + dataSource.setAutomaticTestTable("x6"); + Assert.assertEquals("x6", dataSource.getAutomaticTestTable()); + } + + public void test_connectionTesterClassName() throws Exception { + dataSource.setConnectionTesterClassName("x6"); + Assert.assertEquals("x6", dataSource.getConnectionTesterClassName()); + } } From 0c47d2a38dba008e78a3672a25f52d2b70e4824b Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 09:19:28 +0800 Subject: [PATCH 10/55] test DruidDataSource testcase --- .../com/alibaba/druid/pool/DruidAbstractDataSource.java | 9 ++++----- .../com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java index 844211f849..415cc131f2 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java @@ -1398,11 +1398,10 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements } // 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)); + map.put("ConnectionConnectCount", createErrorCount); + if (createError != null) { + map.put("ConnectionErrorLastMessage", createError.getMessage()); + map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(createError)); } else { map.put("ConnectionErrorLastMessage", null); map.put("ConnectionErrorLastStackTrace", null); diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java index 0ca88cbd78..ae8cbe7b84 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java @@ -43,6 +43,8 @@ public class DruidDataSourceTest8 extends TestCase { } Assert.assertNotNull(error); Assert.assertTrue(dataSource.getCreateErrorCount() > 0); + + dataSource.getCompositeData(); } } From 841534ed432a16cfee89d5d4b62e8e30cd05a03b Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 09:45:27 +0800 Subject: [PATCH 11/55] 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))); + + } +} From b1e1b6c5e703a03322743fb79200851b9166143f Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 10:25:23 +0800 Subject: [PATCH 12/55] decrease lock scope --- .../com/alibaba/druid/pool/DruidDataSource.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 148732e33f..01eb315ba9 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -722,7 +722,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } finally { lock.unlock(); } - + holder.incrementUseCount(); DruidPooledConnection poolalbeConnection = new DruidPooledConnection(holder); @@ -1174,9 +1174,16 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat continue; } + DruidConnectionHolder holder = null; + try { + holder = new DruidConnectionHolder(DruidDataSource.this, connection); + } catch (SQLException ex) { + LOG.error("create connection holder error", ex); + break; + } + lock.lock(); try { - DruidConnectionHolder holder = new DruidConnectionHolder(DruidDataSource.this, connection); connections[poolingCount++] = holder; if (poolingCount > poolingPeak) { @@ -1188,9 +1195,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat notEmpty.signal(); notEmptySignalCount++; - } catch (SQLException ex) { - LOG.error("create connection holder error", ex); - break; } finally { lock.unlock(); } @@ -1214,7 +1218,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (closed) { break; } - + if (timeBetweenEvictionRunsMillis > 0) { Thread.sleep(timeBetweenEvictionRunsMillis); } else { From 96196a0ccdea75b80c2cc25b7380b3a6459f8aa5 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 10:48:06 +0800 Subject: [PATCH 13/55] add DruidDataSourceFactory.getObjectInstance() testcase, test null branche --- .../druid/bvt/pool/DruidDataSourceFactoryTest1.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceFactoryTest1.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceFactoryTest1.java index fa6a574fae..41508b1d54 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceFactoryTest1.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceFactoryTest1.java @@ -160,4 +160,12 @@ public class DruidDataSourceFactoryTest1 extends TestCase { JdbcUtils.close(dataSource); } } + + public void test_getObjectInstance() throws Exception { + Assert.assertNull(new DruidDataSourceFactory().getObjectInstance(null, null, null, null)); + } + + public void test_getObjectInstance_1() throws Exception { + Assert.assertNull(new DruidDataSourceFactory().getObjectInstance(new Object(), null, null, null)); + } } From d9c286e0fb63912fe47ef4e6eeac76c64e712171 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 11:06:45 +0800 Subject: [PATCH 14/55] add DruidConnectionHolder testcase & bug fixed --- .../java/com/alibaba/druid/pool/DruidConnectionHolder.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java b/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java index 035d52fdf6..46088b0fd2 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java +++ b/src/main/java/com/alibaba/druid/pool/DruidConnectionHolder.java @@ -215,10 +215,9 @@ public final class DruidConnectionHolder { buf.append("\""); } - PreparedStatementPool statmentPool = this.getStatementPool(); - if (statmentPool != null && statmentPool.getMap().size() > 0) { + if (statementPool != null && statementPool.getMap().size() > 0) { buf.append("\", CachedStatementCount:"); - buf.append(this.getStatementPool().getMap().size()); + buf.append(statementPool.getMap().size()); } buf.append("}"); From 5ddea980c37056f291fac00b3abff8f5dc1bb017 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 11:07:46 +0800 Subject: [PATCH 15/55] add DruidConnectionHolder testcase & bug fixed --- .../bvt/pool/DruidConnectionHolderTest4.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest4.java diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest4.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest4.java new file mode 100644 index 0000000000..fefe89d08b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidConnectionHolderTest4.java @@ -0,0 +1,59 @@ +package com.alibaba.druid.bvt.pool; + +import java.lang.reflect.Field; +import java.sql.PreparedStatement; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.pool.DruidConnectionHolder; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidPooledConnection; + +public class DruidConnectionHolderTest4 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setPoolPreparedStatements(true); + + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_toString() throws Exception { + DruidPooledConnection conn = dataSource.getConnection(); + + DruidConnectionHolder holder = conn.getConnectionHolder(); + + Field field = DruidConnectionHolder.class.getDeclaredField("statementPool"); + field.setAccessible(true); + Assert.assertNull(field.get(holder)); + + holder.toString(); + + Assert.assertNull(field.get(holder)); + + holder.getStatementPool(); + + Assert.assertNotNull(field.get(holder)); + + holder.toString(); + + PreparedStatement stmt = conn.prepareStatement("select 1"); + stmt.execute(); + stmt.close(); + + conn.close(); + + Assert.assertEquals(1, holder.getStatementPool().size()); + + holder.toString(); + } +} From 87ab420018c037fb505809cd036f6d209c010671 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 12:03:07 +0800 Subject: [PATCH 16/55] add testcase & remove unused code --- .../druid/pool/DruidAbstractDataSource.java | 27 +++++-------------- .../bvt/pool/DruidDataSourceTest_oracle2.java | 8 ++++++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java index 415cc131f2..2ae8f09340 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java @@ -1356,22 +1356,15 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements map.put("StatementExecuteCount", stat.getStatementStat().getExecuteCount()); map.put("StatementRunningCount", stat.getStatementStat().getRunningCount()); - // 15 -24 + // 15 - 19 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("StatementLastErrorTime", null); + map.put("StatementLastErrorMessage", null); - map.put("StatementLastErrorStackTrace", IOUtils.getStackTrace(lastStatementError)); - } else { - map.put("StatementLastErrorTime", null); - map.put("StatementLastErrorMessage", null); - - map.put("StatementLastErrorStackTrace", null); - } + // 20 - 24 + map.put("StatementLastErrorStackTrace", null); map.put("StatementExecuteMillisTotal", stat.getStatementStat().getMillisTotal()); map.put("StatementExecuteLastTime", stat.getStatementStat().getExecuteLastTime()); map.put("ConnectionConnectingCount", stat.getConnectionStat().getConnectingCount()); @@ -1388,14 +1381,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements 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("ResultSetLastErrorMessage", null); + map.put("ResultSetLastErrorStackTrace", null); // 35 - 39 map.put("ConnectionConnectCount", createErrorCount); 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 06828f04d4..dd3424f76c 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 @@ -44,7 +44,15 @@ public class DruidDataSourceTest_oracle2 extends TestCase { dataSource.setUseOracleImplicitCache(false); Assert.assertNull(dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); + dataSource.setUseOracleImplicitCache(true); dataSource.setUseOracleImplicitCache(true); Assert.assertEquals("true", dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); + + dataSource.setUseOracleImplicitCache(false); + Assert.assertNull(dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); + dataSource.setDriver(null); + + dataSource.setUseOracleImplicitCache(true); + Assert.assertNull(dataSource.getConnectProperties().get("oracle.jdbc.FreeMemoryOnEnterImplicitCache")); } } From 645d39f8ededc3ec15dbcc52e93064e7de3d9486 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 16:11:41 +0800 Subject: [PATCH 17/55] add FilterChainImpl testcase --- .../druid/bvt/filter/FilterChainImplTest.java | 375 +++++++++++ .../bvt/filter/FilterChainImplTest2.java | 604 ++++++++++++++++++ 2 files changed, 979 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java new file mode 100644 index 0000000000..b6c95ed016 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java @@ -0,0 +1,375 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.sql.Types; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.filter.FilterChainImpl; +import com.alibaba.druid.mock.MockNClob; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ConnectionProxy; +import com.alibaba.druid.util.JdbcUtils; + +public class FilterChainImplTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_size() { + Assert.assertEquals(dataSource.getProxyFilters().size(), new FilterChainImpl(dataSource).getFilterSize()); + } + + public void test_unwrap() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).unwrap(null, null)); + } + + public void test_unwrap_1() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((Connection) null, null)); + } + + public void test_unwrap_2() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Statement) null)); + } + + public void test_unwrap_3() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (PreparedStatement) null, "")); + } + + public void test_unwrap_4() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (CallableStatement) null, "")); + } + + public void test_unwrap_5() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Clob) null)); + } + + public void test_unwrap_6() throws Exception { + Connection conn = dataSource.getConnection(); + Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, new MockNClob()) instanceof NClob); + conn.close(); + } + + public void test_unwrap_7() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (NClob) null)); + } + + public void test_getUnicodeStream() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getUnicodeStream(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getUnicodeStream_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getUnicodeStream("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getRef() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getRef(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getRef_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getRef("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getArray() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getArray(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getArray_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getArray("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getURL() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getURL(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getURL_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getURL("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getRowId() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getURL(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getRowId_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getURL("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNClob() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNClob(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNClob_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNClob("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getSQLXML() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getSQLXML(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getSQLXML_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getSQLXML("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNString() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNString(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNString_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNString("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNCharacterStream() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNCharacterStream(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getNCharacterStream_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getNCharacterStream("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getObject() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getObject(1)); + + rs.close(); + stmt.close(); + conn.close(); + + } + + public void test_getObject_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + ResultSet rs = stmt.executeQuery(); + rs.next(); + + Assert.assertNull(rs.getObject("1")); + + rs.close(); + stmt.close(); + conn.close(); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java new file mode 100644 index 0000000000..e246f5f19e --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java @@ -0,0 +1,604 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.Types; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcUtils; + +public class FilterChainImplTest2 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_getURL() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getURL(1)); + + stmt.close(); + conn.close(); + } + + public void test_getURL_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getURL("1")); + + stmt.close(); + conn.close(); + } + + public void test_getString() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getString(1)); + + stmt.close(); + conn.close(); + } + + public void test_getString_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getString("1")); + + stmt.close(); + conn.close(); + } + + public void test_getBoolean() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertFalse(stmt.getBoolean(1)); + + stmt.close(); + conn.close(); + } + + public void test_getBoolean_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertFalse(stmt.getBoolean("1")); + + stmt.close(); + conn.close(); + } + + + public void test_getByte() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getByte(1)); + + stmt.close(); + conn.close(); + } + + public void test_getByte_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getByte("1")); + + stmt.close(); + conn.close(); + } + + public void test_getShort() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getShort(1)); + + stmt.close(); + conn.close(); + } + + public void test_getShort_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getShort("1")); + + stmt.close(); + conn.close(); + } + + public void test_getInt() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getInt(1)); + + stmt.close(); + conn.close(); + } + + public void test_getInt_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getInt("1")); + + stmt.close(); + conn.close(); + } + + public void test_getLong() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getLong(1)); + + stmt.close(); + conn.close(); + } + + public void test_getLong_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0, stmt.getLong("1")); + + stmt.close(); + conn.close(); + } + + public void test_getFloat() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0F, stmt.getFloat(1)); + + stmt.close(); + conn.close(); + } + + public void test_getFloat_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0F, stmt.getFloat("1")); + + stmt.close(); + conn.close(); + } + + public void test_getDouble() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0D, stmt.getDouble(1)); + + stmt.close(); + conn.close(); + } + + public void test_getDouble_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertEquals(0D, stmt.getDouble("1")); + + stmt.close(); + conn.close(); + } + + + public void test_getBytes() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBytes(1)); + + stmt.close(); + conn.close(); + } + + public void test_getBytes_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBytes("1")); + + stmt.close(); + conn.close(); + } + + + public void test_getDate() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getDate(1)); + + stmt.close(); + conn.close(); + } + + public void test_getDate_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getDate("1")); + + stmt.close(); + conn.close(); + } + + public void test_getTime() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTime(1)); + + stmt.close(); + conn.close(); + } + + public void test_getTime_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTime("1")); + + stmt.close(); + conn.close(); + } + + public void test_getTimestamp() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTimestamp(1)); + + stmt.close(); + conn.close(); + } + + public void test_getTimestamp_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTimestamp("1")); + + stmt.close(); + conn.close(); + } + + public void test_getBigDecimal() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBigDecimal(1)); + + stmt.close(); + conn.close(); + } + + public void test_getBigDecimal_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBigDecimal("1")); + + stmt.close(); + conn.close(); + } + + public void test_getRef() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getRef(1)); + + stmt.close(); + conn.close(); + } + + public void test_getRef_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getRef("1")); + + stmt.close(); + conn.close(); + } + + public void test_getBlob() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBlob(1)); + + stmt.close(); + conn.close(); + } + + public void test_getBlob_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getBlob("1")); + + stmt.close(); + conn.close(); + } + + public void test_getArray() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getArray(1)); + + stmt.close(); + conn.close(); + } + + public void test_getArray_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getArray("1")); + + stmt.close(); + conn.close(); + } + + public void test_getDate_2() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getDate(1, null)); + + stmt.close(); + conn.close(); + } + + public void test_getDate_3() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getDate("1", null)); + + stmt.close(); + conn.close(); + } + + public void test_getTime_2() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTime(1, null)); + + stmt.close(); + conn.close(); + } + + public void test_getTime_3() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTime("1", null)); + + stmt.close(); + conn.close(); + } + + public void test_getTimestamp_2() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTimestamp(1, null)); + + stmt.close(); + conn.close(); + } + + public void test_getTimestamp_3() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getTimestamp("1", null)); + + stmt.close(); + conn.close(); + } + + public void test_getRowId() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getRowId(1)); + + stmt.close(); + conn.close(); + } + + public void test_getRowId_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getRowId("1")); + + stmt.close(); + conn.close(); + } + + public void test_getNClob() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNClob(1)); + + stmt.close(); + conn.close(); + } + + public void test_getNClob_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNClob("1")); + + stmt.close(); + conn.close(); + } + + public void test_getSQLXML() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getSQLXML(1)); + + stmt.close(); + conn.close(); + } + + public void test_getSQLXML_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getSQLXML("1")); + + stmt.close(); + conn.close(); + } + + public void test_getNString() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNString(1)); + + stmt.close(); + conn.close(); + } + + public void test_getNString_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNString("1")); + + stmt.close(); + conn.close(); + } + + public void test_getNCharacterStream() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNCharacterStream(1)); + + stmt.close(); + conn.close(); + } + + public void test_getNCharacterStream_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getNCharacterStream("1")); + + stmt.close(); + conn.close(); + } + + public void test_getCharacterStream() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getCharacterStream(1)); + + stmt.close(); + conn.close(); + } + + public void test_getCharacterStream_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getCharacterStream("1")); + + stmt.close(); + conn.close(); + } +} From 8239a3edd68c95a0841cde043ac19fdf1dc9e73a Mon Sep 17 00:00:00 2001 From: "shrek.wang" Date: Tue, 28 Aug 2012 17:23:26 +0800 Subject: [PATCH 18/55] add '-detail' and 'refresh-interval' argument, some code refactor --- .../druid/support/console/DruidStat.java | 33 +++-- .../alibaba/druid/support/console/Option.java | 115 +++++++++++------- .../support/console/TabledDataPrinter.java | 78 ++++++++---- .../druid/bvt/console/DruidStatTest.java | 24 +++- .../alibaba/druid/bvt/console/OptionTest.java | 39 ++++-- 5 files changed, 204 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/alibaba/druid/support/console/DruidStat.java b/src/main/java/com/alibaba/druid/support/console/DruidStat.java index 73f207764d..293409f7c9 100644 --- a/src/main/java/com/alibaba/druid/support/console/DruidStat.java +++ b/src/main/java/com/alibaba/druid/support/console/DruidStat.java @@ -15,6 +15,7 @@ */ package com.alibaba.druid.support.console; +import java.util.ArrayList; import java.io.File; import java.io.IOException; import java.util.List; @@ -61,20 +62,21 @@ public class DruidStat { @SuppressWarnings("all") public static void printDruidStat(Option option) throws Exception { - String address = loadManagementAgentAndGetAddress(option.getVmid()); + String address = loadManagementAgentAndGetAddress(option.getPid()); JMXServiceURL jmxUrl = new JMXServiceURL(address); JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl); MBeanServerConnection jmxConn = jmxc.getMBeanServerConnection(); - - if (option.printSqlData()) { - List> content = (List>) invokeService(jmxConn, Option.SQL); - TabledDataPrinter.printSqlData(content, option); - } if (option.printDataSourceData()) { List> content = (List>) invokeService(jmxConn, Option.DATA_SOURCE); TabledDataPrinter.printDataSourceData(content, option); } + + if (option.printSqlData()) { + List> content = (List>) invokeService(jmxConn, Option.SQL); + TabledDataPrinter.printSqlData(content, option); + } + if (option.printActiveConn()) { List> content = (List>) invokeService(jmxConn, Option.ACTIVE_CONN); TabledDataPrinter.printActiveConnStack(content, option); @@ -83,7 +85,24 @@ public class DruidStat { } @SuppressWarnings("all") - private static Object invokeService(MBeanServerConnection jmxConn, int dataType) throws Exception { + public static List getDataSourceIds(Option option) throws Exception{ + String address = loadManagementAgentAndGetAddress(option.getPid()); + JMXServiceURL jmxUrl = new JMXServiceURL(address); + JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl); + MBeanServerConnection jmxConn = jmxc.getMBeanServerConnection(); + List> content = (List>) invokeService(jmxConn, Option.DATA_SOURCE); + TabledDataPrinter.printDataSourceData(content, option); + + List result = new ArrayList(); + for (Map dsStat : content) { + Integer id = (Integer)dsStat.get("Identity"); + result.add(id); + } + return result; + } + + @SuppressWarnings("all") + public static Object invokeService(MBeanServerConnection jmxConn, int dataType) throws Exception { String url = Option.getUrl(dataType); ObjectName name = new ObjectName(DruidStatService.MBEAN_NAME); String result = (String) jmxConn.invoke(name, "service", new String[] { url }, new String[] { String.class.getName() }); diff --git a/src/main/java/com/alibaba/druid/support/console/Option.java b/src/main/java/com/alibaba/druid/support/console/Option.java index bf59ef4e54..ab7a9600b6 100644 --- a/src/main/java/com/alibaba/druid/support/console/Option.java +++ b/src/main/java/com/alibaba/druid/support/console/Option.java @@ -17,15 +17,15 @@ package com.alibaba.druid.support.console; public class Option { - public static enum PrintStyle {HORIZONTAL , VERTICAL }; public static final int DATA_SOURCE = 1 ; public static final int SQL = 2 ; public static final int ACTIVE_CONN = 4 ; - private PrintStyle style = PrintStyle.HORIZONTAL; private int printDataType = 0; - private int vmid = -1; + private int pid = -1; private int id = -1; + private int interval = -1; + private boolean detailPrint; public void addPrintDataType(int newValue) { this.printDataType= this.printDataType | newValue; @@ -55,18 +55,12 @@ public class Option { return this.printDataType; } - public void setStyle(PrintStyle style) { - this.style=style; - } - public PrintStyle getStyle() { - return this.style; - } - public void setVmid(int vmid) { - this.vmid=vmid; + public void setPid(int pid) { + this.pid=pid; } - public int getVmid() { - return this.vmid; + public int getPid() { + return this.pid; } public static String getUrl(int dataType) { @@ -82,6 +76,15 @@ public class Option { } } + private static int parsePositiveInt(String v) { + try { + int pid = Integer.parseInt(v); + return pid; + } catch (NumberFormatException e) { + return -1; + } + } + public static Option parseOptions(String[] args) throws OptionParseException{ Option option = new Option(); int i = 0; @@ -90,57 +93,71 @@ public class Option { } while (i < args.length) { - if ( i == args.length -1 ) { - if (args[i].startsWith("-")) throw new OptionParseException("please specify vmid in last arguments!"); - try { - int vmid = Integer.parseInt(args[i]); - option.setVmid(vmid); - } catch (NumberFormatException e) { - throw new OptionParseException("vmid argument is not a integer!"); - } - } - if (!args[i].startsWith("-")) { - i++; - continue; - } + int v1 = parsePositiveInt(args[i]); + + //check last two arguments + if ( (i == args.length - 2) && v1 > 0) { + int v2 = parsePositiveInt(args[i+1]); + if ( v2 > 0) { + option.setPid(v1); + option.setInterval(v2); + } else { + throw new OptionParseException("请在参数的最后位置上 指定 pid 和 refresh-interval"); + } + break; + } else if ( i == args.length -1 ) { + option.setPid(v1); + } + if (args[i].equals("-sql")) { option.addPrintDataType(SQL); } else if (args[i].equals("-ds")) { option.addPrintDataType(DATA_SOURCE); } else if (args[i].equals("-act")) { option.addPrintDataType(ACTIVE_CONN); - } else if (args[i].equals("-s1")) { - option.setStyle(PrintStyle.HORIZONTAL); - } else if (args[i].equals("-s2")) { - option.setStyle(PrintStyle.VERTICAL); + } else if (args[i].equals("-detail")) { + option.setDetailPrint(true); } else if (args[i].equals("-id")) { try { int id = Integer.parseInt(args[i+1]); option.setId(id); + i++; } catch (NumberFormatException e) { - throw new OptionParseException("vmid argument is not a integer!"); + throw new OptionParseException("id参数必须是整数"); } } i++; } + if (option.getPrintDataType() == 0) { - throw new OptionParseException("please specify one or more arguments in {'-sql','-ds','-act'}."); + throw new OptionParseException("请在{'-sql','-ds','-act'}参数中选择一个或多个"); + } + if (option.getPid() == -1 ) { + throw new OptionParseException("请在参数中指定 pid"); } return option; } public static void printHelp() { - System.out.println("Usage: druidStat -help | -sql -ds -act -s1 -s2 -id vmid"); + System.out.println("Usage: druidStat -help | -sql -ds -act [-detail] [-id id] [refresh-interval]"); System.out.println(); - System.out.println("arguments: "); - System.out.println(" vmid the process id of jvm that running druid"); - System.out.println(" -sql print sql stat data"); - System.out.println(" -ds print datasource stat data"); - System.out.println(" -act print active connection stacktrace data"); - System.out.println(" -s1 print field name in first row"); - System.out.println(" -s2 print field name in first column"); - System.out.println(" -id only print the sql data matched with id, use this with -sql argument" ); - } + System.out.println("参数: "); + System.out.println(" -help 打印此帮助信息"); + System.out.println(" -sql 打印SQL统计数据"); + System.out.println(" -ds 打印DataSource统计数据"); + System.out.println(" -act 打印活动连接的堆栈信息"); + System.out.println(" -detail 打印统计数据的全部字段信息"); + System.out.println(" -id id 要打印的数据的具体id值" ); + System.out.println(" pid 使用druid连接池的jvm进程id"); + System.out.println(" refresh-interval 自动刷新时间间隔, 以秒为单位" ); + + System.out.println(); + System.out.println("说明: "); + System.out.println(" -sql,-ds,-act参数中要至少指定一种数据进行打印, 可以组合使用, 比如 -sql -ds 一起的话就打印两种统计数据"); + System.out.println(" -id id可以跟 -sql 或-ds组合, 比如 -sql -id 5 或 -ds -id 1086752"); + System.out.println(" pid必需指定, refresh-interval可选, 如不指定,则打印数据后退出"); + System.out.println(" pid和refresh-interval参数必需放在命令行的最后, 否则解析会出错"); + } public int getId() { return id; @@ -150,6 +167,18 @@ public class Option { this.id = id; } - + public void setDetailPrint(boolean detailPrint) { + this.detailPrint=detailPrint; + } + public boolean isDetailPrint() { + return this.detailPrint; + } + + public void setInterval(int interval) { + this.interval=interval; + } + public int getInterval() { + return this.interval; + } } diff --git a/src/main/java/com/alibaba/druid/support/console/TabledDataPrinter.java b/src/main/java/com/alibaba/druid/support/console/TabledDataPrinter.java index 8645e71a51..7d13add569 100644 --- a/src/main/java/com/alibaba/druid/support/console/TabledDataPrinter.java +++ b/src/main/java/com/alibaba/druid/support/console/TabledDataPrinter.java @@ -28,15 +28,13 @@ public class TabledDataPrinter { private static final String[] sqlRowTitle = new String[] { "ID", "SQL", "ExecCount", "ExecTime", - "ExecMax","Txn", "Error","Update","FetchRow","Running","Concurrent", - "ExecHisto", "ExecRsHisto","FetchRowHisto","UpdateHisto" }; + "ExecMax","Txn", "Error","Update","FetchRow","Running","Concurrent", "ExecRsHisto" }; private static final String[] sqlRowField = new String[] { "ID", "SQL", "ExecuteCount", "TotalTime", "MaxTimespan", "InTransactionCount", "ErrorCount", "EffectedRowCount", "FetchRowCount", - "RunningCount","ConcurrentMax","Histogram","ExecuteAndResultHoldTimeHistogram", - "FetchRowCountHistogram","EffectedRowCountHistogram" }; + "RunningCount","ConcurrentMax","ExecuteAndResultHoldTimeHistogram" }; private static final String[] sqlColField = new String[] { "ID", "DataSource", "SQL", "ExecuteCount", "ErrorCount", @@ -50,16 +48,16 @@ public class TabledDataPrinter { private static final String[] dsRowTitle = new String[] { - "Name", "DbType", + "Identity", "DbType", "PoolingCount", "PoolingPeak", "PoolingPeakTime", "ActiveCount", "ActivePeak", "ActivePeakTime", - "ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" }; + "ExecuteCount", "ErrorCount" }; private static final String[] dsRowField = new String[] { - "Name", "DbType", + "Identity", "DbType", "PoolingCount", "PoolingPeak", "PoolingPeakTime", "ActiveCount", "ActivePeak", "ActivePeakTime", - "ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" }; + "ExecuteCount", "ErrorCount"}; private static final String[] dsColField = new String[] { @@ -88,35 +86,73 @@ public class TabledDataPrinter { } public static void printDataSourceData(List> content,Option opt) { - if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) { - System.out.println(getFormattedOutput(content, dsRowTitle, dsRowField)); - } else { + while (true ) { + _printDataSourceData(content, opt); + if (opt.getInterval() == -1) break; + try { + Thread.sleep( opt.getInterval() * 1000 ); + } catch (Exception e) { + e.printStackTrace(); + break; + } + } + } + + public static void _printDataSourceData(List> content,Option opt) { + if (opt.getId() != -1 ) { + List> matchedContent = new ArrayList>(); + for (Map dsStat : content) { + Integer idStr = (Integer)dsStat.get("Identity"); + if (idStr.intValue() == opt.getId()) { + matchedContent.add(dsStat); + break; + } + } + content = matchedContent; + } + if (opt.isDetailPrint()) { System.out.println(getVerticalFormattedOutput(content, dsColField)); + } else { + System.out.println(getFormattedOutput(content, dsRowTitle, dsRowField)); } } public static void printSqlData(List> content, Option opt) { - if (opt.getId() != -1) { + while (true ) { + _printSqlData(content, opt); + if (opt.getInterval() == -1) break; + try { + Thread.sleep( opt.getInterval() * 1000 ); + } catch (Exception e) { + e.printStackTrace(); + break; + } + } + } + + public static void _printSqlData(List> content, Option opt) { + if (opt.getId() != -1 ) { List> matchedContent = new ArrayList>(); for (Map sqlStat : content) { Integer idStr = (Integer)sqlStat.get("ID"); if (idStr.intValue() == opt.getId()) { matchedContent.add(sqlStat); - String dbType = (String) sqlStat.get("DbType"); - String sql = (String) sqlStat.get("SQL"); - System.out.println(SQLUtils.format(sql, dbType)); - System.out.println("===============================\n"); + if (opt.isDetailPrint()) { + String dbType = (String) sqlStat.get("DbType"); + String sql = (String) sqlStat.get("SQL"); + System.out.println("Formatted SQL:"); + System.out.println(SQLUtils.format(sql, dbType)); + System.out.println(); + } break; } } content = matchedContent; - - } - if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) { - System.out.println(getFormattedOutput(content, sqlRowTitle, sqlRowField)); - } else { + if (opt.isDetailPrint()) { System.out.println(getVerticalFormattedOutput(content, sqlColField)); + } else { + System.out.println(getFormattedOutput(content, sqlRowTitle, sqlRowField)); } } diff --git a/src/test/java/com/alibaba/druid/bvt/console/DruidStatTest.java b/src/test/java/com/alibaba/druid/bvt/console/DruidStatTest.java index e1be753a5b..3d9e932407 100644 --- a/src/test/java/com/alibaba/druid/bvt/console/DruidStatTest.java +++ b/src/test/java/com/alibaba/druid/bvt/console/DruidStatTest.java @@ -1,6 +1,7 @@ package com.alibaba.druid.bvt.console; +import java.util.List; import java.sql.Connection; import java.sql.Statement; import com.alibaba.druid.support.console.Option; @@ -62,16 +63,27 @@ public class DruidStatTest extends TestCase { String[] cmdArray = {"-sql", pid}; Option opt = Option.parseOptions(cmdArray); DruidStat.printDruidStat(opt); - } - public void test_printDruidStat2() throws Exception { - String pid = getSelfPid(); - String[] cmdArray = {"-ds","-s2", pid}; - Option opt = Option.parseOptions(cmdArray); + cmdArray = new String[] {"-sql","-id","1", pid}; + opt = Option.parseOptions(cmdArray); DruidStat.printDruidStat(opt); + + cmdArray = new String[] {"-sql","-detail", "-id","1", pid}; + opt = Option.parseOptions(cmdArray); + DruidStat.printDruidStat(opt); + + cmdArray = new String[] {"-ds",pid}; + opt = Option.parseOptions(cmdArray); + List ids = DruidStat.getDataSourceIds(opt); + opt.setDetailPrint(true); + opt.setId( ids.get(0).intValue()); + DruidStat.printDruidStat(opt); + + } - public void test_printDruidStat3() throws Exception { + + public void test_printDruidStat2() throws Exception { String pid = getSelfPid(); String[] cmdArray = {"-act", pid}; Option opt = Option.parseOptions(cmdArray); diff --git a/src/test/java/com/alibaba/druid/bvt/console/OptionTest.java b/src/test/java/com/alibaba/druid/bvt/console/OptionTest.java index f6f97420b6..1c738be72a 100644 --- a/src/test/java/com/alibaba/druid/bvt/console/OptionTest.java +++ b/src/test/java/com/alibaba/druid/bvt/console/OptionTest.java @@ -1,5 +1,8 @@ package com.alibaba.druid.bvt.console; +import org.junit.runner.notification.Failure; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; import com.alibaba.druid.support.console.OptionParseException; import com.alibaba.druid.support.console.Option; @@ -16,7 +19,7 @@ public class OptionTest extends TestCase { Assert.assertTrue(opt.printSqlData()); Assert.assertTrue(opt.printDataSourceData()); Assert.assertFalse(opt.printActiveConn()); - Assert.assertEquals(opt.getVmid(), 200); + Assert.assertEquals(opt.getPid(), 200); cmdArray = new String[] {"-act", "738"}; opt = Option.parseOptions(cmdArray); @@ -24,25 +27,38 @@ public class OptionTest extends TestCase { Assert.assertFalse(opt.printSqlData()); Assert.assertFalse(opt.printDataSourceData()); Assert.assertTrue(opt.printActiveConn()); - Assert.assertEquals(opt.getStyle(), Option.PrintStyle.HORIZONTAL); - Assert.assertEquals(opt.getVmid(), 738); + Assert.assertEquals(opt.getPid(), 738); - cmdArray = new String[] {"-ds", "-s2", "1319"}; + cmdArray = new String[] {"-ds", "-detail", "1319"}; opt = Option.parseOptions(cmdArray); Assert.assertNotNull(opt); Assert.assertFalse(opt.printSqlData()); Assert.assertTrue(opt.printDataSourceData()); Assert.assertFalse(opt.printActiveConn()); - Assert.assertEquals(opt.getStyle(), Option.PrintStyle.VERTICAL); - Assert.assertEquals(opt.getVmid(), 1319); + Assert.assertTrue(opt.isDetailPrint()); + Assert.assertEquals(opt.getPid(), 1319); cmdArray = new String[] {"-sql","-id","5","200"}; opt = Option.parseOptions(cmdArray); Assert.assertNotNull(opt); Assert.assertEquals(opt.getId(), 5); - Assert.assertEquals(opt.getVmid(), 200); + Assert.assertEquals(opt.getPid(), 200); + + cmdArray = new String[] {"-sql","-id","5","200", "3"}; + opt = Option.parseOptions(cmdArray); + Assert.assertNotNull(opt); + Assert.assertEquals(opt.getId(), 5); + Assert.assertEquals(opt.getPid(), 200); + Assert.assertEquals(opt.getInterval(), 3); + cmdArray = new String[] {"-ds","-id","5", "-detail", "200", "3"}; + opt = Option.parseOptions(cmdArray); + Assert.assertNotNull(opt); + Assert.assertEquals(opt.getId(), 5); + Assert.assertEquals(opt.getPid(), 200); + Assert.assertEquals(opt.getInterval(), 3); + Assert.assertEquals(opt.isDetailPrint(), true); //not enough arguments cmdArray = new String[] {}; @@ -53,7 +69,7 @@ public class OptionTest extends TestCase { Assert.assertNotNull(e); } - //need vmid + //need pid cmdArray = new String[] {"-ds"}; try { opt = Option.parseOptions(cmdArray); @@ -67,4 +83,11 @@ public class OptionTest extends TestCase { public void test_printHelp() throws Exception { Option.printHelp(); } + + public static void main(String[] args) { + Result result = JUnitCore.runClasses(OptionTest.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + } } From d265c178c971ba15aeee02fcf4b35fa0010309f1 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 17:31:20 +0800 Subject: [PATCH 19/55] add testcase & bug fixed for executeQuery return null --- .../druid/filter/FilterEventAdapter.java | 12 ++- .../pool/DruidPooledPreparedStatement.java | 4 + .../druid/pool/DruidPooledStatement.java | 4 + .../druid/bvt/filter/FilterChainImplTest.java | 4 +- .../bvt/filter/FilterChainImplTest2.java | 45 ++++++++++ .../bvt/filter/FilterChainImplTest3.java | 86 +++++++++++++++++++ .../druid/bvt/pool/TransactionTest.java | 35 ++++++++ 7 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/TransactionTest.java diff --git a/src/main/java/com/alibaba/druid/filter/FilterEventAdapter.java b/src/main/java/com/alibaba/druid/filter/FilterEventAdapter.java index 2e7585cd25..b96ff057d9 100644 --- a/src/main/java/com/alibaba/druid/filter/FilterEventAdapter.java +++ b/src/main/java/com/alibaba/druid/filter/FilterEventAdapter.java @@ -301,8 +301,10 @@ public abstract class FilterEventAdapter extends FilterAdapter { try { ResultSetProxy resultSet = super.statement_executeQuery(chain, statement, sql); - statementExecuteQueryAfter(statement, sql, resultSet); - resultSetOpenAfter(resultSet); + if (resultSet != null) { + statementExecuteQueryAfter(statement, sql, resultSet); + resultSetOpenAfter(resultSet); + } return resultSet; } catch (SQLException error) { @@ -462,9 +464,11 @@ public abstract class FilterEventAdapter extends FilterAdapter { ResultSetProxy resultSet = chain.preparedStatement_executeQuery(statement); - statementExecuteQueryAfter(statement, statement.getSql(), resultSet); + if (resultSet != null) { + statementExecuteQueryAfter(statement, statement.getSql(), resultSet); - resultSetOpenAfter(resultSet); + resultSetOpenAfter(resultSet); + } return resultSet; } catch (SQLException error) { diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java index f9139cad2e..4f462272f1 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java @@ -180,6 +180,10 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement conn.beforeExecute(); try { ResultSet rs = stmt.executeQuery(); + + if (rs == null) { + return null; + } DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); resultSetTrace.add(poolableResultSet); diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java index e402d1472f..b5fa0f6e7f 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java @@ -107,6 +107,10 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement { conn.beforeExecute(); try { ResultSet rs = stmt.executeQuery(sql); + + if (rs == null) { + return rs; + } DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); resultSetTrace.add(poolableResultSet); diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java index b6c95ed016..010d02154d 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java @@ -200,7 +200,7 @@ public class FilterChainImplTest extends TestCase { ResultSet rs = stmt.executeQuery(); rs.next(); - Assert.assertNull(rs.getURL(1)); + Assert.assertNull(rs.getRowId(1)); rs.close(); stmt.close(); @@ -215,7 +215,7 @@ public class FilterChainImplTest extends TestCase { ResultSet rs = stmt.executeQuery(); rs.next(); - Assert.assertNull(rs.getURL("1")); + Assert.assertNull(rs.getRowId("1")); rs.close(); stmt.close(); diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java index e246f5f19e..f6727d889a 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java @@ -3,6 +3,7 @@ package com.alibaba.druid.bvt.filter; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.Types; +import java.util.Collections; import junit.framework.Assert; import junit.framework.TestCase; @@ -601,4 +602,48 @@ public class FilterChainImplTest2 extends TestCase { stmt.close(); conn.close(); } + + public void test_getObject() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getObject(1)); + + stmt.close(); + conn.close(); + } + + public void test_getObject_1() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getObject("1")); + + stmt.close(); + conn.close(); + } + + public void test_getObject_2() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getObject(1, Collections.>emptyMap())); + + stmt.close(); + conn.close(); + } + + public void test_getObject_3() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select ?"); + stmt.registerOutParameter(1, Types.VARCHAR); + + Assert.assertNull(stmt.getObject("1", Collections.>emptyMap())); + + stmt.close(); + conn.close(); + } } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java new file mode 100644 index 0000000000..a0de090f6e --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java @@ -0,0 +1,86 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.mock.MockCallableStatement; +import com.alibaba.druid.mock.MockConnection; +import com.alibaba.druid.mock.MockDriver; +import com.alibaba.druid.mock.MockStatement; +import com.alibaba.druid.mock.MockStatementBase; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcUtils; + +public class FilterChainImplTest3 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + + dataSource.setDriver(new MockDriver() { + public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException { + return null; + } + + public MockStatement createMockStatement(MockConnection conn) { + return new MockStatement(conn) { + public ResultSet getResultSet() throws SQLException { + return null; + } + }; + } + }); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_executeQuery() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select ?"); + stmt.setNull(1, Types.VARCHAR); + Assert.assertNull(stmt.executeQuery()); + stmt.close(); + conn.close(); + } + + public void test_executeQuery_2() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareCall("select ?"); + stmt.setNull(1, Types.VARCHAR); + Assert.assertNull(stmt.executeQuery()); + stmt.close(); + conn.close(); + } + + public void test_executeQuery_3() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + Assert.assertNull(stmt.executeQuery("select 1")); + stmt.close(); + conn.close(); + } + + public void test_execute() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + stmt.execute("select 1"); + Assert.assertNull(stmt.getResultSet()); + stmt.close(); + conn.close(); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/TransactionTest.java b/src/test/java/com/alibaba/druid/bvt/pool/TransactionTest.java new file mode 100644 index 0000000000..55e61934e5 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/TransactionTest.java @@ -0,0 +1,35 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; + +public class TransactionTest 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_txn() throws Exception { + Connection conn = dataSource.getConnection(); + + conn.setAutoCommit(false); + for (int i = 0; i < 100; ++i) { + PreparedStatement stmt = conn.prepareStatement("select + " + (i % 10)); + stmt.executeUpdate(); + stmt.close(); + } + conn.commit(); + conn.close(); + } +} From 477842d660388ec4dac5ad3dcc4111b08044440b Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 22:31:47 +0800 Subject: [PATCH 20/55] add testcase --- .../druid/bvt/filter/FilterChainImplTest.java | 16 +++++++++++++++- .../druid/bvt/filter/FilterChainImplTest2.java | 4 +++- .../druid/bvt/filter/FilterChainImplTest3.java | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java index 010d02154d..7598766d72 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java @@ -12,10 +12,12 @@ import java.sql.Types; import junit.framework.Assert; import junit.framework.TestCase; +import com.alibaba.druid.filter.FilterAdapter; import com.alibaba.druid.filter.FilterChainImpl; import com.alibaba.druid.mock.MockNClob; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.proxy.jdbc.ConnectionProxy; +import com.alibaba.druid.proxy.jdbc.StatementProxy; import com.alibaba.druid.util.JdbcUtils; public class FilterChainImplTest extends TestCase { @@ -26,7 +28,9 @@ public class FilterChainImplTest extends TestCase { dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); - dataSource.setFilters("stat"); + dataSource.setFilters("stat,log4j,wall"); + dataSource.getProxyFilters().add(new FilterAdapter() {} ); + dataSource.setDbType("mysql"); dataSource.init(); } @@ -69,9 +73,19 @@ public class FilterChainImplTest extends TestCase { conn.close(); } + public void test_unwrap_8() throws Exception { + Connection conn = dataSource.getConnection(); + Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Clob) new MockNClob()) instanceof NClob); + conn.close(); + } + public void test_unwrap_7() throws Exception { Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (NClob) null)); } + + public void test_unwrap_9() throws Exception { + Assert.assertNull(new FilterChainImpl(dataSource).wrap((StatementProxy) null, (NClob) null)); + } public void test_getUnicodeStream() throws Exception { Connection conn = dataSource.getConnection(); diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java index f6727d889a..fbfc19b684 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java @@ -8,6 +8,7 @@ import java.util.Collections; import junit.framework.Assert; import junit.framework.TestCase; +import com.alibaba.druid.filter.FilterAdapter; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.util.JdbcUtils; @@ -19,7 +20,8 @@ public class FilterChainImplTest2 extends TestCase { dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); - dataSource.setFilters("stat"); + dataSource.setFilters("stat,log4j,wall"); + dataSource.getProxyFilters().add(new FilterAdapter() {} ); dataSource.init(); } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java index a0de090f6e..ed049e6a2e 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest3.java @@ -10,6 +10,7 @@ import java.sql.Types; import junit.framework.Assert; import junit.framework.TestCase; +import com.alibaba.druid.filter.FilterAdapter; import com.alibaba.druid.mock.MockCallableStatement; import com.alibaba.druid.mock.MockConnection; import com.alibaba.druid.mock.MockDriver; @@ -26,7 +27,9 @@ public class FilterChainImplTest3 extends TestCase { dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); - dataSource.setFilters("stat"); + dataSource.setFilters("stat,log4j,wall"); + dataSource.getProxyFilters().add(new FilterAdapter() {} ); + dataSource.setDbType("mysql"); dataSource.setDriver(new MockDriver() { public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException { From 96949d176c203defb3d8bc1c6fbe0e6f12a83135 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 23:21:50 +0800 Subject: [PATCH 21/55] add testcase & remove Filter not used code --- .../java/com/alibaba/druid/filter/Filter.java | 2 - .../alibaba/druid/filter/FilterAdapter.java | 5 - .../druid/bvt/filter/FilterChainImplTest.java | 198 +++++++++--------- .../druid/bvt/proxy/DruidDriverTest.java | 12 -- 4 files changed, 103 insertions(+), 114 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/Filter.java b/src/main/java/com/alibaba/druid/filter/Filter.java index 6a0c2a5ce5..a7f4845231 100644 --- a/src/main/java/com/alibaba/druid/filter/Filter.java +++ b/src/main/java/com/alibaba/druid/filter/Filter.java @@ -1277,8 +1277,6 @@ public interface Filter { void clob_truncate(FilterChain chain, ClobProxy wrapper, long len) throws SQLException; - void loadConfig(AbstractDruidFilterConfig druidFilterConfig); - void dataSource_releaseConnection(FilterChain chain, DruidPooledConnection connection) throws SQLException; DruidPooledConnection dataSource_getConnection(FilterChain chain, DruidDataSource dataSource, long maxWaitMillis) throws SQLException; diff --git a/src/main/java/com/alibaba/druid/filter/FilterAdapter.java b/src/main/java/com/alibaba/druid/filter/FilterAdapter.java index 32804f030a..4d280bbea6 100644 --- a/src/main/java/com/alibaba/druid/filter/FilterAdapter.java +++ b/src/main/java/com/alibaba/druid/filter/FilterAdapter.java @@ -44,7 +44,6 @@ import javax.management.NotificationBroadcasterSupport; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; -import com.alibaba.druid.proxy.config.AbstractDruidFilterConfig; import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; import com.alibaba.druid.proxy.jdbc.ClobProxy; import com.alibaba.druid.proxy.jdbc.ConnectionProxy; @@ -60,10 +59,6 @@ import com.alibaba.druid.proxy.jdbc.StatementProxy; */ public abstract class FilterAdapter extends NotificationBroadcasterSupport implements Filter { - @Override - public void loadConfig(AbstractDruidFilterConfig DruidFilterConfig) { - } - @Override public void init(DataSourceProxy dataSource) { } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java index 7598766d72..29bfa43d10 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest.java @@ -29,7 +29,8 @@ public class FilterChainImplTest extends TestCase { dataSource.setUrl("jdbc:mock:xxx"); dataSource.setFilters("stat,log4j,wall"); - dataSource.getProxyFilters().add(new FilterAdapter() {} ); + dataSource.getProxyFilters().add(new FilterAdapter() { + }); dataSource.setDbType("mysql"); dataSource.init(); @@ -46,43 +47,50 @@ public class FilterChainImplTest extends TestCase { public void test_unwrap() throws Exception { Assert.assertNull(new FilterChainImpl(dataSource).unwrap(null, null)); } - + public void test_unwrap_1() throws Exception { Assert.assertNull(new FilterChainImpl(dataSource).wrap((Connection) null, null)); } - + public void test_unwrap_2() throws Exception { - Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Statement) null)); + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (Statement) null)); } - + public void test_unwrap_3() throws Exception { - Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (PreparedStatement) null, "")); + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (PreparedStatement) null, "")); } - + public void test_unwrap_4() throws Exception { - Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (CallableStatement) null, "")); + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (CallableStatement) null, "")); } - + public void test_unwrap_5() throws Exception { - Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Clob) null)); + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (Clob) null)); } - + public void test_unwrap_6() throws Exception { Connection conn = dataSource.getConnection(); - Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, new MockNClob()) instanceof NClob); + Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + new MockNClob()) instanceof NClob); conn.close(); } - + public void test_unwrap_8() throws Exception { Connection conn = dataSource.getConnection(); - Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (Clob) new MockNClob()) instanceof NClob); + Assert.assertTrue(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (Clob) new MockNClob()) instanceof NClob); conn.close(); } - + public void test_unwrap_7() throws Exception { - Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) null, (NClob) null)); + Assert.assertNull(new FilterChainImpl(dataSource).wrap((ConnectionProxy) dataSource.getConnection().getConnection(), + (NClob) null)); } - + public void test_unwrap_9() throws Exception { Assert.assertNull(new FilterChainImpl(dataSource).wrap((StatementProxy) null, (NClob) null)); } @@ -93,297 +101,297 @@ public class FilterChainImplTest extends TestCase { stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getUnicodeStream(1)); - + rs.close(); stmt.close(); conn.close(); } - + public void test_getUnicodeStream_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getUnicodeStream("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getRef() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getRef(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getRef_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getRef("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getArray() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getArray(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getArray_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getArray("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getURL() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getURL(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getURL_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getURL("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getRowId() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getRowId(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getRowId_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getRowId("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNClob() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNClob(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNClob_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNClob("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getSQLXML() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getSQLXML(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getSQLXML_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getSQLXML("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNString() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNString(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNString_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNString("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNCharacterStream() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNCharacterStream(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getNCharacterStream_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getNCharacterStream("1")); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getObject() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getObject(1)); - + rs.close(); stmt.close(); conn.close(); - + } - + public void test_getObject_1() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); stmt.setNull(1, Types.VARCHAR); ResultSet rs = stmt.executeQuery(); rs.next(); - + Assert.assertNull(rs.getObject("1")); - + rs.close(); stmt.close(); conn.close(); - + } } diff --git a/src/test/java/com/alibaba/druid/bvt/proxy/DruidDriverTest.java b/src/test/java/com/alibaba/druid/bvt/proxy/DruidDriverTest.java index 599155ad36..35cc17c2ef 100644 --- a/src/test/java/com/alibaba/druid/bvt/proxy/DruidDriverTest.java +++ b/src/test/java/com/alibaba/druid/bvt/proxy/DruidDriverTest.java @@ -35,10 +35,6 @@ public class DruidDriverTest extends TestCase { } - @Override - public void loadConfig(AbstractDruidFilterConfig druidFilterConfig) { - - } } static class PrivateJdbcFilterAdapter extends FilterAdapter { @@ -46,11 +42,6 @@ public class DruidDriverTest extends TestCase { private PrivateJdbcFilterAdapter(){ } - - @Override - public void loadConfig(AbstractDruidFilterConfig druidFilterConfig) { - - } } public static class InitErrorJdbcFilterAdapter extends FilterAdapter { @@ -59,9 +50,6 @@ public class DruidDriverTest extends TestCase { throw new InstantiationException("init error"); } - @Override - public void loadConfig(AbstractDruidFilterConfig druidFilterConfig) { - } } static class PrivateDriver extends MockDriver { From dd52d01bb9bf61a4fee8659dc8cafd017f545bb2 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 28 Aug 2012 23:49:36 +0800 Subject: [PATCH 22/55] fix testcase --- .../java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java index fbfc19b684..fd5a0fe91e 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/FilterChainImplTest2.java @@ -22,6 +22,7 @@ public class FilterChainImplTest2 extends TestCase { dataSource.setUrl("jdbc:mock:xxx"); dataSource.setFilters("stat,log4j,wall"); dataSource.getProxyFilters().add(new FilterAdapter() {} ); + dataSource.setDbType("mysql"); dataSource.init(); } From 884f9d372bab12a389f2022ff7d779ed891b0c66 Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 29 Aug 2012 07:16:02 +0800 Subject: [PATCH 23/55] remove compile warnnings --- src/main/java/com/alibaba/druid/filter/Filter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/alibaba/druid/filter/Filter.java b/src/main/java/com/alibaba/druid/filter/Filter.java index a7f4845231..b7ec85617c 100644 --- a/src/main/java/com/alibaba/druid/filter/Filter.java +++ b/src/main/java/com/alibaba/druid/filter/Filter.java @@ -46,7 +46,6 @@ import java.util.Properties; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; -import com.alibaba.druid.proxy.config.AbstractDruidFilterConfig; import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; import com.alibaba.druid.proxy.jdbc.ClobProxy; import com.alibaba.druid.proxy.jdbc.ConnectionProxy; From 597a8e04441978e8bb27b5e17328d868ba20dc0a Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 29 Aug 2012 07:28:40 +0800 Subject: [PATCH 24/55] sql.html & sql.json support parameter : dataSourceId --- .../druid/stat/DruidStatManagerFacade.java | 6 +- .../alibaba/druid/stat/DruidStatService.java | 56 +++++++++++++++---- .../druid/util/DruidDataSourceUtils.java | 15 +++++ .../com/alibaba/druid/util/StringUtils.java | 23 -------- .../support/http/resources/datasource.html | 2 + .../resources/support/http/resources/sql.html | 5 +- 6 files changed, 71 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java b/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java index fbf6e386f8..a4886a56f6 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java +++ b/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java @@ -133,9 +133,13 @@ public class DruidStatManagerFacade { return null; } - public List> getSqlStatDataList() { + public List> getSqlStatDataList(Integer dataSourceId) { List> result = new ArrayList>(); for (Object datasource : getDruidDataSourceInstances()) { + if (dataSourceId != null && dataSourceId.intValue() != System.identityHashCode(datasource)) { + continue; + } + for (Object sqlStat : DruidDataSourceUtils.getSqlStatMap(datasource).values()) { Map data = JdbcSqlStatUtils.getData(sqlStat); diff --git a/src/main/java/com/alibaba/druid/stat/DruidStatService.java b/src/main/java/com/alibaba/druid/stat/DruidStatService.java index 3e38805d60..a597f60e1b 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidStatService.java +++ b/src/main/java/com/alibaba/druid/stat/DruidStatService.java @@ -79,7 +79,7 @@ public class DruidStatService implements DruidStatServiceMBean { public String service(String url) { - Map parameters = StringUtils.getParameters(url); + Map parameters = getParameters(url); if (url.equals("/basic.json")) { return returnJSONResult(RESULT_CODE_SUCCESS, statManagerFacade.returnJSONBasicStat()); @@ -138,7 +138,7 @@ public class DruidStatService implements DruidStatServiceMBean { if (url.startsWith("/webapp.json")) { return returnJSONResult(RESULT_CODE_SUCCESS, getWebAppStatDataList(parameters)); } - + if (url.startsWith("/websession.json")) { return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatDataList(parameters)); } @@ -187,7 +187,7 @@ public class DruidStatService implements DruidStatServiceMBean { List> array = WebAppStatManager.getInstance().getSessionStatData(); return comparatorOrderBy(array, parameters); } - + private List> getWebAppStatDataList(Map parameters) { List> array = WebAppStatManager.getInstance().getWebAppStatData(); return comparatorOrderBy(array, parameters); @@ -199,7 +199,8 @@ public class DruidStatService implements DruidStatServiceMBean { // when parameters is null String orderBy, orderType = null; - Integer page, perPageCount = null; + Integer page = DEFAULT_PAGE; + Integer perPageCount = DEFAULT_PER_PAGE_COUNT; if (parameters == null) { orderBy = DEFAULT_ORDER_TYPE; orderType = DEFAULT_ORDER_TYPE; @@ -208,15 +209,19 @@ public class DruidStatService implements DruidStatServiceMBean { } else { orderBy = parameters.get("orderBy"); orderType = parameters.get("orderType"); - page = Integer.parseInt(parameters.get("page")); - perPageCount = Integer.parseInt(parameters.get("perPageCount")); + String pageParam = parameters.get("page"); + if (pageParam != null && pageParam.length() != 0) { + page = Integer.parseInt(pageParam); + } + String pageCountParam = parameters.get("perPageCount"); + if (pageCountParam != null && pageCountParam.length() > 0) { + perPageCount = Integer.parseInt(pageCountParam); + } } // others,such as order orderBy = orderBy == null ? DEFAULT_ORDERBY : orderBy; orderType = orderType == null ? DEFAULT_ORDER_TYPE : orderType; - page = page == null ? DEFAULT_PAGE : page; - perPageCount = perPageCount == null ? DEFAULT_PER_PAGE_COUNT : perPageCount; if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE; @@ -234,8 +239,16 @@ public class DruidStatService implements DruidStatServiceMBean { } private List> getSqlStatDataList(Map parameters) { - List> array = statManagerFacade.getSqlStatDataList(); - return comparatorOrderBy(array, parameters); + Integer dataSourceId = null; + + String dataSourceIdParam = parameters.get("dataSourceId"); + if (dataSourceIdParam != null && dataSourceIdParam.length() > 0) { + dataSourceId = Integer.parseInt(dataSourceIdParam); + } + + List> array = statManagerFacade.getSqlStatDataList(dataSourceId); + List> sortedArray = comparatorOrderBy(array, parameters); + return sortedArray; } private String getSqlStat(Integer id) { @@ -310,4 +323,27 @@ public class DruidStatService implements DruidStatServiceMBean { } } + public static Map getParameters(String url) { + if (url == null || (url = url.trim()).length() == 0) { + return Collections. emptyMap(); + } + + String parametersStr = StringUtils.subString(url, "?", null); + if (parametersStr == null || parametersStr.length() == 0) { + return Collections. emptyMap(); + } + + String[] parametersArray = parametersStr.split("&"); + Map parameters = new LinkedHashMap(); + + for (String parameterStr : parametersArray) { + int index = parameterStr.indexOf("="); + if (index <= 0) continue; + + String name = parameterStr.substring(0, index); + String value = parameterStr.substring(index + 1); + parameters.put(name, value); + } + return parameters; + } } diff --git a/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java b/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java index 4720e6c1af..fd9a5cceba 100644 --- a/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java +++ b/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java @@ -44,6 +44,21 @@ public class DruidDataSourceUtils { } } + public static long getID(Object druidDatasource) { + if (druidDatasource.getClass() == DruidDataSource.class) { + return ((DruidDataSource) druidDatasource).getID(); + } + + try { + Method method = druidDatasource.getClass().getMethod("getID"); + Object obj = method.invoke(druidDatasource); + return (Long) obj; + } catch (Exception e) { + LOG.error("getID error", e); + return -1; + } + } + public static String getName(Object druidDatasource) { if (druidDatasource.getClass() == DruidDataSource.class) { return ((DruidDataSource) druidDatasource).getName(); diff --git a/src/main/java/com/alibaba/druid/util/StringUtils.java b/src/main/java/com/alibaba/druid/util/StringUtils.java index 7b8e7dabd2..5edb3d7cd0 100644 --- a/src/main/java/com/alibaba/druid/util/StringUtils.java +++ b/src/main/java/com/alibaba/druid/util/StringUtils.java @@ -15,9 +15,6 @@ */ package com.alibaba.druid.util; -import java.util.LinkedHashMap; -import java.util.Map; - import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; @@ -75,26 +72,6 @@ public class StringUtils { } } - public static Map getParameters(String url) { - if (url == null || (url = url.trim()).length() == 0) return null; - - String parametersStr = subString(url, "?", null); - if (parametersStr == null || parametersStr.length() == 0) return null; - - String[] parametersArray = parametersStr.split("&"); - Map parameters = new LinkedHashMap(); - - for (String parameterStr : parametersArray) { - int index = parameterStr.indexOf("="); - if (index <= 0) continue; - - String name = parameterStr.substring(0, index); - String value = parameterStr.substring(index + 1); - parameters.put(name, value); - } - return parameters; - } - public static boolean equals(String a, String b) { if (a == null) { return b == null; diff --git a/src/main/resources/support/http/resources/datasource.html b/src/main/resources/support/http/resources/datasource.html index 7170193d88..28f7b2908e 100644 --- a/src/main/resources/support/http/resources/datasource.html +++ b/src/main/resources/support/http/resources/datasource.html @@ -118,6 +118,8 @@ html += 'PollingConnection InfoViewInfo for polling connection. [View JSON API]' + html += 'SQL ListViewInfo for SQL. [View JSON API]' html += '
'; return html; diff --git a/src/main/resources/support/http/resources/sql.html b/src/main/resources/support/http/resources/sql.html index 939a33cf18..2d7055c825 100644 --- a/src/main/resources/support/http/resources/sql.html +++ b/src/main/resources/support/http/resources/sql.html @@ -75,14 +75,15 @@