Search in sources :

Example 1 with DruidDataSourceStatValue

use of com.alibaba.druid.pool.DruidDataSourceStatValue in project druid by alibaba.

the class MonitorClient method collectSql.

@SuppressWarnings("resource")
public void collectSql() {
    if ((!collectSqlEnable) && !collectSqlWallEnable) {
        return;
    }
    Set<Object> dataSources = DruidDataSourceStatManager.getInstances().keySet();
    List<DruidDataSourceStatValue> statValueList = new ArrayList<DruidDataSourceStatValue>(dataSources.size());
    List<WallProviderStatValue> wallStatValueList = new ArrayList<WallProviderStatValue>();
    for (Object item : dataSources) {
        if (!(item instanceof DruidDataSource)) {
            continue;
        }
        DruidDataSource dataSource = (DruidDataSource) item;
        if (collectSqlEnable) {
            DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
            statValueList.add(statValue);
        }
        if (collectSqlWallEnable) {
            WallProviderStatValue wallStatValue = dataSource.getWallStatValue(true);
            if (wallStatValue != null && wallStatValue.getCheckCount() > 0) {
                wallStatValueList.add(wallStatValue);
            }
        }
    }
    MonitorContext ctx = createContext();
    if (statValueList.size() > 0) {
        dao.saveSql(ctx, statValueList);
    }
    if (wallStatValueList.size() > 0) {
        dao.saveSqlWall(ctx, wallStatValueList);
    }
}
Also used : DruidDataSourceStatValue(com.alibaba.druid.pool.DruidDataSourceStatValue) WallProviderStatValue(com.alibaba.druid.wall.WallProviderStatValue) ArrayList(java.util.ArrayList) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 2 with DruidDataSourceStatValue

use of com.alibaba.druid.pool.DruidDataSourceStatValue in project druid by alibaba.

the class SqlSkipCountTest method test_connectPropertiesChange.

public void test_connectPropertiesChange() throws Exception {
    for (int i = 0; i < 2000; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("select " + i);
        stmt.execute();
        stmt.close();
        conn.close();
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(1000, statValue.getSqlList().size());
        Assert.assertEquals(1000, statValue.getSqlSkipCount());
    }
    dataSource.setConnectionProperties("druid.stat.sql.MaxSize=2000");
    for (int i = 0; i < 2000; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("select " + i);
        stmt.execute();
        stmt.close();
        conn.close();
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(2000, statValue.getSqlList().size());
        Assert.assertEquals(0, statValue.getSqlSkipCount());
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(0, statValue.getSqlList().size());
        Assert.assertEquals(0, statValue.getSqlSkipCount());
    }
    dataSource.setConnectionProperties("druid.stat.sql.MaxSize=2000");
    for (int i = 0; i < 2000; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("select " + i);
        stmt.execute();
        stmt.close();
        conn.close();
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(2000, statValue.getSqlList().size());
        Assert.assertEquals(0, statValue.getSqlSkipCount());
    }
    dataSource.setConnectionProperties("druid.stat.sql.MaxSize=100");
    for (int i = 0; i < 2000; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("select " + i);
        stmt.execute();
        stmt.close();
        conn.close();
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(100, statValue.getSqlList().size());
        Assert.assertEquals(1900, statValue.getSqlSkipCount());
    }
    {
        DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
        Assert.assertEquals(0, statValue.getSqlList().size());
        Assert.assertEquals(0, statValue.getSqlSkipCount());
    }
}
Also used : DruidDataSourceStatValue(com.alibaba.druid.pool.DruidDataSourceStatValue) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 3 with DruidDataSourceStatValue

use of com.alibaba.druid.pool.DruidDataSourceStatValue in project druid by alibaba.

the class MonitorDaoJdbcImpl method saveSql.

@Override
public void saveSql(MonitorContext ctx, List<DruidDataSourceStatValue> dataSourceList) {
    save(dataSourceStatBeanInfo, ctx, dataSourceList);
    for (DruidDataSourceStatValue dataSourceStatValue : dataSourceList) {
        List<JdbcSqlStatValue> sqlList = dataSourceStatValue.getSqlList();
        save(sqlStatBeanInfo, ctx, sqlList);
    }
}
Also used : JdbcSqlStatValue(com.alibaba.druid.stat.JdbcSqlStatValue) DruidDataSourceStatValue(com.alibaba.druid.pool.DruidDataSourceStatValue)

Aggregations

DruidDataSourceStatValue (com.alibaba.druid.pool.DruidDataSourceStatValue)3 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 JdbcSqlStatValue (com.alibaba.druid.stat.JdbcSqlStatValue)1 WallProviderStatValue (com.alibaba.druid.wall.WallProviderStatValue)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1