use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.
the class ParamTest method test_default.
public void test_default() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:");
Assert.assertEquals(0, dataSource.getProxyFilters().size());
dataSource.setFilters("stat");
JdbcStatManager.getInstance().reset();
dataSource.init();
JdbcDataSourceStat stat = dataSource.getDataSourceStat();
Assert.assertEquals(0, stat.getConnectionStat().getConnectCount());
Assert.assertEquals(1, dataSource.getProxyFilters().size());
for (int i = 0; i < 2; ++i) {
Connection conn = dataSource.getConnection();
Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
Assert.assertEquals(0, stat.getConnectionStat().getCloseCount());
conn.close();
Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
// logic
Assert.assertEquals(0, stat.getConnectionStat().getCloseCount());
// close不会导致计数器+1
}
dataSource.close();
// Assert.assertEquals(0, JdbcStatManager.getInstance().getDataSources().size());
Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
Assert.assertEquals(1, stat.getConnectionStat().getCloseCount());
JdbcStatManager.getInstance().reset();
Assert.assertEquals(1, stat.getConnectionStat().getConnectCount());
Assert.assertEquals(1, stat.getConnectionStat().getCloseCount());
}
use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.
the class StatFilter method statementPrepareCallAfter.
@Override
public void statementPrepareCallAfter(CallableStatementProxy statement) {
JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
dataSourceStat.getStatementStat().incrementPrepareCallCount();
JdbcSqlStat sqlStat = createSqlStat(statement, statement.getSql());
statement.setSqlStat(sqlStat);
}
use of com.alibaba.druid.stat.JdbcDataSourceStat 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);
}
use of com.alibaba.druid.stat.JdbcDataSourceStat 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 {
String dbType = this.dbType;
if (dbType == null) {
dbType = dataSource.getDbType();
}
sql = mergeSql(sql, dbType);
return dataSourceStat.createSqlStat(sql);
}
}
use of com.alibaba.druid.stat.JdbcDataSourceStat 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);
}
Aggregations