使用aop记录数据库操作的执行时间
在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志。
首先我们可以写一个类实现MethodInterceptor接口:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; /**
* 自定义 AOP 处理时间类
*/
public class TimeHandler implements MethodInterceptor {
/**
* log
*/
private final static Log log = LogFactory.getLog(TimeHandler.class);
/**
* 对于执行时间超过指定毫秒,日志级别为error
* 单位:毫秒
* 默认值:200
*/
private int error = 200;
/**
* 对于执行时间超过指定毫秒,日志级别为warn
* 单位:毫秒
* 默认值:100
*/
private int warn = 100;
/**
* 对于执行时间超过指定毫秒,日志级别为info
* 单位:毫秒
* 默认值:50
*/
private int info = 50; /**
* Method invoke ...
*
* @param methodInvocation of type MethodInvocation
* @return Object
* @throws Throwable when
*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long procTime = System.currentTimeMillis();
try {
return methodInvocation.proceed();
}
finally {
procTime = System.currentTimeMillis() - procTime;
String msg = "Process method " + methodInvocation.getMethod().getName() + " successful! Total time: " + procTime + " milliseconds!";
if (procTime > error) {
if (log.isErrorEnabled()) log.error(msg);
} else if (procTime > warn) {
if (log.isWarnEnabled()) log.warn(msg);
} else if (procTime > info) {
if (log.isInfoEnabled()) log.info(msg);
} else {
if (log.isDebugEnabled()) log.debug(msg);
}
}
} /**
* Method setError sets the error of this TimeHandler object.
*
* @param error the error of this TimeHandler object.
*/
public void setError(int error) {
this.error = error;
} /**
* Method setWarn sets the warn of this TimeHandler object.
*
* @param warn the warn of this TimeHandler object.
*/
public void setWarn(int warn) {
this.warn = warn;
} /**
* Method setInfo sets the info of this TimeHandler object.
*
* @param info the info of this TimeHandler object.
*/
public void setInfo(int info) {
this.info = info;
}
}
然后我们可以在Spring中配置
<!-- Dao方法处理时间 -->
<bean id="daoTimeHandler" class="TimeHandler"/> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Dao</value>
</property>
<property name="interceptorNames">
<list>
<value>daoTimeHandler</value>
</list>
</property>
</bean>
以上在运行时就可以在每个*Dao执行后记录该操作的使用时间。
使用aop记录数据库操作的执行时间的更多相关文章
- 记录数据库操作记录的DDL触发器
我们在项目中经常会对数据做一些操作,比如增加一个字段,修改一个存储过程,删除表等等操作,很有必要记录这些操作,以便以后出了问题,方便找到元凶.接下来介绍一个DDL触发器在实际环境中的使用,这个DDL触 ...
- aop 记录用户操作(一)
转载: http://www.cnblogs.com/guokai870510826/p/5981015.html 使用标签来设置需要的记录 实例:@ISystemLog() @Controller ...
- 拦截并记录数据库操作-Logging and Intercepting Database Operations
原文:http://msdn.microsoft.com/zh-cn/data/dn469464 Logging and Intercepting Database Operations Starti ...
- Spring学习总结(16)——Spring AOP实现执行数据库操作前根据业务来动态切换数据源
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- mysql--->mysql查看数据库操作记录
mysql查看数据库操作记录 MySQL的查询日志记录了所有MySQL数据库请求的信息.无论这些请求是否得到了正确的执行.默认文件名为hostname.log.默认情况下MySQL查询日志是关闭的.生 ...
- 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(二) -- 多表查询
MySQL行(记录)的操作(二) -- 多表查询 数据的准备 #建表 create table department( id int, name varchar(20) ); create table ...
- 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(一)
MySQL的行(记录)的操作(一) 1. 增(insert) insert into 表名 value((字段1,字段2...); # 只能增加一行记录 insert into 表名 values(字 ...
- SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志
目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...
- EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
前言 Entity Framework 延伸系列目录 今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不 ...
随机推荐
- Java图片处理(一)图片合成
如何将多个头像合成类似QQ的群头像? 如上图所示,如何用java将单一的图片合成如上群头像. 在一个正方形外框中,要将多个图片合成上述图片.首先要做的是,依据圆相交的程度,计算圆心坐标与图片间空白区域 ...
- wpf随笔
因项目需要查找wpf.DataGrid的Binding方法, 由于其属于Dev框架体系内,偏向于winform并无Binding 1.且线程外更改UI控件还需要委托或者action,而Wpf控件仅需要 ...
- unicode 编码在线转换--javascript
// unicode 编码在线转换工具--javascript 本人在网上搜索,看到有使用javascript做unicode编码转换的,感觉很好玩,所以拿来使用的. 这个功能有目前测试了两种: 1) ...
- sc7731 Android 5.1 Camera 学习之二 framework 到 HAL接口整理
前面已经分析过,Client端发起远程调用,而实际完成处理任务的,是Server端的 CameraClient 实例.远程client 和 server是两个不同的进程,它们使用binder作为通信工 ...
- netcdf入门(转)
一.概述 NetCDF全称为network Common Data Format,中文译法为“网络通用数据格式”,对程序员来说,它和zip.jpeg.bmp文件格式类似,都是一种文件格式的标准.ne ...
- CSS Sprites图片处理
简介: CSS Sprites是一个网页图片处理方式,在国内都叫CSS精灵,css Sprites允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像 ...
- 网易新闻优化APK下载链接
你好,欢迎你的访问! 本次是APK包的下载链接: 点我下载 这是我目前实现的效果,正在完善中...
- asp.net 后台获取flv视频地址进行播放【转】
源码下载:http://download.csdn.net/detail/njxiaogui/7609687 前台:.aspx <table> <tr> <td>& ...
- 【JavaScript】理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- VS2012无法安装cocos2d-x-2.1.4 解决方法及VS2012新建coco2d-x项目(一)
转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 (注:此方法是可行,仅供参考,建议大家直接看我的 一见命令解决vs安装并创建c ...