Search in sources :

Example 1 with JdbcDataSourceStat

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

the class StatFilter method connection_connect.

public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
    ConnectionProxy connection = null;
    long startNano = System.nanoTime();
    long startTime = System.currentTimeMillis();
    long nanoSpan;
    long nowTime = System.currentTimeMillis();
    JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
    dataSourceStat.getConnectionStat().beforeConnect();
    try {
        connection = chain.connection_connect(info);
        nanoSpan = System.nanoTime() - startNano;
    } catch (SQLException ex) {
        dataSourceStat.getConnectionStat().connectError(ex);
        throw ex;
    }
    dataSourceStat.getConnectionStat().afterConnected(nanoSpan);
    if (connection != null) {
        JdbcConnectionStat.Entry statEntry = getConnectionInfo(connection);
        dataSourceStat.getConnections().put(connection.getId(), statEntry);
        statEntry.setConnectTime(new Date(startTime));
        statEntry.setConnectTimespanNano(nanoSpan);
        statEntry.setEstablishNano(System.nanoTime());
        statEntry.setEstablishTime(nowTime);
        statEntry.setConnectStackTrace(new Exception());
        dataSourceStat.getConnectionStat().setActiveCount(dataSourceStat.getConnections().size());
    }
    return connection;
}
Also used : SQLException(java.sql.SQLException) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) JdbcConnectionStat(com.alibaba.druid.stat.JdbcConnectionStat) Date(java.util.Date) SQLException(java.sql.SQLException)

Example 2 with JdbcDataSourceStat

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

the class StatFilter method resultSetOpenAfter.

@Override
protected void resultSetOpenAfter(ResultSetProxy resultSet) {
    JdbcDataSourceStat dataSourceStat = resultSet.getStatementProxy().getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getResultSetStat().beforeOpen();
    resultSet.setConstructNano();
    StatFilterContext.getInstance().resultSet_open();
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 3 with JdbcDataSourceStat

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

the class StatFilter method statementCreateAfter.

@Override
public void statementCreateAfter(StatementProxy statement) {
    JdbcDataSourceStat dataSourceStat = statement.getConnectionProxy().getDirectDataSource().getDataSourceStat();
    dataSourceStat.getStatementStat().incrementCreateCounter();
    super.statementCreateAfter(statement);
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 4 with JdbcDataSourceStat

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

the class StatFilter method dataSource_releaseConnection.

@Override
public void dataSource_releaseConnection(FilterChain chain, DruidPooledConnection conn) throws SQLException {
    chain.dataSource_recycle(conn);
    long nanos = System.nanoTime() - conn.getConnectedTimeNano();
    long millis = nanos / (1000L * 1000L);
    JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
    dataSourceStat.getConnectionHoldHistogram().record(millis);
    StatFilterContext.getInstance().pool_connection_close(nanos);
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 5 with JdbcDataSourceStat

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

the class StatFilter method connection_close.

@Override
public void connection_close(FilterChain chain, ConnectionProxy connection) throws SQLException {
    if (connection.getCloseCount() == 0) {
        long nowNano = System.nanoTime();
        JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
        dataSourceStat.getConnectionStat().incrementConnectionCloseCount();
        JdbcConnectionStat.Entry connectionInfo = getConnectionInfo(connection);
        long aliveNanoSpan = nowNano - connectionInfo.getEstablishNano();
        JdbcConnectionStat.Entry existsConnection = dataSourceStat.getConnections().remove(connection.getId());
        if (existsConnection != null) {
            dataSourceStat.getConnectionStat().afterClose(aliveNanoSpan);
        }
    }
    chain.connection_close(connection);
// duplicate close, C3P0等连接池,在某些情况下会关闭连接多次。
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) JdbcConnectionStat(com.alibaba.druid.stat.JdbcConnectionStat)

Aggregations

JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)26 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)6 SQLException (java.sql.SQLException)5 JdbcConnectionStat (com.alibaba.druid.stat.JdbcConnectionStat)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)3 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)3 Connection (java.sql.Connection)3 Properties (java.util.Properties)3 DbType (com.alibaba.druid.DbType)2 Savepoint (java.sql.Savepoint)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 Filter (com.alibaba.druid.filter.Filter)1 FilterChain (com.alibaba.druid.filter.FilterChain)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 StatFilter (com.alibaba.druid.filter.stat.StatFilter)1 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)1 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)1