Search in sources :

Example 1 with JdbcSqlStat

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

the class StatFilter method clobOpenAfter.

private void clobOpenAfter(JdbcDataSourceStat dataSourceStat, StatementProxy stmt, ClobProxy clob) {
    dataSourceStat.incrementClobOpenCount();
    if (stmt != null) {
        JdbcSqlStat sqlStat = stmt.getSqlStat();
        if (sqlStat != null) {
            sqlStat.incrementClobOpenCount();
        }
    }
    StatFilterContext.getInstance().clob_open();
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat)

Example 2 with JdbcSqlStat

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

the class StatFilter method statementExecuteBatchBefore.

@Override
protected void statementExecuteBatchBefore(StatementProxy statement) {
    final String sql = statement.getBatchSql();
    final int batchSize = statement.getBatchSqlList().size();
    JdbcSqlStat sqlStat = statement.getSqlStat();
    if (sqlStat == null || sqlStat.isRemoved()) {
        sqlStat = createSqlStat(statement, sql);
        statement.setSqlStat(sqlStat);
    }
    if (sqlStat != null) {
        sqlStat.addExecuteBatchCount(batchSize);
    }
    internalBeforeStatementExecute(statement, sql);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) Savepoint(java.sql.Savepoint)

Example 3 with JdbcSqlStat

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

the class StatFilter method statement_executeErrorAfter.

@Override
protected void statement_executeErrorAfter(StatementProxy statement, String sql, Throwable error) {
    ConnectionProxy connection = statement.getConnectionProxy();
    JdbcConnectionStat.Entry connectionCounter = getConnectionInfo(connection);
    long nanos = System.nanoTime() - statement.getLastExecuteStartNano();
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().error(error);
    dataSourceStat.getStatementStat().afterExecute(nanos);
    connectionCounter.error(error);
    // SQL
    JdbcSqlStat sqlStat = statement.getSqlStat();
    if (sqlStat != null) {
        sqlStat.decrementExecutingCount();
        sqlStat.error(error);
        sqlStat.addExecuteTime(statement.getLastExecuteType(), statement.isFirstResultSet(), nanos);
        statement.setLastExecuteTimeNano(nanos);
    }
    StatFilterContext.getInstance().executeAfter(sql, nanos, error);
    Profiler.release(nanos);
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) JdbcConnectionStat(com.alibaba.druid.stat.JdbcConnectionStat)

Example 4 with JdbcSqlStat

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

the class JdbcSqlStatUtils method getData.

@SuppressWarnings("unchecked")
public static Map<String, Object> getData(Object jdbcSqlStat) {
    try {
        if (jdbcSqlStat.getClass() == JdbcSqlStat.class) {
            return ((JdbcSqlStat) jdbcSqlStat).getData();
        }
        Method method = jdbcSqlStat.getClass().getMethod("getData");
        Object obj = method.invoke(jdbcSqlStat);
        return (Map<String, Object>) obj;
    } catch (Exception e) {
        LOG.error("getData error", e);
        return null;
    }
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) Method(java.lang.reflect.Method) Map(java.util.Map)

Example 5 with JdbcSqlStat

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

the class StatFilterBuildSlowParameterTest method test_buildSlowSql.

public void test_buildSlowSql() throws Exception {
    long currentMillis = System.currentTimeMillis();
    String sql = "select ?, ?, ?, ?, ?";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    String dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, true);
        stmt.setInt(2, 123);
        stmt.setLong(3, 10001);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(true, parameters.get(0));
        Assert.assertEquals(123, parameters.get(1));
        Assert.assertEquals(10001, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    currentMillis = System.currentTimeMillis();
    date = new Date(System.currentTimeMillis());
    dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, false);
        stmt.setInt(2, 234);
        stmt.setLong(3, 10002);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(false, parameters.get(0));
        Assert.assertEquals(234, parameters.get(1));
        Assert.assertEquals(10002, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setNull(1, Types.VARCHAR);
        stmt.setString(2, buf.toString());
        stmt.setClob(3, new MockClob());
        stmt.setNClob(4, new MockNClob());
        stmt.setBlob(5, new MockBlob());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(null, parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals("<Clob>", parameters.get(2));
        Assert.assertEquals("<NClob>", parameters.get(3));
        Assert.assertEquals("<Blob>", parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("中国abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBinaryStream(1, new ByteInputStream(new byte[0]));
        stmt.setString(2, buf.toString());
        stmt.setTime(3, new Time(currentMillis));
        stmt.setBigDecimal(4, new BigDecimal("56789.123"));
        stmt.setRowId(5, new MockRowId());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals("<InputStream>", parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals(dateText, parameters.get(2));
        Assert.assertEquals(56789.123, parameters.get(3));
        Assert.assertEquals("<com.alibaba.druid.mock.MockRowId>", parameters.get(4));
    }
}
Also used : MockNClob(com.alibaba.druid.mock.MockNClob) JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) MockRowId(com.alibaba.druid.mock.MockRowId) ByteInputStream(org.nutz.lang.util.ByteInputStream) MockBlob(com.alibaba.druid.mock.MockBlob) MockClob(com.alibaba.druid.mock.MockClob) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

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