mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
File diff suppressed because it is too large
Load Diff
@@ -128,6 +128,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private volatile boolean enable = true;
|
||||
|
||||
private boolean resetStatEnable = true;
|
||||
private final AtomicLong resetCount = new AtomicLong();
|
||||
|
||||
private String initStackTrace;
|
||||
|
||||
@@ -214,6 +215,22 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = properties.getProperty("druid.clearFiltersEnable");
|
||||
if ("true".equals(property)) {
|
||||
this.setClearFiltersEnable(true);
|
||||
} else if ("false".equals(property)) {
|
||||
this.setClearFiltersEnable(false);
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = properties.getProperty("druid.resetStatEnable");
|
||||
if ("true".equals(property)) {
|
||||
this.setResetStatEnable(true);
|
||||
} else if ("false".equals(property)) {
|
||||
this.setResetStatEnable(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUseGloalDataSourceStat() {
|
||||
@@ -234,6 +251,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
public void setResetStatEnable(boolean resetStatEnable) {
|
||||
this.resetStatEnable = resetStatEnable;
|
||||
if (dataSourceStat != null) {
|
||||
dataSourceStat.setResetStatEnable(resetStatEnable);
|
||||
}
|
||||
}
|
||||
|
||||
public long getDiscardCount() {
|
||||
@@ -258,7 +278,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
public void resetStat() {
|
||||
if (!resetStatEnable) {
|
||||
if (!isResetStatEnable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -298,6 +318,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
transactionHistogram.reset();
|
||||
cachedPreparedStatementDeleteCount.set(0);
|
||||
recycleErrorCount.set(0);
|
||||
|
||||
resetCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public long getResetCount() {
|
||||
return this.resetCount.get();
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
@@ -558,6 +584,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
} else {
|
||||
dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbType, this.connectProperties);
|
||||
}
|
||||
dataSourceStat.setResetStatEnable(this.resetStatEnable);
|
||||
|
||||
connections = new DruidConnectionHolder[maxActive];
|
||||
|
||||
@@ -616,6 +643,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
String threadName = "Druid-ConnectionPool-Log-" + System.identityHashCode(this);
|
||||
logStatsThread = new LogStatsThread(threadName);
|
||||
logStatsThread.start();
|
||||
|
||||
this.resetStatEnable = false;
|
||||
}
|
||||
|
||||
private void createAndStartDestroyThread() {
|
||||
@@ -1032,6 +1061,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
this.discardConnection(holder.getConnection());
|
||||
holder.setDiscard(true);
|
||||
pooledConnection.disable(t);
|
||||
|
||||
LOG.error("discard connection", sqlEx);
|
||||
}
|
||||
|
||||
throw sqlEx;
|
||||
|
||||
@@ -24,6 +24,7 @@ import javax.management.ObjectName;
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public interface DruidDataSourceMBean extends DruidAbstractDataSourceMBean {
|
||||
long getResetCount();
|
||||
|
||||
boolean isEnable();
|
||||
|
||||
|
||||
@@ -66,12 +66,17 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
//
|
||||
});
|
||||
|
||||
private final ConcurrentMap<Long, JdbcConnectionStat.Entry> connections = new ConcurrentHashMap<Long, JdbcConnectionStat.Entry>(16, 0.75f, 1);
|
||||
private final ConcurrentMap<Long, JdbcConnectionStat.Entry> connections = new ConcurrentHashMap<Long, JdbcConnectionStat.Entry>(
|
||||
16,
|
||||
0.75f,
|
||||
1);
|
||||
|
||||
private final AtomicLong clobOpenCount = new AtomicLong();
|
||||
|
||||
private final AtomicLong blobOpenCount = new AtomicLong();
|
||||
|
||||
private boolean resetStatEnable = true;
|
||||
|
||||
private static JdbcDataSourceStat global;
|
||||
|
||||
static {
|
||||
@@ -92,6 +97,18 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
public static void setGlobal(JdbcDataSourceStat value) {
|
||||
global = value;
|
||||
}
|
||||
|
||||
public void configFromProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
public boolean isResetStatEnable() {
|
||||
return resetStatEnable;
|
||||
}
|
||||
|
||||
public void setResetStatEnable(boolean resetStatEnable) {
|
||||
this.resetStatEnable = resetStatEnable;
|
||||
}
|
||||
|
||||
public JdbcDataSourceStat(String name, String url){
|
||||
this(name, url, null);
|
||||
@@ -187,6 +204,10 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
if (!isResetStatEnable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
blobOpenCount.set(0);
|
||||
clobOpenCount.set(0);
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.alibaba.druid.bvt.filter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.filter.encoding.EncodingConvertFilter;
|
||||
import com.alibaba.druid.filter.stat.StatFilter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
|
||||
public class ClearFilterTest extends TestCase {
|
||||
public void test_filters() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
Assert.assertEquals(0, dataSource.getProxyFilters().size());
|
||||
dataSource.setFilters("encoding");
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
dataSource.setFilters("!stat");
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
Assert.assertEquals(StatFilter.class.getName(), dataSource.getFilterClassNames().get(0));
|
||||
dataSource.setClearFiltersEnable(false);
|
||||
dataSource.setFilters("!encoding");
|
||||
Assert.assertEquals(StatFilter.class.getName(), dataSource.getFilterClassNames().get(0));
|
||||
Assert.assertEquals(EncodingConvertFilter.class.getName(), dataSource.getFilterClassNames().get(1));
|
||||
|
||||
dataSource.setConnectionProperties("druid.clearFiltersEnable=false");
|
||||
Assert.assertFalse(dataSource.isClearFiltersEnable());
|
||||
|
||||
dataSource.setConnectionProperties("druid.clearFiltersEnable=true");
|
||||
Assert.assertTrue(dataSource.isClearFiltersEnable());
|
||||
|
||||
dataSource.setConnectionProperties("druid.clearFiltersEnable=xx"); // no change
|
||||
Assert.assertTrue(dataSource.isClearFiltersEnable());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,18 @@ public class TimeBetweenLogStatsMillisTest extends TestCase {
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
Assert.assertEquals(true, dataSource.isResetStatEnable());
|
||||
dataSource.init();
|
||||
Assert.assertEquals(1000, dataSource.getTimeBetweenLogStatsMillis());
|
||||
Assert.assertEquals(false, dataSource.isResetStatEnable());
|
||||
dataSource.resetStat();
|
||||
Assert.assertEquals(0, dataSource.getResetCount());
|
||||
dataSource.setConnectionProperties("druid.resetStatEnable=true");
|
||||
Assert.assertEquals(true, dataSource.isResetStatEnable());
|
||||
|
||||
dataSource.setConnectionProperties("druid.resetStatEnable=false");
|
||||
Assert.assertEquals(false, dataSource.isResetStatEnable());
|
||||
|
||||
dataSource.setConnectionProperties("druid.resetStatEnable=xxx");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public class MemTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// DruidDataSource dataSource = new DruidDataSource();
|
||||
// dataSource.setUrl("jdbc:mock:xxx");
|
||||
// dataSource.setInitialSize(1);
|
||||
// dataSource.setDbType("mysql");
|
||||
// dataSource.setFilters("mergeStat");
|
||||
// dataSource.setPoolPreparedStatements(true);
|
||||
// dataSource.setMaxOpenPreparedStatements(50);
|
||||
|
||||
// for (int i = 1000; i < 2000; ++i) {
|
||||
// String tableName = "t" + i;
|
||||
// String sql = "select * from " + tableName + " where " + tableName + ".id = " + i;
|
||||
// Connection conn = dataSource.getConnection();
|
||||
//
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
// stmt.execute();
|
||||
// stmt.close();
|
||||
//
|
||||
// conn.close();
|
||||
// }
|
||||
|
||||
// Connection conn = dataSource.getConnection();
|
||||
//
|
||||
String sql = "SELECT UMID, HWID, MAC, GUID, RID , GMT_CREATE, GMT_MODIFIED FROM umid_rid " + //
|
||||
"WHERE RID = ? " + //
|
||||
"LIMIT 10";
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
// stmt.execute();
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, "mysql");
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = ParameterizedOutputVisitorUtils.createParameterizedOutputVisitor(out, "mysql");
|
||||
stmt.accept(visitor);
|
||||
|
||||
Thread.sleep(1000 * 1000);
|
||||
// dataSource.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user