当前位置:网站首页>How to use filters in jfinal to monitor Druid for SQL execution?
How to use filters in jfinal to monitor Druid for SQL execution?
2022-06-29 22:00:00 【Huawei cloud developer Alliance】
Abstract : What I want to do at first is to intercept through interceptors SQL perform , But it was tested and found that , The filter can listen to at least every SQL Execution and return results of . therefore , Record this exploration .
This article is shared from Huawei cloud community 《jfinal Filter monitoring in Druid Of SQL perform 【 May 07】》, author :KevinQ .
What I want to do at first is to intercept through interceptors SQL perform , For example, similar to PageHelper This plug-in , Through interceptors or filters , Manually modify SQL sentence , To meet certain business requirements , For example, perform paging , Or restrict access to data permissions . But I found the information that the filter is not for this , This is what the database middleware does , such as MyCat etc. .
But it was tested and found that , The filter can listen to at least every SQL Execution and return results of . therefore , Record this exploration .
Configure filters
stay jfinal In the startup configuration class of , There is a function configPlugin(Plugins me) Function to configure the plug-in , This function will be in jfinal Call at startup , The argument to this function is Plugins me, This parameter is a plug-in manager , You can add plug-ins through this plug-in manager .
Database plug-ins Druid Is added in this function .
public void configPlugin(Plugins me) { DruidPlugin druidPlugin = createDruidPlugin_holdoa(); druidPlugin.setPublicKey(p.get("publicKeydebug").trim()); wallFilter = new WallFilter(); wallFilter.setDbType("mysql"); druidPlugin_oa.addFilter(wallFilter); druidPlugin_oa.addFilter(new StatFilter()); me.add(druidPlugin);}We refer to WallFilter as well as StatFilter Also create a filter class :
import com.alibaba.druid.filter.FilterEventAdapter;public class DataScopeFilter extends FilterEventAdapter {}We found that FilterEventAdapter There are several methods in :
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {...}protected void statementExecuteUpdateBefore(StatementProxy statement, String sql) {...}protected void statementExecuteUpdateAfter(StatementProxy statement, String sql, int updateCount) {...}protected void statementExecuteQueryBefore(StatementProxy statement, String sql) {...}protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) {...}protected void statementExecuteBefore(StatementProxy statement, String sql) {...}protected void statementExecuteAfter(StatementProxy statement, String sql, boolean result) {...}Let's repeat these methods to see ( exclude Update Method , Because we are more concerned with query statements )
package xxxx.xxxx;import com.alibaba.druid.filter.FilterChain;import com.alibaba.druid.filter.FilterEventAdapter;import com.alibaba.druid.proxy.jdbc.ResultSetProxy;import com.alibaba.druid.proxy.jdbc.StatementProxy;import com.jfinal.kit.LogKit;import java.sql.SQLException;public class DataScopeFilter extends FilterEventAdapter { @Override public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException { LogKit.info("statement_execute"); return super.statement_execute(chain, statement, sql); } @Override protected void statementExecuteQueryBefore(StatementProxy statement, String sql) { LogKit.info("statementExecuteQueryBefore"); super.statementExecuteQueryBefore(statement, sql); } @Override protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) { LogKit.info("statementExecuteQueryAfter"); super.statementExecuteQueryAfter(statement, sql, resultSet); } @Override protected void statementExecuteBefore(StatementProxy statement, String sql) { LogKit.info("statementExecuteBefore"); super.statementExecuteBefore(statement, sql); } @Override protected void statementExecuteAfter(StatementProxy statement, String sql, boolean result) { LogKit.info("statementExecuteAfter"); super.statementExecuteAfter(statement, sql, result); } @Override public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException { LogKit.info("statement_executeQuery"); return super.statement_executeQuery(chain, statement, sql); }}And then again config Add a filter to the configuration class :
druidPlugin.addFilter(new DataScopeFilter());The execution sequence of the initiation is :
statement_executeQuerystatementExecuteQueryBeforestatementExecuteQueryAfterView parent code , It is found that the execution logic is , First, execute statement_executeQuery, And then because the method of the parent is called , The parent method body is :
@Override public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException { statementExecuteQueryBefore(statement, sql); try { ResultSetProxy resultSet = super.statement_executeQuery(chain, statement, sql); if (resultSet != null) { statementExecuteQueryAfter(statement, sql, resultSet); resultSetOpenAfter(resultSet); } return resultSet; } catch (SQLException error) { statement_executeErrorAfter(statement, sql, error); throw error; } catch (RuntimeException error) { statement_executeErrorAfter(statement, sql, error); throw error; } catch (Error error) { statement_executeErrorAfter(statement, sql, error); throw error; } }This further triggers statementExecuteQueryBefore Methods and statementExecuteQueryAfter Method .
So we , modify statement_executeQuery Method :
@Override public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException { statementExecuteQueryBefore(statement, sql); ResultSetProxy result = chain.statement_executeQuery(statement, sql); statementExecuteQueryAfter(statement, sql, result); return result; }such , Let the output result be :
statementExecuteQueryBeforestatement_executeQuerystatementExecuteQueryAfterWe can do it in Before perhaps After Method , such as : Record SQL The actual executor of , Operating time , Request execution SQL The interface of .
sql Be declared final type
Find out what's going on SQL stay Druid The corresponding class in is :DruidPooledPreparedStatement, Its class structure is :
public class DruidPooledPreparedStatement extends DruidPooledStatement implements PreparedStatement { private final static Log LOG = LogFactory.getLog(DruidPooledPreparedStatement.class); private final PreparedStatementHolder holder; private final PreparedStatement stmt; private final String sql; ....}This means , Once this class is created ,SQL After setting, it cannot be modified , therefore , We need to change SQL Words , It needs to be in prepared The object is modified to the corresponding execution before it is generated SQL.
In the course of debugging , It is found that the following method needs to be overwritten :
@Override public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, String sql) throws SQLException { // It can be modified SQL Purpose sql += " LIMIT 1"; PreparedStatementProxy statement = super.connection_prepareStatement(chain, connection, sql); statementPrepareAfter(statement); return statement; }We can add custom here SQL Modify the logic , For example, add data permissions and so on .
Click to follow , The first time to learn about Huawei's new cloud technology ~
边栏推荐
- Information available from radar echo
- 硅树脂油漆申请美国标准UL 790 Class A 合适吗?
- 小型圖書館項目總結
- Houdini graphic notes: VAT (3.0) import ue4/5 setup wizard [official document translation]
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- ASP dynamically creates table table
- Introduction to gaofen-3 satellite (GF-3)
- DB queries the database, merges two unrelated tables, adds non-existent fields, and assigns default values
- Alibaba keyword search commodity API interface (item_search- commodity search interface by keyword), Alibaba Search API interface
- Which securities to choose for securities account opening? In addition, is it safe to open an account online now?
猜你喜欢

Layer 3 loop brought by route Summary - solution experiment

Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech

ASP动态创建表格 Table

ASP using panel to realize simple registration page

Viewing technological changes through Huawei Corps (V): smart Park

DevCloud加持下的青软,让教育“智”上云端

CLI tool foundation of ros2 robot f1tenth

报表交付工程师
![[advanced ROS] Lecture 3 ROS file system and distributed communication](/img/9f/2c316f5b0aa36b63769672048da772.png)
[advanced ROS] Lecture 3 ROS file system and distributed communication

Bs-gx-018 student examination system based on SSM
随机推荐
What is unique about MES system?
The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem
Taro applet enables wxml code compression
Numpy's research imitation 1
Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle
2022 openvino DevCon unveils secrets! Intel and many partners deepen the construction of developer ecology and release the innovation potential of AI industry
Amazon Keyword Search API interface (item_search- Amazon product search interface by keyword), Amazon API interface
Taro2.* applet configuration sharing wechat circle of friends
Viewing technological changes through Huawei Corps (V): smart Park
STM32 minimum system construction (schematic diagram)
软件快速交付真的需要以安全为代价吗?
Goahead translation - Active Server Pages
My creation anniversary
Realization of graduation project topic selection system based on JSP
小型图书馆项目总结
Graduation summary of construction practice camp
MES系统究竟有何独特之处?
The explain function of the DALEX package of R language generates a machine learning model interpreter and predict for the specified classification prediction_ The parts function analyzes the contribu
Small library project summary
小型圖書館項目總結