Search in sources :

Example 6 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class StatFilterTest3 method test_dbType.

@SuppressWarnings("deprecation")
public void test_dbType() throws Exception {
    StatFilter filter = new StatFilter();
    Assert.assertFalse(filter.isMergeSql());
    filter.setDbType("mysql");
    filter.setMergeSql(true);
    Assert.assertTrue(filter.isMergeSql());
    Assert.assertEquals("mysql", filter.getDbType());
    Assert.assertEquals("SELECT ?\nLIMIT ?", filter.mergeSql("select 'x' limit 1"));
}
Also used : StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 7 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class SlowSqlMillisTest2 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(false);
    System.setProperty("druid.stat.slowSqlMillis", "500");
    dataSource.setFilters("stat");
    {
        StatFilter filter = (StatFilter) dataSource.getProxyFilters().get(0);
        Assert.assertEquals(3000, filter.getSlowSqlMillis());
    }
    dataSource.init();
}
Also used : StatFilter(com.alibaba.druid.filter.stat.StatFilter) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 8 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class ConnectPropertiesChangeTest method test_connectPropertiesChange.

public void test_connectPropertiesChange() throws Exception {
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Assert.assertEquals("3", dataSource.getConnectProperties().getProperty("a"));
    Assert.assertEquals("4", dataSource.getConnectProperties().getProperty("b"));
    Connection conn = dataSource.getConnection();
    conn.close();
    dataSource.setConnectionProperties("a=3;b=4");
    Assert.assertEquals(1, dataSourceLog.getInfoCount());
    dataSource.setConnectionProperties("b=5;c=6");
    Assert.assertEquals(2, dataSourceLog.getInfoCount());
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b"));
    Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c"));
    StatFilter filter = dataSource.unwrap(StatFilter.class);
    Assert.assertNotNull(filter);
    Assert.assertFalse(filter.isMergeSql());
    dataSource.setConnectionProperties("b=5;c=6;druid.stat.mergeSql=true");
    Assert.assertTrue(filter.isMergeSql());
    Assert.assertEquals(3, dataSource.getConnectProperties().size());
    Assert.assertEquals("true", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
    Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b"));
    Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c"));
    dataSource.setConnectionProperties("b=5;c=6;druid.stat.mergeSql=false");
    Assert.assertFalse(filter.isMergeSql());
    Assert.assertEquals("false", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
    Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b"));
    Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c"));
}
Also used : Connection(java.sql.Connection) StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 9 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class ConnectPropertiesChangeTest1 method test_connectPropertiesChange.

public void test_connectPropertiesChange() throws Exception {
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Connection conn = dataSource.getConnection();
    conn.close();
    StatFilter filter = dataSource.unwrap(StatFilter.class);
    Assert.assertNotNull(filter);
    Assert.assertFalse(filter.isMergeSql());
    Assert.assertEquals(234, dataSource.getDataSourceStat().getMaxSqlSize());
    dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.sql.MaxSize=456");
    Assert.assertEquals(456, dataSource.getDataSourceStat().getMaxSqlSize());
    Assert.assertTrue(filter.isMergeSql());
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Assert.assertEquals("true", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
    dataSource.setConnectionProperties("druid.stat.mergeSql=false");
    Assert.assertFalse(filter.isMergeSql());
    Assert.assertEquals("false", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
}
Also used : Connection(java.sql.Connection) StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 10 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class CounterFilterTest method test_countFilter.

public void test_countFilter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(null, config);
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    StatFilter filter = new StatFilter();
    filter.init(dataSource);
    dataSourceStat.reset();
    Assert.assertNull(StatFilter.getStatFilter(dataSource));
    Assert.assertNull(dataSourceStat.getSqlStat(Integer.MAX_VALUE));
    Assert.assertNull(dataSourceStat.getConnectionStat().getConnectLastTime());
    FilterChain chain = new FilterChainImpl(dataSource) {

        public ConnectionProxy connection_connect(Properties info) throws SQLException {
            throw new SQLException();
        }
    };
    Exception error = null;
    try {
        filter.connection_connect(chain, new Properties());
    } catch (SQLException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
    Assert.assertEquals(1, dataSourceStat.getConnectionStat().getConnectErrorCount());
    Assert.assertNotNull(dataSourceStat.getConnectionStat().getConnectLastTime());
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatFilter(com.alibaba.druid.filter.stat.StatFilter) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Properties(java.util.Properties) SQLException(java.sql.SQLException)

Aggregations

StatFilter (com.alibaba.druid.filter.stat.StatFilter)13 Connection (java.sql.Connection)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 MockDriver (com.alibaba.druid.mock.MockDriver)2 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)2 Statement (java.sql.Statement)2 FilterChain (com.alibaba.druid.filter.FilterChain)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)1 JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1