首页>代码>spring boot+jfinal的ActiveRecordPlugin重新整合林业厅会议系统>/bf/src/main/java/com/example/bf/config/JFinalTxAop.java
package com.example.bf.config; import com.jfinal.kit.LogKit; import com.jfinal.plugin.activerecord.ActiveRecordException; import com.jfinal.plugin.activerecord.Config; import com.jfinal.plugin.activerecord.DbKit; import com.jfinal.plugin.activerecord.NestedTransactionHelpException; import com.jfinal.plugin.activerecord.tx.TxConfig; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; /** * @author wgw * @date 2018/4/13 */ @Aspect @Component public class JFinalTxAop { /** * 自定义JFinal 事物注解 * value中的意思解释 * * @annotation 表示注解只能支持方法上 * @within 表示注解在类下面所有的方法 , 暂时不使用这种方式 */ @Pointcut("@annotation(com.example.bf.config.JFinalTx)") private void method () { } @Around(value = "method()", argNames = "pjp") public Object doAround ( ProceedingJoinPoint pjp ) throws Throwable { Object retVal = null; Config config = getConfigWithTxConfig( pjp ); if (config == null) config = DbKit.getConfig(); Connection conn = config.getThreadLocalConnection(); // Nested transaction support if (conn != null) { try { if (conn.getTransactionIsolation() < getTransactionLevel( config )) conn.setTransactionIsolation( getTransactionLevel( config ) ); retVal = pjp.proceed(); return retVal; } catch (SQLException e) { throw new ActiveRecordException( e ); } } Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); config.setThreadLocalConnection( conn ); conn.setTransactionIsolation( getTransactionLevel( config ) );// conn.setTransactionIsolation(transactionLevel); conn.setAutoCommit( false ); retVal = pjp.proceed(); conn.commit(); } catch (NestedTransactionHelpException e) { if (conn != null) try { conn.rollback(); } catch (Exception e1) { LogKit.error( e1.getMessage(), e1 ); } LogKit.logNothing( e ); } catch (Throwable t) { if (conn != null) try { conn.rollback(); } catch (Exception e1) { LogKit.error( e1.getMessage(), e1 ); } throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException( t ); } finally { try { if (conn != null) { if (autoCommit != null) conn.setAutoCommit( autoCommit ); conn.close(); } } catch (Throwable t) { // can not throw exception here, otherwise the more important exception in previous catch block can not be thrown LogKit.error( t.getMessage(), t ); } finally { // prevent memory leak config.removeThreadLocalConnection(); } } return retVal; } /** * 获取配置的事务级别 * * @param config * @return */ protected int getTransactionLevel ( Config config ) { return config.getTransactionLevel(); } /** * @param pjp * @return Config */ public static Config getConfigWithTxConfig ( ProceedingJoinPoint pjp ) { MethodSignature ms = (MethodSignature) pjp.getSignature(); Method method = ms.getMethod(); TxConfig txConfig = method.getAnnotation( TxConfig.class ); if (txConfig == null) txConfig = pjp.getTarget().getClass().getAnnotation( TxConfig.class ); if (txConfig != null) { Config config = DbKit.getConfig( txConfig.value() ); if (config == null) throw new RuntimeException( "Config not found with TxConfig: " + txConfig.value() ); return config; } return null; } }
最近下载更多
wy4525 LV6
2022年8月12日
zhaoyangwfd LV17
2022年3月31日
最呆萌呀 LV7
2022年3月13日
wanglinddad LV54
2021年12月25日
893213895 LV18
2021年12月17日
敏敏 LV1
2021年12月9日
最代码-宋家辉 LV61
2021年10月6日
icesolitude LV8
2021年7月7日
哆啦A梦伴我同行 LV10
2021年6月17日
wmm966520 LV5
2021年6月3日
最近浏览更多
admin_z LV22
1月28日
lilong007 LV20
2023年12月30日
漫步的海星 LV4
2023年9月21日
微信网友_6658803495538688
2023年9月21日
暂无贡献等级
yshysh123
2023年9月20日
暂无贡献等级
泓鼎168 LV19
2023年8月31日
宇宙最强帅哥 LV1
2023年6月29日
neuwxr2015 LV8
2023年6月13日
skook7 LV2
2023年6月1日
微信网友_6461019441418240 LV2
2023年5月6日