Search in sources :

Example 26 with JdbcSqlStat

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

the class TestOnBorrowFileAndNameTest method test_stat.

public void test_stat() throws Exception {
    String sql = "SELECT NOW()";
    JdbcSqlStat.setContextSqlName("select_now");
    JdbcSqlStat.setContextSqlFile("test_file");
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    conn.close();
    Assert.assertEquals(true, stmt.isClosed());
    Assert.assertEquals(true, rs.isClosed());
    rs.close();
    stmt.close();
    dataSource.shrink();
    JdbcStatManager.getInstance().getDataSourceList();
    Assert.assertEquals(1, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
    Assert.assertEquals(2, dataSource.getDataSourceStat().getSqlList().size());
    Iterator<JdbcSqlStat> iterator = dataSource.getDataSourceStat().getSqlStatMap().values().iterator();
    JdbcSqlStat sql_0 = iterator.next();
    JdbcSqlStat sql_1 = iterator.next();
    Assert.assertEquals("SELECT 1", sql_0.getSql());
    Assert.assertNull(sql_0.getFile());
    Assert.assertNull(sql_0.getName());
    Assert.assertEquals("SELECT NOW()", sql_1.getSql());
    Assert.assertEquals("test_file", sql_1.getFile());
    Assert.assertEquals("select_now", sql_1.getName());
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 27 with JdbcSqlStat

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

the class DumpTest method test_getStatData.

public void test_getStatData() throws Exception {
    String sql = "select ?, ?";
    Connection conn = dataSource.getConnection();
    Connection conn1 = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "xxx");
    stmt.setInt(2, 33);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.close();
    conn.close();
    conn1.close();
    Map<String, Object> statData = dataSource.getStatData();
    Assert.assertEquals(2, statData.get("PoolingCount"));
    Assert.assertEquals(2, statData.get("PoolingPeak"));
    Assert.assertEquals(2L, statData.get("LogicConnectCount"));
    Assert.assertEquals(2L, statData.get("LogicCloseCount"));
    Assert.assertEquals(0L, statData.get("LogicConnectErrorCount"));
    Assert.assertEquals(1, dataSource.getSqlStatMap().size());
    JdbcSqlStat sqlStat = dataSource.getSqlStatMap().get(sql);
    Assert.assertNotNull(sqlStat);
    Assert.assertNotNull(dataSource.getSqlStat(sqlStat.getId()));
    Assert.assertNotNull(dataSource.getSqlStat((int) sqlStat.getId()));
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 28 with JdbcSqlStat

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

the class StatFilterAfterResetTest method test_stat.

public void test_stat() throws Exception {
    final String sql = "SELECT 1";
    Assert.assertTrue(dataSource.isInited());
    JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
    Assert.assertNull(sqlStat);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        rs.next();
        rs.close();
        sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        Assert.assertEquals("first failed", 1, sqlStat.getExecuteAndResultHoldTimeHistogramSum());
        rs.close();
        Assert.assertEquals("second failed", 1, sqlStat.getExecuteAndResultHoldTimeHistogramSum());
        stmt.close();
        conn.close();
        Assert.assertEquals(1, sqlStat.getExecuteAndResultHoldTimeHistogramSum());
    }
    JdbcStatManager.getInstance().reset();
    Assert.assertFalse(sqlStat.isRemoved());
    JdbcStatManager.getInstance().reset();
    Assert.assertTrue(sqlStat.isRemoved());
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        rs.next();
        rs.close();
        conn.close();
    }
    Assert.assertNotSame(sqlStat, dataSource.getDataSourceStat().getSqlStat(sql));
    {
        Assert.assertEquals(0, sqlStat.getExecuteAndResultHoldTimeHistogramSum());
    }
    sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
    Assert.assertEquals(1, sqlStat.getExecuteAndResultHoldTimeHistogramSum());
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 29 with JdbcSqlStat

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

the class StatFilterClobTest method test_clob.

public void test_clob() throws Exception {
    String sql = "select ?";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    MockClob clob = new MockClob();
    stmt.setClob(1, clob);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    Assert.assertTrue(rs.getObject(1) instanceof ClobProxy);
    rs.close();
    stmt.close();
    conn.close();
    JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
    Assert.assertNotNull(sqlStat);
    Assert.assertEquals(1, sqlStat.getClobOpenCount());
    sqlStat.reset();
    Assert.assertEquals(0, sqlStat.getClobOpenCount());
}
Also used : ClobProxy(com.alibaba.druid.proxy.jdbc.ClobProxy) JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) MockClob(com.alibaba.druid.mock.MockClob) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 30 with JdbcSqlStat

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

the class StatFilterExecErrorTest method test_stat.

public void test_stat() throws Exception {
    Connection conn = dataSource.getConnection();
    String sql = "select 'x'";
    PreparedStatement stmt = conn.prepareStatement("select 'x'");
    JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
    Assert.assertEquals(0, sqlStat.getReadStringLength());
    try {
        stmt.executeQuery();
    } catch (SQLException eror) {
    } finally {
        JdbcUtils.close(stmt);
        JdbcUtils.close(conn);
    }
    Assert.assertEquals(1, sqlStat.getErrorCount());
    Assert.assertEquals(0, sqlStat.getRunningCount());
    sqlStat.reset();
}
Also used : JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

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