Druid的Filter机制

JerryXia 发表于 , 阅读 (0)
之前的这篇文章介绍了Druid的连接池管理实现,但除了基本的连接管理,Druid还支持监控代理等功能,便于开发人员分析数据库操作的一些性能指标等。其主要使用Filter机制来实现,本文将介绍下其中的Filter实现

Filter接口定义

Filter接口中,可以看出,Filter主要重新定义了来自ConnectionStatementResultSet等对象的接口,并且均通过对应的Proxy对象来替代,每个接口中还带有一个FilterChain参数,主要是为了支持Filter链,如:

public interface Filter extends Wrapper {	   /**    * 初始化Filter    */     void init(DataSourceProxy dataSource);    /**     * 销毁Filter     */     void destroy();    // Connection相关方法    /**     * 创建一个连接     */    ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException;    /**     * 创建一个Statement代理对象     */    StatementProxy connection_createStatement(FilterChain chain, ConnectionProxy connection) throws SQLException;    // ...    // Statement相关方法    /**     * 执行查询操作     */    ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException;    /**     * 执行更新操作     */    int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql) throws SQLException;        // ...    // ResultSet相关方法    /**      * 下一条记录     */    boolean resultSet_next(FilterChain chain, ResultSetProxy resultSet) throws SQLException;    /**     * 关闭ResultSet     */    void resultSet_close(FilterChain chain, ResultSetProxy resultSet) throws SQLException;    // ...}    

Filter类似,FilterChain也是定义了ConnectionStatementResultSet等对象的相关接口,只是在实际调用这些对象前,会先执行一遍配置的Filter链。

 public interface FilterChain {     	/** 	 * 创建Connection代理 	 */    ConnectionProxy connection_connect(Properties info) throws SQLException;    /**     * 创建Statement代理     */    StatementProxy connection_createStatement(ConnectionProxy connection) throws SQLException;    /**     * 创建PrepareStatement代理     */     PreparedStatementProxy connection_prepareStatement(ConnectionProxy connection, String sql) throws SQLException;