public void test_count_filter() throws Exception { DataSourceProxyConfig config = new DataSourceProxyConfig(); config.setUrl(""); config.setRawUrl("jdbc:mock:"); StatFilter filter = new StatFilter(); MockDriver driver = new MockDriver(); DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config); filter.init(dataSource); config.getFilters().add(filter); Connection conn = dataSource.connect(null); Statement stmt = conn.createStatement(); ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql); rs.close(); stmt.close(); conn.close(); conn.close(); dataSource.getCompositeData(); dataSource.getProperties(); dataSource.getDataSourceMBeanDomain(); }
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()); }
@Override public Connection connect(String url, Properties info) throws SQLException { if (!acceptsURL(url)) { return null; } connectCount.incrementAndGet(); DataSourceProxyImpl dataSource = getDataSource(url, info); return dataSource.connect(info); }
/** * 参数定义: com.alibaba.druid.log.LogFilter=filter com.alibaba.druid.log.LogFilter.p1=prop-value * com.alibaba.druid.log.LogFilter.p2=prop-value * * @param url * @return * @throws SQLException */ private DataSourceProxyImpl getDataSource(String url, Properties info) throws SQLException { DataSourceProxyImpl dataSource = proxyDataSources.get(url); if (dataSource == null) { DataSourceProxyConfig config = parseConfig(url, info); Driver rawDriver = createDriver(config.getRawDriverClassName()); DataSourceProxyImpl newDataSource = new DataSourceProxyImpl(rawDriver, config); { String property = System.getProperty("druid.filters"); if (property != null && property.length() > 0) { for (String filterItem : property.split(",")) { FilterManager.loadFilter(config.getFilters(), filterItem); } } } { int dataSourceId = createDataSourceId(); newDataSource.setId(dataSourceId); for (Filter filter : config.getFilters()) { filter.init(newDataSource); } } DataSourceProxy oldDataSource = proxyDataSources.putIfAbsent(url, newDataSource); if (oldDataSource == null) { if (config.isJmxOption()) { JMXUtils.register("com.alibaba.druid:type=JdbcStat", JdbcStatManager.getInstance()); } } dataSource = proxyDataSources.get(url); } return dataSource; }
@Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { DataSourceProxyImpl dataSource = getDataSource(url, info); return dataSource.getRawDriver().getPropertyInfo(dataSource.getConfig().getRawUrl(), info); }