在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志。

  首先我们可以写一个类实现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记录数据库操作的执行时间的更多相关文章

  1. 记录数据库操作记录的DDL触发器

    我们在项目中经常会对数据做一些操作,比如增加一个字段,修改一个存储过程,删除表等等操作,很有必要记录这些操作,以便以后出了问题,方便找到元凶.接下来介绍一个DDL触发器在实际环境中的使用,这个DDL触 ...

  2. aop 记录用户操作(一)

    转载: http://www.cnblogs.com/guokai870510826/p/5981015.html 使用标签来设置需要的记录 实例:@ISystemLog() @Controller ...

  3. 拦截并记录数据库操作-Logging and Intercepting Database Operations

    原文:http://msdn.microsoft.com/zh-cn/data/dn469464 Logging and Intercepting Database Operations Starti ...

  4. Spring学习总结(16)——Spring AOP实现执行数据库操作前根据业务来动态切换数据源

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  5. mysql--->mysql查看数据库操作记录

    mysql查看数据库操作记录 MySQL的查询日志记录了所有MySQL数据库请求的信息.无论这些请求是否得到了正确的执行.默认文件名为hostname.log.默认情况下MySQL查询日志是关闭的.生 ...

  6. 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(二) -- 多表查询

    MySQL行(记录)的操作(二) -- 多表查询 数据的准备 #建表 create table department( id int, name varchar(20) ); create table ...

  7. 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(一)

    MySQL的行(记录)的操作(一) 1. 增(insert) insert into 表名 value((字段1,字段2...); # 只能增加一行记录 insert into 表名 values(字 ...

  8. SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  9. EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)

    前言 Entity Framework 延伸系列目录 今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不 ...

随机推荐

  1. ASP.NET服务器控件在IE10浏览器(非兼容模式)下报脚本错误的可能解决办法

    关于IE10出现LinkButton点击无效的情况:        一般高配置的系统如Win7旗舰版SP1系统不会出现这种情况,针对家庭普通版和专业版的用户通过测试都有这种情况,对于开发人员要解决不同 ...

  2. Jquery 提示

    1  文字提示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  3. JS获取URL参数 方法

    function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = qu ...

  4. cocos2dx中加入unzip

    作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037323.html  cocos2dx中没有直接解压文件的,自己网上找了个,记录一下. ...

  5. c# 轻量级 ORM 框架 之 Model解析 (四)

    关于orm框架设计,还有必要说的或许就是Model解析了,也是重要的一个环节,在实现上还是相对比较简单的. Model解析,主要用到的技术是反射了,即:把类的属性与表的字段做映射. 把自己的设计及实现 ...

  6. coco2d学习day01 精灵分析

    day01笔记 using namespace cocos2d; == USING_NS_CC; 一般用后面的 #ifndef __MYLAYER_H__ #define __MYLAYER_H__ ...

  7. 丢失全部控制文件,noresetlogs重建控制文件,alter database open

    測试2: (1)一致性的全备 SQL> shutdown immediate; $ cp -rf $ORACLE_BASE/oradata/boss/*.dbf /oradata/bossbak ...

  8. sqlserver 各种判断是否存在(表名、函数、存储过程等)

    库是否存在 if exists(select * from master..sysdatabases where name=N'库名') print 'exists'elseprint 'not ex ...

  9. cocos2dx实例开发之flappybird(入门版)

    cocos2dx社区里有个系列博客完整地复制原版flappybird的全部特性.只是那个代码写得比較复杂,新手学习起来有点捉摸不透,这里我写了个简单的版本号.演演示样例如以下: watermark/2 ...

  10. mysql官方示例数据库

    employees数据库:http://ari.iteye.com/blog/1066690  https://launchpad.net/test-db/employees-db-1/1.0.6