JFinal ActiveRecordPlugin插件事物交给Spring管理

思路:使用spring AOP代理

这里使用springboot来实现,spring同理

1、maven 依赖

2、JFinalTxAop

  1. package com.zgxl.common.aop;
  2.  
  3. import com.jfinal.kit.LogKit;
  4. import com.jfinal.plugin.activerecord.ActiveRecordException;
  5. import com.jfinal.plugin.activerecord.Config;
  6. import com.jfinal.plugin.activerecord.DbKit;
  7. import com.jfinal.plugin.activerecord.NestedTransactionHelpException;
  8. import com.jfinal.plugin.activerecord.tx.TxConfig;
  9. import org.aspectj.lang.ProceedingJoinPoint;
  10. import org.aspectj.lang.annotation.Around;
  11. import org.aspectj.lang.annotation.Aspect;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.aspectj.lang.reflect.MethodSignature;
  14. import org.springframework.stereotype.Component;
  15.  
  16. import java.lang.reflect.Method;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19.  
  20. /**
  21.  * @author choxsu
  22.  * @date 2018/4/13
  23.  */
  24. @Aspect
  25. @Component
  26. public class JFinalTxAop {
  27.  
  28.  
  29.     /**
  30.      * 自定义JFinal 事物注解
  31.      * value中的意思解释
  32.      *
  33.      * @annotation 表示注解只能支持方法上
  34.      * @within 表示注解在类下面所有的方法 , 暂时不使用这种方式
  35.      */
  36.     @Pointcut("@annotation(com.zgxl.common.aop.JFinalTx)")
  37.     private void method() {
  38.     }
  39.  
  40.     @Around(value = "method()", argNames = "pjp")
  41.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  42.         Object retVal = null;
  43.         Config config = getConfigWithTxConfig(pjp);
  44.         if (config == null)
  45.             config = DbKit.getConfig();
  46.  
  47.         Connection conn = config.getThreadLocalConnection();
  48.         // Nested transaction support
  49.         if (conn != null) {
  50.             try {
  51.                 if (conn.getTransactionIsolation() < getTransactionLevel(config))
  52.                     conn.setTransactionIsolation(getTransactionLevel(config));
  53.                 retVal = pjp.proceed();
  54.                 return retVal;
  55.             } catch (SQLException e) {
  56.                 throw new ActiveRecordException(e);
  57.             }
  58.         }
  59.  
  60.         Boolean autoCommit = null;
  61.         try {
  62.             conn = config.getConnection();
  63.             autoCommit = conn.getAutoCommit();
  64.             config.setThreadLocalConnection(conn);
  65.             conn.setTransactionIsolation(getTransactionLevel(config));// conn.setTransactionIsolation(transactionLevel);
  66.  
  67.             conn.setAutoCommit(false);
  68.             retVal = pjp.proceed();
  69.             conn.commit();
  70.         } catch (NestedTransactionHelpException e) {
  71.             if (conn != null) try {
  72.                 conn.rollback();
  73.             } catch (Exception e1) {
  74.                 LogKit.error(e1.getMessage(), e1);
  75.             }
  76.             LogKit.logNothing(e);
  77.         } catch (Throwable t) {
  78.             if (conn != null) try {
  79.                 conn.rollback();
  80.             } catch (Exception e1) {
  81.                 LogKit.error(e1.getMessage(), e1);
  82.             }
  83.             throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException(t);
  84.         } finally {
  85.             try {
  86.                 if (conn != null) {
  87.                     if (autoCommit != null)
  88.                         conn.setAutoCommit(autoCommit);
  89.                     conn.close();
  90.                 }
  91.             } catch (Throwable t) {
  92.                 // can not throw exception here, otherwise the more important exception in previous catch block can not be thrown
  93.                 LogKit.error(t.getMessage(), t);
  94.             } finally {
  95.                 // prevent memory leak
  96.                 config.removeThreadLocalConnection();
  97.             }
  98.         }
  99.         return retVal;
  100.     }
  101.  
  102.     /**
  103.      * 获取配置的事务级别
  104.      *
  105.      * @param config
  106.      * @return
  107.      */
  108.     protected int getTransactionLevel(Config config) {
  109.         return config.getTransactionLevel();
  110.     }
  111.  
  112.     /**
  113.      * @param pjp
  114.      * @return Config
  115.      */
  116.     public static Config getConfigWithTxConfig(ProceedingJoinPoint pjp) {
  117.         MethodSignature ms = (MethodSignature) pjp.getSignature();
  118.         Method method = ms.getMethod();
  119.         TxConfig txConfig = method.getAnnotation(TxConfig.class);
  120.         if (txConfig == null)
  121.             txConfig = pjp.getTarget().getClass().getAnnotation(TxConfig.class);
  122.  
  123.         if (txConfig != null) {
  124.             Config config = DbKit.getConfig(txConfig.value());
  125.             if (config == null)
  126.                 throw new RuntimeException("Config not found with TxConfig: " + txConfig.value());
  127.             return config;
  128.         }
  129.         return null;
  130.     }
  131. }

3、JFinalTx

  1. package com.zgxl.common.aop;
  2.  
  3. /**
  4.  * @author choxsu
  5.  */
  6.  
  7. import java.lang.annotation.*;
  8.  
  9. /**
  10.  * Jfinal事物交给spring管理注解
  11.  * 目前只支持用在方法上
  12.  */
  13. @Inherited
  14. @Target({ElementType.METHOD})
  15. @Retention(RetentionPolicy.RUNTIME)
  16. public @interface JFinalTx {
  17.  
  18. }

4、使用

TestController

  1. package com.choxsu.elastic.controller;
  2. import com.choxsu.elastic.service.TestService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8.  * @author choxsu
  9.  */
  10. @RestController
  11. @RequestMapping(value = {"/test/v1"})
  12. public class TestController {    
  13.  
  14.      @Autowired
  15.      private TestService testService;    
  16.      
  17.      @GetMapping(value = "/testTran")    
  18.      
  19.      public Object testTran(){        
  20.          return testService.testTran();
  21.      }
  22. }


TestService
 
  1. package com.choxsu.elastic.service;
  2. import com.choxsu.elastic.config.JFinalTx;
  3. import com.jfinal.kit.Ret;import com.jfinal.plugin.activerecord.Db;
  4. import com.jfinal.plugin.activerecord.Record;
  5. import org.springframework.stereotype.Service;
  6. /**
  7.  * @author choxsu
  8.  */
  9.  @Service
  10.  public class TestService {    
  11.  
  12.      /**
  13.      * 事物测试
  14.      *
  15.      * @return
  16.      */
  17.     @JFinalTx
  18.     public Object testTran() {
  19.         Record record = new Record();
  20.         record.set("id", 10);
  21.         Db.save("test", record);        
  22.         if (true) {            
  23.             throw new RuntimeException("test");
  24.         }        
  25.         return Ret.by("msg", "success");
  26.     }
  27. }


本文转载至:http://www.jfinal.com/share/825 ,感谢作者的分享

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值