Search in sources :

Example 6 with StatementProxy

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

the class LogFilterTest method test_logFilter_2.

public void test_logFilter_2() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    {
        log4jFilter.init(dataSource);
        setLogDisableAll(log4jFilter, true);
        config.getFilters().add(log4jFilter);
    }
    CommonsLogFilter logFilter = new CommonsLogFilter();
    {
        logFilter.init(dataSource);
        setLogDisableAll(logFilter, true);
        config.getFilters().add(logFilter);
    }
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.BLOB, Types.CLOB, Types.NCLOB, Types.BINARY, Types.OTHER };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }
    };
    ConnectionProxy conn = (ConnectionProxy) dataSource.connect(new Properties());
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
    }
    {
        final MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                throw new SQLException();
            }
        };
        StatementProxy stmt = new StatementProxyImpl(conn, new MockStatement(conn) {

            public ResultSet getResultSet() throws SQLException {
                return rs;
            }
        }, 0);
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.statement_getResultSet(stmt);
    }
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        {
            logFilter.setResultSetLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
        {
            logFilter.setResultSetNextAfterLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
    }
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement)

Example 7 with StatementProxy

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

the class AsyncCloseTest2 method setUp.

protected void setUp() throws Exception {
    Field logField = DruidDataSource.class.getDeclaredField("LOG");
    logField.setAccessible(true);
    Log dataSourceLog = (Log) logField.get(null);
    if (dataSourceLog instanceof Log4jImpl) {
        this.log4jLog = ((Log4jImpl) dataSourceLog).getLog();
        this.log4jOldLevel = this.log4jLog.getLevel();
        this.log4jLog.setLevel(Level.FATAL);
    } else if (dataSourceLog instanceof NoLoggingImpl) {
        noLoggingImpl = (NoLoggingImpl) dataSourceLog;
        noLoggingImpl.setErrorEnabled(false);
    }
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:");
    dataSource.setAsyncCloseConnectionEnable(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setMaxActive(16);
    dataSource.getProxyFilters().add(new FilterAdapter() {

        @Override
        public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
            throw new SQLException();
        }
    });
    dataSource.setExceptionSorter(new MyExceptionSorter());
    dataSource.init();
    connExecutor = Executors.newFixedThreadPool(128);
    closeExecutor = Executors.newFixedThreadPool(128);
}
Also used : Field(java.lang.reflect.Field) Log4jImpl(com.alibaba.druid.support.logging.Log4jImpl) Log(com.alibaba.druid.support.logging.Log) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) NoLoggingImpl(com.alibaba.druid.support.logging.NoLoggingImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 8 with StatementProxy

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

the class AsyncCloseTest3 method setUp.

protected void setUp() throws Exception {
    // m;
    xmx = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() / (1000 * 1000);
    System.gc();
    Field logField = DruidDataSource.class.getDeclaredField("LOG");
    logField.setAccessible(true);
    Log dataSourceLog = (Log) logField.get(null);
    if (dataSourceLog instanceof Log4jImpl) {
        this.log4jLog = ((Log4jImpl) dataSourceLog).getLog();
        this.log4jOldLevel = this.log4jLog.getLevel();
        this.log4jLog.setLevel(Level.FATAL);
    } else if (dataSourceLog instanceof NoLoggingImpl) {
        noLoggingImpl = (NoLoggingImpl) dataSourceLog;
        noLoggingImpl.setErrorEnabled(false);
    }
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:");
    // dataSource.setAsyncCloseConnectionEnable(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setMaxActive(16);
    dataSource.getProxyFilters().add(new FilterAdapter() {

        @Override
        public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
            throw new SQLException();
        }
    });
    dataSource.setExceptionSorter(new MyExceptionSorter());
    dataSource.init();
    connExecutor = Executors.newFixedThreadPool(2);
    closeExecutor = Executors.newFixedThreadPool(2);
}
Also used : Field(java.lang.reflect.Field) Log4jImpl(com.alibaba.druid.support.logging.Log4jImpl) Log(com.alibaba.druid.support.logging.Log) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) NoLoggingImpl(com.alibaba.druid.support.logging.NoLoggingImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 9 with StatementProxy

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

the class FilterEventAdapter method connection_createStatement.

@Override
public StatementProxy connection_createStatement(FilterChain chain, ConnectionProxy connection, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    StatementProxy statement = super.connection_createStatement(chain, connection, resultSetType, resultSetConcurrency, resultSetHoldability);
    statementCreateAfter(statement);
    return statement;
}
Also used : CallableStatementProxy(com.alibaba.druid.proxy.jdbc.CallableStatementProxy) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) PreparedStatementProxy(com.alibaba.druid.proxy.jdbc.PreparedStatementProxy)

Aggregations

StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)9 SQLException (java.sql.SQLException)7 FilterChain (com.alibaba.druid.filter.FilterChain)6 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)6 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)5 PreparedStatementProxy (com.alibaba.druid.proxy.jdbc.PreparedStatementProxy)5 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)4 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)3 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 CallableStatementProxy (com.alibaba.druid.proxy.jdbc.CallableStatementProxy)2 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)2 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)2 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)2 Log (com.alibaba.druid.support.logging.Log)2 Log4jImpl (com.alibaba.druid.support.logging.Log4jImpl)2 NoLoggingImpl (com.alibaba.druid.support.logging.NoLoggingImpl)2 Field (java.lang.reflect.Field)2 Properties (java.util.Properties)2 FilterEventAdapter (com.alibaba.druid.filter.FilterEventAdapter)1