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

  首先我们可以写一个类实现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. Linux下修改hostname

    我维护两三个机房的数十台机器,开发用机器,运营用机器,自己工作机器也是ubuntu,有时开很多ssh,干的还是同样的事情,很容易搞混.所以需要一目了然的知道某台机器的情况,避免犯晕.这就需要修改主机名 ...

  2. ActiveMQ简介与安装

    开源消息总线 支持JMS1.1和J2EE 1.4规范的 JMS Provider实现(持久化,XA消息,事务) 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去 支持 ...

  3. [转][IIS]发布网站,提示用户 'IIS APPPOOL\***' 登录失败。

    链接:http://www.cnblogs.com/tianguook/p/3881075.html 用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中 ...

  4. WSO2 ESB

    什么是WSO2 ESB? WSO2 ESB是一个轻量级的易于使用的企业服务资源总线.WSO2 ESB允许系统管理员和SOA架构师,消息路由,虚拟化,中介,转换,日志记录,任务调度,负载平衡,失败了路由 ...

  5. $(document).ready()与 window.onload执行时机

    $(document).ready()方法和window.onload方法有相似的功能,但是在执行时机方面是有区别的.window.onload方法是子啊网页中的所有元素(包括元素的所有关联的文件)完 ...

  6. 初步认识pg_control文件之一

    这个据说是PostgreSQL的control file. 到底如何呢,先看看改名后如何,把pg_control文件改名,然后启动 Postgres,运行时得到信息: [postgres@pg101 ...

  7. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  8. Codeforces Round #188 (Div. 2) A. Even Odds 水题

    A. Even Odds Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/318/problem/ ...

  9. .net+mssql制作抽奖程序思路及源码

    近期一直在研究数据库,刚好有个项目要做抽奖程序,恩,拿来练练手吧. 抽奖程序: 思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽 ...

  10. GLSL-几何着色器详解跟实例(GS:Geometry Shader)[转]

    [OpenGL4.0]GLSL-几何着色器详解和实例(GS:Geometry Shader) 一.什么是几何着色器(GS:Geometry Shader) Input Assembler(IA)从顶点 ...