TransManager

  1. final
  2. public class TransManager {
  3.     
  4.     private final static Log log = LogUtil.getLog(TransManager.class);
  5.     
  6.     private final static ThreadLocal<Map<String,Object>> cache = new ThreadLocal<Map<String,Object>>(){
  7.         @Override
  8.         protected Map<String,Object> initialValue() {
  9.             return new HashMap<String,Object>();
  10.         }
  11.     };
  12.     
  13.     
  14.     //--------------------------------------
  15.     
  16.     private static List<TransListener> listenerLst = new CopyOnWriteArrayList<TransListener>();
  17.     
  18.     public static void addListener(TransListener listener){
  19.         if(listener == null){
  20.             throw new java.lang.NullPointerException();
  21.         }
  22.         listenerLst.add(listener);
  23.     }
  24.     
  25.     public static void remove(TransListener listener){
  26.         if(listener == null){
  27.             throw new java.lang.NullPointerException();
  28.         }
  29.         listenerLst.remove(listener);
  30.     }
  31.     
  32.     //--------------------------------------
  33.     
  34.     public static <T> T wrap(Class<T> clz) {
  35.         return wrap(clz, false , true);
  36.     }
  37.     
  38.     public static <T> T wrap(Class<T> clz , boolean isUseCache) {
  39.         return wrap(clz, false , isUseCache);
  40.     }
  41.     
  42.     public static <T> T wrap(Class<T> clz , boolean isReadOnly , boolean isUseCache) {
  43.         return wrap(clz, DbSessionFactory.MAIN , isReadOnly , isUseCache);
  44.     }
  45.     
  46.     
  47.     @SuppressWarnings("unchecked")
  48.     public static <T> T wrap(Class<T> clz , int dbtype , boolean isReadOnly , boolean isUseCache) {
  49.         
  50.         if(!isUseCache){
  51.             return makeObj(clz,dbtype,isReadOnly);
  52.         }else{
  53.         
  54.             String key = clz.getCanonicalName()+"_"+dbtype+"_"+isReadOnly;
  55.             
  56.             T obj = (T)cache.get().get(key);
  57.             
  58.             if(obj == null){
  59.                 obj = makeObj(clz,dbtype,isReadOnly);
  60.                 cache.get().put(key,obj);
  61.             }
  62.             
  63.             return obj;
  64.         }
  65.     }
  66.     
  67.     
  68.     @SuppressWarnings("unchecked")
  69.     private static <T> T makeObj(Class<T> clz,final int dbtype , final boolean isReadOnly){
  70.         return (T)Enhancer.create(clz, new MethodInterceptor(){
  71.             public Object intercept(Object obj, Method method, Object[] arg,MethodProxy proxy) throws Throwable {
  72.                 return execute(dbtype, isReadOnly, obj, null , proxy , arg);
  73.             }
  74.         });
  75.     }
  76.     
  77.     
  78.     //---------------------------------
  79.     
  80.     public static <T> T doTransaction(Transaction<T> t) throws Exception {
  81.         return doTransaction(t , DbSessionFactory.MAIN , false);
  82.     }
  83.     
  84.     
  85.     private static Method exeMethod ;
  86.     static{
  87.         try {
  88.             exeMethod = Transaction.class.getMethod("execute");
  89.         } catch (Exception e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.     @SuppressWarnings("unchecked")
  94.     public static <T> T doTransaction(Transaction<T> t , int dbtype , boolean isReadOnly) throws Exception {
  95.         return (T)execute(dbtype, isReadOnly, t, exeMethod , null , null);
  96.     }
  97.     
  98.     
  99.     //----------------------------------
  100.     
  101.     private static Object execute(int dbtype , boolean isReadOnly , Object obj , Method method ,MethodProxy proxy,Object[] args) throws Exception {
  102.         
  103.         try{
  104.             
  105.             begin(dbtype,isReadOnly);  
  106.             
  107.             Object res = method==null? proxy.invokeSuper(obj,args) : method.invoke(obj, args);
  108.             
  109.             commit(dbtype,isReadOnly);
  110.             
  111.             return res;
  112.             
  113.         } catch (Throwable dx) {
  114.             rollback(dbtype,isReadOnly);
  115.             throw new Exception(dx);
  116.         }
  117.     }
  118.     
  119.     private static void begin(int dbtype , boolean isReadOnly) throws Exception{
  120.         
  121.         for(TransListener listerner : listenerLst ){
  122.             try{
  123.                 listerner.begin(dbtype, isReadOnly);
  124.             }catch(Exception dx){
  125.                 log.error("TransListener begin err!" , dx);
  126.             }
  127.         }
  128.         
  129.         if(isReadOnly){
  130.             DbSessionFactory.beginReadOnlyTransaction(dbtype);
  131.         }else{
  132.             DbSessionFactory.beginTransaction(dbtype);
  133.         }
  134.         
  135.     }
  136.     
  137.     private static void commit(int dbtype , boolean isReadOnly) throws Exception{
  138.         
  139.         if(isReadOnly){
  140.             DbSessionFactory.commitReadOnlyTransaction(dbtype);
  141.         }else{
  142.             DbSessionFactory.commitTransaction(dbtype);
  143.         }
  144.         
  145.         for(TransListener listerner : listenerLst ){
  146.             try{
  147.                 listerner.commit(dbtype, isReadOnly);
  148.             }catch(Exception dx){
  149.                 log.error("TransListener commit err!" , dx);
  150.             }
  151.         }
  152.     }
  153.     private static void rollback(int dbtype , boolean isReadOnly) throws Exception{
  154.         
  155.         if(isReadOnly){
  156.             DbSessionFactory.rollbackReadOnlyTransaction(dbtype);
  157.         }else{
  158.             DbSessionFactory.rollbackTransaction(dbtype);
  159.         }
  160.         
  161.         for(TransListener listerner : listenerLst ){
  162.             try{
  163.                 listerner.rollback(dbtype, isReadOnly);
  164.             }catch(Exception dx){
  165.                 log.error("TransListener rollback err!" , dx);
  166.             }
  167.         }
  168.     }
  169.     
  170.     
  171.     //------------------------------------
  172.     
  173.     public interface TransListener {
  174.         
  175.         public void begin(int dbtype , boolean isReadOnly);
  176.         
  177.         public void commit(int dbtype , boolean isReadOnly);
  178.         
  179.         public void rollback(int dbtype , boolean isReadOnly);
  180.         
  181.     }
  182.     
  183.     
  184.     //------------------------------------
  185.     
  186.     
  187.     public interface Transaction<T> {
  188.         public T execute() throws Exception;
  189.     }
  190. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值