当前位置:网站首页>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-30 22:04:00 【Game programming】
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_executeQuerystatementExecuteQueryBeforestatementExecuteQueryAfter
View 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_executeQuerystatementExecuteQueryAfter
We 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 ~
author : Huawei cloud developer Alliance
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Introduction to go web programming: a probe into the excellent test library gocovey
- vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
- How to use data sets in machine learning?
- 【BSP视频教程】BSP视频教程第19期:单片机BootLoader的AES加密实战,含上位机和下位机代码全开源(2022-06-26)
- 十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
- A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
- Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
- Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
- MFC interface library bcgcontrolbar v33.0 - desktop alarm window, grid control upgrade, etc
- 机器学习工作要求研究生吗?
猜你喜欢
Analysis of PostgreSQL storage structure
国产数据库乱象
5G 在智慧医疗中的需求
十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises
B_ QuRT_ User_ Guide(32)
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir
Rethink healthy diet based on intestinal microbiome
Look at the top 10 capabilities of alicloud cipu
Qsort function and Simulation Implementation of qsort function
随机推荐
京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
Do a scrollbar thinking
去中心化交易所系统开发技术原理丨数字货币去中心化交易所系统开发(说明案例)
Vite2 is compatible with lower versions of chrome (such as Sogou 80). Some grammars requiring higher versions are processed through polyfills
Coredns modifying upstream
Zhoushaojian, rare
Stinky tofu made by Grandma
1-21 jsonp interface
交易所系统开发如何开发?数字货币交易所系统开发成熟技术案例
国产数据库乱象
1-15 nodemon
Alibaba Kube eventer MySQL sink simple usage record
十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
微服務鏈路風險分析
Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
Excitatory neurotransmitter glutamate and brain health
1-12 初步认识Express
腾讯3年,功能测试进阶自动化测试,送给在手工测试中迷茫的你
Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
Windbg调试工具介绍