Search in sources :

Example 1 with JdbcStatContext

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

the class StatFilter method statement_close.

@Override
public void statement_close(FilterChain chain, StatementProxy statement) throws SQLException {
    chain.statement_close(statement);
    JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().incrementStatementCloseCounter();
    JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
    if (context != null) {
        context.setName(null);
        context.setFile(null);
        context.setSql(null);
    }
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext)

Example 2 with JdbcStatContext

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

the class StatFilterTest2 method setStatSql.

public static final void setStatSql(String val) {
    JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
    if (context == null) {
        context = JdbcStatManager.getInstance().createStatContext();
        JdbcStatManager.getInstance().setStatContext(context);
    }
    context.setSql(val);
}
Also used : JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext)

Example 3 with JdbcStatContext

use of com.alibaba.druid.stat.JdbcStatContext 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 {
        DbType dbType = this.dbType;
        if (dbType == null) {
            dbType = DbType.of(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) DbType(com.alibaba.druid.DbType)

Example 4 with JdbcStatContext

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

the class StatFilter method internalBeforeStatementExecute.

private final void internalBeforeStatementExecute(StatementProxy statement, String sql) {
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().beforeExecute();
    final ConnectionProxy connection = statement.getConnectionProxy();
    final JdbcConnectionStat.Entry connectionCounter = getConnectionInfo(connection);
    statement.setLastExecuteStartNano();
    connectionCounter.setLastSql(sql);
    if (connectionStackTraceEnable) {
        connectionCounter.setLastStatementStatckTrace(new Exception());
    }
    // //////////SQL
    JdbcSqlStat sqlStat = statement.getSqlStat();
    if (sqlStat == null || sqlStat.isRemoved()) {
        sqlStat = createSqlStat(statement, sql);
        statement.setSqlStat(sqlStat);
    }
    JdbcStatContext statContext = JdbcStatManager.getInstance().getStatContext();
    if (statContext != null) {
        sqlStat.setName(statContext.getName());
        sqlStat.setFile(statContext.getFile());
    }
    boolean inTransaction = false;
    try {
        inTransaction = !statement.getConnectionProxy().getAutoCommit();
    } catch (SQLException e) {
        LOG.error("getAutoCommit error", e);
    }
    if (sqlStat != null) {
        sqlStat.setExecuteLastStartTime(System.currentTimeMillis());
        sqlStat.incrementRunningCount();
        if (inTransaction) {
            sqlStat.incrementInTransactionCount();
        }
    }
    StatFilterContext.getInstance().executeBefore(sql, inTransaction);
    String mergedSql;
    if (sqlStat != null) {
        mergedSql = sqlStat.getSql();
    } else {
        mergedSql = sql;
    }
    Profiler.enter(mergedSql, Profiler.PROFILE_TYPE_SQL);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) SQLException(java.sql.SQLException) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) JdbcConnectionStat(com.alibaba.druid.stat.JdbcConnectionStat) SQLException(java.sql.SQLException) JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext)

Example 5 with JdbcStatContext

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

the class ConnectionTest3 method setUp.

protected void setUp() throws Exception {
    DruidDataSourceStatManager.clear();
    driver = new MockDriver();
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(driver);
    dataSource.setInitialSize(1);
    dataSource.setMaxActive(2);
    dataSource.setMaxIdle(2);
    dataSource.setMinIdle(1);
    // 300 / 10
    dataSource.setMinEvictableIdleTimeMillis(300 * 1000);
    // 180 / 10
    dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setFilters("stat,trace");
    JdbcStatContext context = new JdbcStatContext();
    context.setTraceEnable(true);
    JdbcStatManager.getInstance().setStatContext(context);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext)

Aggregations

JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)9 MockDriver (com.alibaba.druid.mock.MockDriver)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)3 DbType (com.alibaba.druid.DbType)1 NullExceptionSorter (com.alibaba.druid.pool.vendor.NullExceptionSorter)1 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)1 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)1 JdbcConnectionStat (com.alibaba.druid.stat.JdbcConnectionStat)1 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)1 SQLException (java.sql.SQLException)1