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());
}
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()));
}
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());
}
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());
}
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();
}
Aggregations