Search in sources :

Example 21 with JdbcSqlStat

use of com.alibaba.druid.stat.JdbcSqlStat 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 22 with JdbcSqlStat

use of com.alibaba.druid.stat.JdbcSqlStat 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)

Example 23 with JdbcSqlStat

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

the class StatFilter method resultSet_close.

@Override
public void resultSet_close(FilterChain chain, ResultSetProxy resultSet) throws SQLException {
    long nanos = System.nanoTime() - resultSet.getConstructNano();
    int fetchRowCount = resultSet.getFetchRowCount();
    JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
    dataSourceStat.getResultSetStat().afterClose(nanos);
    dataSourceStat.getResultSetStat().addFetchRowCount(fetchRowCount);
    dataSourceStat.getResultSetStat().incrementCloseCounter();
    StatFilterContext.getInstance().addFetchRowCount(fetchRowCount);
    String sql = resultSet.getSql();
    if (sql != null) {
        JdbcSqlStat sqlStat = resultSet.getSqlStat();
        if (sqlStat != null && resultSet.getCloseCount() == 0) {
            sqlStat.addFetchRowCount(fetchRowCount);
            long stmtExecuteNano = resultSet.getStatementProxy().getLastExecuteTimeNano();
            sqlStat.addResultSetHoldTimeNano(stmtExecuteNano, nanos);
            if (resultSet.getReadStringLength() > 0) {
                sqlStat.addStringReadLength(resultSet.getReadStringLength());
            }
            if (resultSet.getReadBytesLength() > 0) {
                sqlStat.addReadBytesLength(resultSet.getReadBytesLength());
            }
            if (resultSet.getOpenInputStreamCount() > 0) {
                sqlStat.addInputStreamOpenCount(resultSet.getOpenInputStreamCount());
            }
            if (resultSet.getOpenReaderCount() > 0) {
                sqlStat.addReaderOpenCount(resultSet.getOpenReaderCount());
            }
        }
    }
    chain.resultSet_close(resultSet);
    StatFilterContext.getInstance().resultSet_close(nanos);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Savepoint(java.sql.Savepoint)

Example 24 with JdbcSqlStat

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

the class StatFilter method blobOpenAfter.

private void blobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, Blob blob) {
    dataSourceStat.incrementBlobOpenCount();
    if (stmt != null) {
        JdbcSqlStat sqlStat = stmt.getSqlStat();
        if (sqlStat != null) {
            sqlStat.incrementBlobOpenCount();
        }
    }
    StatFilterContext.getInstance().blob_open();
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat)

Example 25 with JdbcSqlStat

use of com.alibaba.druid.stat.JdbcSqlStat 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);
    Profiler.enter(sql, 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)

Aggregations

JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)42 Connection (java.sql.Connection)31 PreparedStatement (java.sql.PreparedStatement)31 ResultSet (java.sql.ResultSet)29 JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)6 SQLException (java.sql.SQLException)3 Savepoint (java.sql.Savepoint)3 MockClob (com.alibaba.druid.mock.MockClob)2 MockConnection (com.alibaba.druid.mock.MockConnection)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)2 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)2 JdbcConnectionStat (com.alibaba.druid.stat.JdbcConnectionStat)2 MockBlob (com.alibaba.druid.mock.MockBlob)1 MockNClob (com.alibaba.druid.mock.MockNClob)1 MockRowId (com.alibaba.druid.mock.MockRowId)1 ClobProxy (com.alibaba.druid.proxy.jdbc.ClobProxy)1 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 Time (java.sql.Time)1