bug fixed for useGloalDataSourceStat

This commit is contained in:
wenshao
2013-05-10 02:31:36 +08:00
parent 0cf04d27f1
commit dc45b9d57e
3 changed files with 26 additions and 3 deletions
@@ -471,8 +471,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
initExceptionSorter();
initValidConnectionChecker();
if (useGloalDataSourceStat) {
if (isUseGloalDataSourceStat()) {
dataSourceStat = JdbcDataSourceStat.getGlobal();
if (dataSourceStat.getDbType() == null) {
dataSourceStat.setDbType(this.getDbType());
}
if (dataSourceStat == null) {
dataSourceStat = new JdbcDataSourceStat("Global", "Global", this.dbType);
JdbcDataSourceStat.setGlobal(dataSourceStat);
@@ -113,6 +113,14 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
sqlStatMap = new LRUCache<String, JdbcSqlStat>(maxSize, 16, 0.75f, false);
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public void reset() {
blobOpenCount.set(0);
@@ -266,6 +274,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
if (sqlStat == null) {
sqlStat = new JdbcSqlStat(sql);
sqlStat.setDbType(this.dbType);
sqlStat.setName(this.name);
sqlStatMap.put(sql, sqlStat);
}
@@ -3,14 +3,18 @@ package com.alibaba.druid.bvt.proxy.filter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Assert;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.JdbcDataSourceStat;
import com.alibaba.druid.stat.JdbcStatManager;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
public class GlobalStatTest0 extends TestCase {
@@ -41,6 +45,7 @@ public class GlobalStatTest0 extends TestCase {
Assert.assertEquals(0, JdbcStatManager.getInstance().getSqlList().size());
}
@SuppressWarnings("unchecked")
public void test_execute() throws Exception {
{
Connection conn = dataSourceA.getConnection();
@@ -69,6 +74,12 @@ public class GlobalStatTest0 extends TestCase {
Assert.assertSame(JdbcDataSourceStat.getGlobal(), dataSourceB.getDataSourceStat());
Assert.assertEquals(1, JdbcStatManager.getInstance().getSqlList().size());
String json = JSONUtils.toJSONString(JdbcStatManager.getInstance().getSqlList());
List<Map<String, Object>> sqlList = (List<Map<String, Object>>) JSONUtils.parse(json);
Map<String, Object> sqlInfo = sqlList.get(0);
Assert.assertEquals(JdbcConstants.MOCK, sqlInfo.get("DbType"));
Assert.assertEquals(2, sqlInfo.get("ExecuteCount"));
Assert.assertEquals(2, sqlInfo.get("FetchRowCount"));
}
}