英文:
How to create performance listener for MyBatis statements
问题
我想创建一种MyBatis语句的质量服务,一种全局性能监听器,它将分析所有执行的MyBatis语句,并记录那些返回太多命中或执行时间过长的语句。如果可能的话,尽量对现有业务逻辑的影响最小化。
英文:
I would like to create a sort of Quality of Service for MyBatis statements, a sort of global performance listener which will analyze all executed MyBatis statements and log those that return too many hits or take too long. If possible in such a way that the impact on the existing business logic is as small as possible.
答案1
得分: 1
I only did a quick test, but a custom interceptor (a.k.a. plugin) like the following should do the job.
以下是要翻译的内容:
The message is logged only when 1) the log level is DEBUG or finer and 2) the execution time exceeds the value of the constant THRESHOLD_NANOS
.
只有在以下条件满足时才记录该消息:1)日志级别为DEBUG或更高级别,2)执行时间超过常量THRESHOLD_NANOS
的值。
To register the interceptor...
要注册拦截器...
-
with XML configuration
-
使用XML配置
<plugins>
<plugin
interceptor="xxx.yyy.LogExecutionTimeInterceptor" />
</plugins>
@Bean
Interceptor getLogExecutionTimeInterceptor() {
return new LogExecutionTimeInterceptor();
}
英文:
I only did a quick test, but a custom interceptor (a.k.a. plugin) like the following should do the job.
The message is logged only when 1) the log level is DEBUG or finer and 2) the execution time exceeds the value of the constant THRESHOLD_NANOS
.
import java.text.MessageFormat;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
@Intercepts({
@Signature(
type = Executor.class,
method = "update",
args = { MappedStatement.class, Object.class }),
@Signature(
type = Executor.class,
method = "query",
args = { MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class })
})
public class LogExecutionTimeInterceptor implements Interceptor {
private static final Log log = LogFactory.getLog(LogExecutionTimeInterceptor.class);
private static final long THRESHOLD_NANOS = 0L;
@Override
public Object intercept(Invocation invocation) throws Throwable {
long before = System.nanoTime();
Object result = invocation.proceed();
long duration = System.nanoTime() - before;
if (duration > THRESHOLD_NANOS && log.isDebugEnabled()) {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
log.debug(MessageFormat.format(
"Statement {0} took {1} nano seconds", mappedStatement.getId(), duration));
}
return result;
}
}
To register the interceptor...
- with XML configuration
<plugins>
<plugin
interceptor="xxx.yyy.LogExecutionTimeInterceptor" />
</plugins>
@Bean
Interceptor getLogExecutionTimeInterceptor() {
return new LogExecutionTimeInterceptor();
}
答案2
得分: 0
你可以创建Interceptor接口的实现,并在配置文件中将其注册为插件。
英文:
You can create implementation of Interceptor interface and register it as a plugin in configuration file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论