Explorar el Código

修复RoutingDataSource+Atomikos 下嵌套数据源超时的问题

NorthLan hace 7 años
padre
commit
7e3851083e

+ 3 - 2
zen-orm/src/main/kotlin/com/gxzc/zen/orm/DynamicMultipleDataSource.kt

@@ -30,8 +30,9 @@ class DynamicMultipleDataSource : AbstractRoutingDataSource() {
     }
 
     override fun determineCurrentLookupKey(): Any {
+        if (DATA_SOURCE_KEY.get() == null) {
+            return DSKey.DSKEY_SYS
+        }
         return DATA_SOURCE_KEY.get()
-//        clear()
-//        return lookupKey
     }
 }

+ 5 - 5
zen-orm/src/main/kotlin/com/gxzc/zen/orm/aop/DataSourceSwitchAspect.kt

@@ -34,9 +34,9 @@ class DataSourceSwitchAspect {
 
     private var isAnnotationAspect = ThreadLocal<Boolean>().apply { set(false) }
 
-//    @Pointcut("execution(* com.gxzc.zen.api..*Service.*(..))")
-//    fun zenServicePointCut() {
-//    }
+    @Pointcut("execution(* com.gxzc.zen.api..*Service.*(..))")
+    fun zenServicePointCut() {
+    }
 
     @Pointcut("execution(* com.gxzc.zen.api..*Mapper.*(..))")
     fun zenMapperPointCut() {
@@ -84,8 +84,8 @@ class DataSourceSwitchAspect {
 
             clazz.`package`.name
         } catch (e: Exception) {
-            logger.debug("get packageName of MapperProxy error, ignore if nothing happened!")
-            logger.debug("set packageName of target...")
+            logger.trace("get packageName of MapperProxy error, ignore if nothing happened!")
+            logger.trace("set packageName of target...")
             target::class.java.`package`.name
         }
 

+ 14 - 0
zen-orm/src/main/kotlin/com/gxzc/zen/orm/aop/MultiTransactionAspect.kt

@@ -11,6 +11,7 @@ import org.aspectj.lang.reflect.MethodSignature
 import org.slf4j.LoggerFactory
 import org.springframework.stereotype.Component
 import org.springframework.transaction.jta.JtaTransactionManager
+import javax.transaction.Status
 import kotlin.reflect.KClass
 
 /**
@@ -27,6 +28,8 @@ class MultiTransactionAspect {
         private val logger = LoggerFactory.getLogger(MultiTransactionAspect::class.java)
     }
 
+//    private val counter: AtomicInteger = AtomicInteger(0)
+
     init {
         logger.info("MultiTransactionAspect initializing...")
     }
@@ -36,6 +39,9 @@ class MultiTransactionAspect {
      */
     @Around("@annotation(com.gxzc.zen.orm.annotation.ZenTransactional)")
     fun multiTransactionAround(joinPoint: ProceedingJoinPoint): Any? {
+//        if (counter.get() > 0) {
+//            return joinPoint.proceed()
+//        }
         logger.trace("@ZenTransactional aspect...")
         val methodName = joinPoint.signature.name
         val parameterTypes = (joinPoint.signature as MethodSignature).method.parameterTypes
@@ -45,8 +51,16 @@ class MultiTransactionAspect {
             val multiTransaction = method.getAnnotation(ZenTransactional::class.java)
             val transactionManager = SpringContextHolder.getBean(JtaTransactionManager::class.java)
             val userTransaction = transactionManager.userTransaction
+
+            // 防止嵌套事务 多次拦截
+            if (userTransaction.status == Status.STATUS_ACTIVE) {
+                return joinPoint.proceed()
+            }
+//            logger.info(userTransaction.status.toString())
             return try {
                 userTransaction.begin()
+//                counter.addAndGet(1)
+//                logger.info(userTransaction.status.toString())
                 val r = joinPoint.proceed()
                 userTransaction.commit()
                 r

+ 37 - 0
zen-orm/src/main/kotlin/com/gxzc/zen/orm/config/AtomikosTransactionConfig.kt

@@ -0,0 +1,37 @@
+package com.gxzc.zen.orm.config
+
+import com.atomikos.icatch.jta.UserTransactionImp
+import com.atomikos.icatch.jta.UserTransactionManager
+import com.gxzc.zen.orm.DynamicMultipleDataSource
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
+import org.springframework.context.annotation.Bean
+import org.springframework.context.annotation.Configuration
+import org.springframework.core.annotation.Order
+import org.springframework.jdbc.datasource.DataSourceTransactionManager
+import org.springframework.transaction.annotation.EnableTransactionManagement
+import org.springframework.transaction.jta.JtaTransactionManager
+
+/**
+ *
+ * @author NorthLan
+ * @date 2018/4/2
+ * @url https://noahlan.com
+ */
+@EnableTransactionManagement
+@Configuration
+class AtomikosTransactionConfig {
+    @Bean
+    fun jtaTransactionManager(): JtaTransactionManager {
+        return JtaTransactionManager(
+                UserTransactionImp().apply { setTransactionTimeout(3000) },
+                UserTransactionManager().apply { forceShutdown = true }).apply {
+            setAllowCustomIsolationLevels(true)
+        }
+    }
+
+//    @Bean
+//    @ConditionalOnProperty(prefix = "orm", name = ["multi-datasource-enable"], havingValue = "true", matchIfMissing = true)
+//    fun dataSourceTransactionManager(dataSource: DynamicMultipleDataSource): DataSourceTransactionManager {
+//        return DataSourceTransactionManager(dataSource)
+//    }
+}

+ 0 - 13
zen-orm/src/main/kotlin/com/gxzc/zen/orm/config/MybatisPlusConfig.kt

@@ -1,13 +1,9 @@
 package com.gxzc.zen.orm.config
 
-import com.baomidou.mybatisplus.mapper.LogicSqlInjector
 import com.baomidou.mybatisplus.plugins.PaginationInterceptor
-import com.baomidou.mybatisplus.plugins.PerformanceInterceptor
 import org.mybatis.spring.annotation.MapperScan
 import org.springframework.context.annotation.Bean
 import org.springframework.context.annotation.Configuration
-import org.springframework.context.annotation.Profile
-import org.springframework.transaction.annotation.EnableTransactionManagement
 
 /**
  *
@@ -15,7 +11,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement
  */
 @Configuration
 @MapperScan("com.gxzc.zen.api.*.mapper*")
-@EnableTransactionManagement
 class MybatisPlusConfig {
 
 //    /**
@@ -34,12 +29,4 @@ class MybatisPlusConfig {
     fun paginationInterceptor(): PaginationInterceptor {
         return PaginationInterceptor()
     }
-//
-//    /**
-//     * 逻辑删插件
-//     */
-//    @Bean
-//    fun logicSqlInjector(): LogicSqlInjector {
-//        return LogicSqlInjector()
-//    }
 }

+ 5 - 1
zen-orm/src/main/kotlin/com/gxzc/zen/orm/config/MybatisPlusFactoryConfig.kt

@@ -84,7 +84,7 @@ class MybatisPlusFactoryConfig {
         return setFactoryBean(dataSource)
     }
 
-    private fun setFactoryBean(dataSource: DataSource): MybatisSqlSessionFactoryBean {
+    private fun setFactoryBean(dataSource: DataSource/*, mapperLocation: String?*/): MybatisSqlSessionFactoryBean {
         return MybatisSqlSessionFactoryBean().apply {
             setDataSource(dataSource)
             vfs = SpringBootVFS::class.java
@@ -124,6 +124,10 @@ class MybatisPlusFactoryConfig {
             if (!ObjectUtils.isEmpty(properties?.resolveMapperLocations())) {
                 setMapperLocations(properties?.resolveMapperLocations())
             }
+//            if (StringUtils.hasLength(mapperLocation)) {
+//                setMapperLocations(PathMatchingResourcePatternResolver().getResources(mapperLocation))
+//            }
+
             if (!ObjectUtils.isEmpty(properties?.globalConfig)) {
                 setGlobalConfig(properties?.globalConfig?.convertGlobalConfiguration())
             }