Search in sources :

Example 11 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class ParamTest method test_default.

public void test_default() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:");
    Assert.assertEquals(0, dataSource.getProxyFilters().size());
    dataSource.setFilters("stat");
    JdbcStatManager.getInstance().reset();
    dataSource.init();
    JdbcDataSourceStat stat = dataSource.getDataSourceStat();
    Assert.assertEquals(0, stat.getConnectionStat().getConnectCount());
    Assert.assertEquals(1, dataSource.getProxyFilters().size());
    for (int i = 0; i < 2; ++i) {
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
        Assert.assertEquals(0, stat.getConnectionStat().getCloseCount());
        conn.close();
        Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
        // logic
        Assert.assertEquals(0, stat.getConnectionStat().getCloseCount());
    // close不会导致计数器+1
    }
    dataSource.close();
    // Assert.assertEquals(0, JdbcStatManager.getInstance().getDataSources().size());
    Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
    Assert.assertEquals(1, stat.getConnectionStat().getCloseCount());
    JdbcStatManager.getInstance().reset();
    Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
    Assert.assertEquals(1, stat.getConnectionStat().getCloseCount());
}
Also used : Connection(java.sql.Connection) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 12 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class StatFilter method statementPrepareCallAfter.

@Override
public void statementPrepareCallAfter(CallableStatementProxy statement) {
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().incrementPrepareCallCount();
    JdbcSqlStat sqlStat = createSqlStat(statement, statement.getSql());
    statement.setSqlStat(sqlStat);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 13 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class StatFilter method statementPrepareAfter.

@Override
public void statementPrepareAfter(PreparedStatementProxy statement) {
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().incrementPrepareCounter();
    JdbcSqlStat sqlStat = createSqlStat(statement, statement.getSql());
    statement.setSqlStat(sqlStat);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 14 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class StatFilter method createSqlStat.

public JdbcSqlStat createSqlStat(StatementProxy statement, String sql) {
    DataSourceProxy dataSource = statement.getConnectionProxy().getDirectDataSource();
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
    String contextSql = context != null ? context.getSql() : null;
    if (contextSql != null && contextSql.length() > 0) {
        return dataSourceStat.createSqlStat(contextSql);
    } else {
        String dbType = this.dbType;
        if (dbType == null) {
            dbType = dataSource.getDbType();
        }
        sql = mergeSql(sql, dbType);
        return dataSourceStat.createSqlStat(sql);
    }
}
Also used : DataSourceProxy(com.alibaba.druid.proxy.jdbc.DataSourceProxy) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext)

Example 15 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class StatFilter method internalAfterStatementExecute.

private final void internalAfterStatementExecute(StatementProxy statement, boolean firstResult, int... updateCountArray) {
    final long nowNano = System.nanoTime();
    final long nanos = nowNano - statement.getLastExecuteStartNano();
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().afterExecute(nanos);
    final JdbcSqlStat sqlStat = statement.getSqlStat();
    if (sqlStat != null) {
        sqlStat.incrementExecuteSuccessCount();
        sqlStat.decrementRunningCount();
        sqlStat.addExecuteTime(statement.getLastExecuteType(), firstResult, nanos);
        statement.setLastExecuteTimeNano(nanos);
        if ((!statement.isFirstResultSet()) && statement.getLastExecuteType() == StatementExecuteType.Execute) {
            try {
                int updateCount = statement.getUpdateCount();
                sqlStat.addUpdateCount(updateCount);
            } catch (SQLException e) {
                LOG.error("getUpdateCount error", e);
            }
        } else {
            for (int updateCount : updateCountArray) {
                sqlStat.addUpdateCount(updateCount);
                sqlStat.addFetchRowCount(0);
                StatFilterContext.getInstance().addUpdateCount(updateCount);
            }
        }
        long millis = nanos / (1000 * 1000);
        if (millis >= slowSqlMillis) {
            String slowParameters = buildSlowParameters(statement);
            sqlStat.setLastSlowParameters(slowParameters);
            if (logSlowSql) {
                LOG.error("slow sql " + millis + " millis. " + statement.getLastExecuteSql() + "" + slowParameters);
            }
        }
    }
    String sql = statement.getLastExecuteSql();
    StatFilterContext.getInstance().executeAfter(sql, nanos, null);
    Profiler.release(nanos);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) SQLException(java.sql.SQLException) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Savepoint(java.sql.Savepoint)

Aggregations

JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)25 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)6 SQLException (java.sql.SQLException)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 JdbcConnectionStat (com.alibaba.druid.stat.JdbcConnectionStat)3 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)3 Connection (java.sql.Connection)3 Properties (java.util.Properties)3 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)2 Savepoint (java.sql.Savepoint)2 HashMap (java.util.HashMap)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 Filter (com.alibaba.druid.filter.Filter)1 FilterChain (com.alibaba.druid.filter.FilterChain)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 StatFilter (com.alibaba.druid.filter.stat.StatFilter)1 MockDriver (com.alibaba.druid.mock.MockDriver)1 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)1 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)1 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)1