|
@@ -1,54 +1,57 @@
|
|
|
package com.gxzc.zen.orm.aop
|
|
|
|
|
|
+import com.gxzc.zen.common.exception.ZenException
|
|
|
+import com.gxzc.zen.common.exception.ZenExceptionEnum
|
|
|
import com.gxzc.zen.common.util.SpringContextHolder
|
|
|
-import com.gxzc.zen.orm.annotation.MultiTransactional
|
|
|
+import com.gxzc.zen.orm.annotation.ZenTransactional
|
|
|
import org.aspectj.lang.ProceedingJoinPoint
|
|
|
import org.aspectj.lang.annotation.Around
|
|
|
import org.aspectj.lang.annotation.Aspect
|
|
|
import org.aspectj.lang.reflect.MethodSignature
|
|
|
import org.slf4j.LoggerFactory
|
|
|
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
|
|
|
import org.springframework.stereotype.Component
|
|
|
import org.springframework.transaction.jta.JtaTransactionManager
|
|
|
import kotlin.reflect.KClass
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ * 一致性事务 切面
|
|
|
+ * 非多数据源时也可使用
|
|
|
* @author NorthLan at 2018/2/3
|
|
|
*/
|
|
|
@Aspect
|
|
|
@Suppress("unused")
|
|
|
@Component
|
|
|
-@ConditionalOnProperty(prefix = "orm", name = ["multi-datasource-enable"], havingValue = "true", matchIfMissing = true)
|
|
|
-class MultiDataSourceAspect {
|
|
|
+//@ConditionalOnProperty(prefix = "orm", name = ["multi-datasource-enable"], havingValue = "true", matchIfMissing = true)
|
|
|
+class MultiTransactionAspect {
|
|
|
companion object {
|
|
|
- private val logger = LoggerFactory.getLogger(MultiDataSourceAspect::class.java)
|
|
|
+ private val logger = LoggerFactory.getLogger(MultiTransactionAspect::class.java)
|
|
|
}
|
|
|
|
|
|
init {
|
|
|
- logger.info("MultiDataSourceAspect initializing...")
|
|
|
+ logger.info("MultiTransactionAspect initializing...")
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 一致性事务切面解决方案
|
|
|
*/
|
|
|
- @Around("@annotation(com.gxzc.zen.orm.annotation.MultiTransactional)")
|
|
|
+ @Around("@annotation(com.gxzc.zen.orm.annotation.ZenTransactional)")
|
|
|
fun multiTransactionAround(joinPoint: ProceedingJoinPoint): Any? {
|
|
|
- logger.info("@MultiTransactional aspect...")
|
|
|
+ logger.info("@ZenTransactional aspect...")
|
|
|
val methodName = joinPoint.signature.name
|
|
|
val parameterTypes = (joinPoint.signature as MethodSignature).method.parameterTypes
|
|
|
val method = joinPoint.target::class.java.getMethod(methodName, *parameterTypes)
|
|
|
|
|
|
- if (method.isAnnotationPresent(MultiTransactional::class.java)) {
|
|
|
- val multiTransaction = method.getAnnotation(MultiTransactional::class.java)
|
|
|
+ if (method.isAnnotationPresent(ZenTransactional::class.java)) {
|
|
|
+ val multiTransaction = method.getAnnotation(ZenTransactional::class.java)
|
|
|
val transactionManager = SpringContextHolder.getBean(JtaTransactionManager::class.java)
|
|
|
val userTransaction = transactionManager.userTransaction
|
|
|
return try {
|
|
|
- userTransaction?.begin()
|
|
|
+ userTransaction.begin()
|
|
|
val r = joinPoint.proceed()
|
|
|
- userTransaction?.commit()
|
|
|
- return r
|
|
|
+ userTransaction.commit()
|
|
|
+ r
|
|
|
} catch (e: Throwable) {
|
|
|
+ logger.error("error: ", e)
|
|
|
val rollbackExceptions = multiTransaction.rollbackFor
|
|
|
val noRollbackExceptions = multiTransaction.noRollbackFor
|
|
|
|
|
@@ -57,10 +60,13 @@ class MultiDataSourceAspect {
|
|
|
|
|
|
// rollbackFor回滚 noRollbackFor不回滚
|
|
|
if (isRollbackExceptionPresent || !isNoRollbackExceptionPresent) {
|
|
|
- userTransaction?.rollback()
|
|
|
+ logger.info("callback...")
|
|
|
+ userTransaction.rollback()
|
|
|
} else {
|
|
|
- userTransaction?.commit()
|
|
|
+ logger.info("commit...")
|
|
|
+ userTransaction.commit()
|
|
|
}
|
|
|
+ throw ZenException(ZenExceptionEnum.SERVER_ERROR)
|
|
|
}
|
|
|
} else {
|
|
|
return joinPoint.proceed()
|