Search in sources :

Example 1 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class FilterEventAdapter method connection_connect.

public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
    connection_connectBefore(chain, info);
    ConnectionProxy connection = super.connection_connect(chain, info);
    connection_connectAfter(connection);
    return connection;
}
Also used : ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 2 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy 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 3 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class EncodingConvertFilter method connection_connect.

public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
    ConnectionProxy conn = chain.connection_connect(info);
    CharsetParameter param = new CharsetParameter();
    param.setClientEncoding(info.getProperty(CharsetParameter.CLIENTENCODINGKEY));
    param.setServerEncoding(info.getProperty(CharsetParameter.SERVERENCODINGKEY));
    if (param.getClientEncoding() == null || "".equalsIgnoreCase(param.getClientEncoding())) {
        param.setClientEncoding(clientEncoding);
    }
    if (param.getServerEncoding() == null || "".equalsIgnoreCase(param.getServerEncoding())) {
        param.setServerEncoding(serverEncoding);
    }
    conn.putAttribute(ATTR_CHARSET_PARAMETER, param);
    conn.putAttribute(ATTR_CHARSET_CONVERTER, new CharsetConvert(param.getClientEncoding(), param.getServerEncoding()));
    return conn;
}
Also used : ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 4 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class MySqlValidConnectionChecker method isValidConnection.

public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (conn.isClosed()) {
        return false;
    }
    if (usePingMethod) {
        if (conn instanceof DruidPooledConnection) {
            conn = ((DruidPooledConnection) conn).getConnection();
        }
        if (conn instanceof ConnectionProxy) {
            conn = ((ConnectionProxy) conn).getRawObject();
        }
        if (clazz.isAssignableFrom(conn.getClass())) {
            if (validationQueryTimeout < 0) {
                validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
            }
            ping.invoke(conn, true, validationQueryTimeout * 1000);
            return true;
        }
    }
    String query = validateQuery;
    if (validateQuery == null || validateQuery.isEmpty()) {
        query = DEFAULT_VALIDATION_QUERY;
    }
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        if (validationQueryTimeout > 0) {
            stmt.setQueryTimeout(validationQueryTimeout);
        }
        rs = stmt.executeQuery(query);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 5 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class PGValidConnectionChecker method isValidConnection.

public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (validateQuery == null || validateQuery.isEmpty()) {
        validateQuery = this.defaultValidateQuery;
    }
    if (conn.isClosed()) {
        return false;
    }
    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
    }
    if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
    }
    int queryTimeout = validationQueryTimeout < 0 ? defaultQueryTimeout : validationQueryTimeout;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        stmt.setQueryTimeout(queryTimeout);
        rs = stmt.executeQuery(validateQuery);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Aggregations

ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)32 SQLException (java.sql.SQLException)19 FilterChain (com.alibaba.druid.filter.FilterChain)18 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)18 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)17 Properties (java.util.Properties)13 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)7 ResultSet (java.sql.ResultSet)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)3 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)3 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 Statement (java.sql.Statement)3 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 FilterEventAdapter (com.alibaba.druid.filter.FilterEventAdapter)2 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)2 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)2 PreparedStatementProxy (com.alibaba.druid.proxy.jdbc.PreparedStatementProxy)2 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)2 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)2