当前位置: MXJAVA > JAVA开发 > 文章正文

java动态代理实现数据库事务拦截器

发表于 2008-06-15 | 阅读 117 views

  很多开源框架中都实现了拦截器功能,比如Spring、WebWork、Struts2等。例用拦截器,可以在对类方法进行调用的前后加入共通的操作,比如在调用方法的前后打印log等,使业务代码更加单纯。

  拦截器是利用java的动态代理技术来实现的。接下来我们就看一个例子,这个例子利用java动态代理,对所有insert、update、delete为前缀的方法进行事务处理。

  1. /** 
  2.  * grape mxjava.com.grape.core.interceptor.Interceptor.java 2008-6-15 
  3.  */ 
  4. package mxjava.com.grape.core.interceptor; 
  5.  
  6. import java.lang.reflect.InvocationHandler; 
  7. import java.lang.reflect.Proxy
  8.  
  9. /** 
  10.  * 拦截器 
  11.  * 
  12.  * @author hiswing 
  13.  */ 
  14. public abstract class Interceptor implements InvocationHandler { 
  15.     /** 被拦截的对象 */ 
  16.     protected Object obj; 
  17.  
  18.     /** 
  19.      * 动态生成一个代理类对象 
  20.      * @param obj 
  21.      * @return 代理类对象 
  22.      */ 
  23.     public Object bind(Object obj) { 
  24.         this.obj = obj; 
  25.  
  26.         Class cls = this.obj.getClass(); 
  27.         // 被代理类的ClassLoader 
  28.         // 要被代理的接口,本方法返回对象会自动声称实现了这些接口 
  29.         return Proxy.newProxyInstance(cls.getClassLoader(), 
  30.                 cls.getInterfaces(), this); 
  31.     } 
  32.  
  33. /** 
  34.  * grape mxjava.com.grape.core.interceptor.TransactionDynamicProxy.java 
  35.  * 2008-6-15 
  36.  */ 
  37. package mxjava.com.grape.core.interceptor; 
  38.  
  39. import java.lang.reflect.Method; 
  40.  
  41. import mxjava.com.grape.core.AppContext; 
  42. import mxjava.com.grape.core.db.Connection; 
  43. import mxjava.com.grape.core.db.DBException; 
  44.  
  45. /** 
  46.  * 数据库事务拦截器 
  47.  * @author hiswing 
  48.  */ 
  49. public class TransactionInterceptor extends Interceptor { 
  50.  
  51.     /** 数据库链接 */ 
  52.     private Connection connect; 
  53.  
  54.     private String[] method; 
  55.  
  56.     /** 
  57.      * 初期化数据库链接 
  58.      */ 
  59.     public TransactionInterceptor() { 
  60.         this.connect = getConnection(); 
  61.         this.method = new String[] {"insert""update""delete"}; 
  62.     } 
  63.  
  64.     /** 
  65.      * 代理要调用的方法,并在方法调用前后调用连接器的方法 
  66.      * 
  67.      * @param proxy 代理对象 
  68.      * @param method 被代理的接口方法 
  69.      * @param args 被代理接口方法的参数 
  70.      * @return 方法调用返回的结果 
  71.      * @throws Throwable 
  72.      */ 
  73.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
  74.         Object result = null
  75.         boolean flag = this.checkName(method.getName()); 
  76.         try { 
  77.             if (flag) { 
  78.                 this.connect.begin(); 
  79.                 result = method.invoke(super.obj, args); 
  80.                 this.connect.commit(); 
  81.             } else { 
  82.                 result = method.invoke(super.obj, args); 
  83.             } 
  84.         } catch (DBException ex) { 
  85.             if (flag) { 
  86.                 this.connect.rollBack(); 
  87.             } 
  88.             throw ex; 
  89.         } 
  90.  
  91.         return result; 
  92.     } 
  93.  
  94.     /** 
  95.      * 检查是否需要进行事务处理 
  96.      * @param methodName 
  97.      * @return 
  98.      */ 
  99.     private boolean checkName(String methodName) { 
  100.         if (this.method == null || this.method.length == 0) { 
  101.             return false
  102.         } 
  103.  
  104.         for (String mn : this.method) { 
  105.             if (!"".equals(mn.trim()) && methodName.indexOf(mn) == 0) { 
  106.                 return true
  107.             } 
  108.         } 
  109.         return false
  110.     } 

 

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网 关键字: ,
喜欢MXJAVA的文章,那就通过 RSS Feed 功能订阅阅读吧!

我要评论

*

* 绝不会泄露



返回首页 | 关于我们 | 联系我们 | 广告合作 | 友情链接 | 版权声明