mirror of
https://gitee.com/dromara/Raincat.git
synced 2024-12-02 03:48:35 +08:00
Merge pull request #43 from xyzhimo/master
support choose specific transaction manager
This commit is contained in:
commit
22a3239fc4
@ -56,5 +56,11 @@ public class TxTransactionInfo {
|
||||
@Getter
|
||||
private PropagationEnum propagationEnum;
|
||||
|
||||
/**
|
||||
* 事务管理器名称
|
||||
*/
|
||||
@Getter
|
||||
private String transactionManager;
|
||||
|
||||
|
||||
}
|
||||
|
@ -44,4 +44,11 @@ public @interface TxTransaction {
|
||||
* @return 多少秒
|
||||
*/
|
||||
int waitMaxTime() default 60;
|
||||
|
||||
/**
|
||||
* 事务管理器名称
|
||||
*
|
||||
* @return 如果为空,则取默认的
|
||||
*/
|
||||
String transactionManager() default "";
|
||||
}
|
||||
|
@ -54,6 +54,18 @@ public final class SpringBeanUtils {
|
||||
return cfgContext.getBean(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* acquire spring bean by bean name and type.
|
||||
*
|
||||
* @param beanName bean name
|
||||
* @param type type
|
||||
* @param <T> class
|
||||
* @return bean
|
||||
*/
|
||||
public <T> T getBean(final String beanName, final Class<T> type) {
|
||||
return cfgContext.getBean(beanName, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* register bean in spring ioc.
|
||||
* @param beanName bean name
|
||||
|
@ -0,0 +1,24 @@
|
||||
package org.dromara.raincat.core.helper;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
public class TransactionManagerHelper {
|
||||
|
||||
/**
|
||||
* description:
|
||||
*
|
||||
* @param transactionManager the name of transactionManager
|
||||
*
|
||||
* @return if transactionManager is blank return the primary PlatformTransactionManager
|
||||
* otherwise return the PlatformTransactionManager by the name.
|
||||
*/
|
||||
public static PlatformTransactionManager getTransactionManager(String transactionManager) {
|
||||
if (StringUtils.isBlank(transactionManager)) {
|
||||
return SpringBeanUtils.getInstance().getBean(PlatformTransactionManager.class);
|
||||
} else {
|
||||
return SpringBeanUtils.getInstance().getBean(transactionManager, PlatformTransactionManager.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -33,6 +33,7 @@ import org.dromara.raincat.core.concurrent.task.BlockTask;
|
||||
import org.dromara.raincat.core.concurrent.task.BlockTaskHelper;
|
||||
import org.dromara.raincat.core.concurrent.threadlocal.TxTransactionLocal;
|
||||
import org.dromara.raincat.core.concurrent.threadpool.TxTransactionThreadPool;
|
||||
import org.dromara.raincat.core.helper.TransactionManagerHelper;
|
||||
import org.dromara.raincat.core.service.TxManagerMessageService;
|
||||
import org.dromara.raincat.core.service.TxTransactionHandler;
|
||||
import org.slf4j.Logger;
|
||||
@ -63,15 +64,11 @@ public class ActorTxTransactionHandler implements TxTransactionHandler {
|
||||
|
||||
private final TxCompensationManager txCompensationManager;
|
||||
|
||||
private final PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Autowired
|
||||
public ActorTxTransactionHandler(final TxCompensationManager txCompensationManager,
|
||||
final PlatformTransactionManager platformTransactionManager,
|
||||
final TxTransactionThreadPool txTransactionThreadPool,
|
||||
final TxManagerMessageService txManagerMessageService) {
|
||||
this.txCompensationManager = txCompensationManager;
|
||||
this.platformTransactionManager = platformTransactionManager;
|
||||
this.txTransactionThreadPool = txTransactionThreadPool;
|
||||
this.txManagerMessageService = txManagerMessageService;
|
||||
}
|
||||
@ -91,6 +88,8 @@ public class ActorTxTransactionHandler implements TxTransactionHandler {
|
||||
String commitStatus = CommonConstant.TX_TRANSACTION_COMMIT_STATUS_BAD;
|
||||
|
||||
final BlockTask waitTask = BlockTaskHelper.getInstance().getTask(waitKey);
|
||||
PlatformTransactionManager platformTransactionManager =
|
||||
TransactionManagerHelper.getTransactionManager(info.getTransactionManager());
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
|
||||
|
@ -19,6 +19,7 @@
|
||||
package org.dromara.raincat.core.service.handler;
|
||||
|
||||
import org.dromara.raincat.common.bean.TxTransactionInfo;
|
||||
import org.dromara.raincat.core.helper.TransactionManagerHelper;
|
||||
import org.dromara.raincat.core.service.TxTransactionHandler;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -35,13 +36,6 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
@Component
|
||||
public class InsideCompensationHandler implements TxTransactionHandler {
|
||||
|
||||
private final PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Autowired
|
||||
public InsideCompensationHandler(final PlatformTransactionManager platformTransactionManager) {
|
||||
this.platformTransactionManager = platformTransactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理补偿内嵌的远程方法的时候,不提交,只调用.
|
||||
*
|
||||
@ -54,6 +48,8 @@ public class InsideCompensationHandler implements TxTransactionHandler {
|
||||
public Object handler(final ProceedingJoinPoint point, final TxTransactionInfo info) throws Throwable {
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
PlatformTransactionManager platformTransactionManager =
|
||||
TransactionManagerHelper.getTransactionManager(info.getTransactionManager());
|
||||
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
|
||||
try {
|
||||
return point.proceed();
|
||||
|
@ -23,6 +23,7 @@ import org.dromara.raincat.common.bean.TxTransactionInfo;
|
||||
import org.dromara.raincat.common.constant.CommonConstant;
|
||||
import org.dromara.raincat.core.concurrent.threadlocal.CompensationLocal;
|
||||
import org.dromara.raincat.core.concurrent.threadlocal.TxTransactionLocal;
|
||||
import org.dromara.raincat.core.helper.TransactionManagerHelper;
|
||||
import org.dromara.raincat.core.service.TxTransactionHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -38,13 +39,6 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
@Component
|
||||
public class StartCompensationHandler implements TxTransactionHandler {
|
||||
|
||||
private final PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Autowired
|
||||
public StartCompensationHandler(final PlatformTransactionManager platformTransactionManager) {
|
||||
this.platformTransactionManager = platformTransactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 补偿的时候,不走分布式事务处理.
|
||||
*
|
||||
@ -56,6 +50,9 @@ public class StartCompensationHandler implements TxTransactionHandler {
|
||||
@Override
|
||||
public Object handler(final ProceedingJoinPoint point, final TxTransactionInfo info) throws Throwable {
|
||||
TxTransactionLocal.getInstance().setTxGroupId(CommonConstant.COMPENSATE_ID);
|
||||
|
||||
PlatformTransactionManager platformTransactionManager =
|
||||
TransactionManagerHelper.getTransactionManager(info.getTransactionManager());
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
|
||||
|
@ -32,6 +32,7 @@ import org.dromara.raincat.common.netty.bean.TxTransactionGroup;
|
||||
import org.dromara.raincat.common.netty.bean.TxTransactionItem;
|
||||
import org.dromara.raincat.core.compensation.manager.TxCompensationManager;
|
||||
import org.dromara.raincat.core.concurrent.threadlocal.TxTransactionLocal;
|
||||
import org.dromara.raincat.core.helper.TransactionManagerHelper;
|
||||
import org.dromara.raincat.core.service.TxManagerMessageService;
|
||||
import org.dromara.raincat.core.service.TxTransactionHandler;
|
||||
import org.slf4j.Logger;
|
||||
@ -61,15 +62,11 @@ public class StartTxTransactionHandler implements TxTransactionHandler {
|
||||
|
||||
private final TxCompensationManager txCompensationManager;
|
||||
|
||||
private final PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Autowired(required = false)
|
||||
public StartTxTransactionHandler(final TxManagerMessageService txManagerMessageService,
|
||||
final TxCompensationManager txCompensationManager,
|
||||
final PlatformTransactionManager platformTransactionManager) {
|
||||
final TxCompensationManager txCompensationManager) {
|
||||
this.txManagerMessageService = txManagerMessageService;
|
||||
this.txCompensationManager = txCompensationManager;
|
||||
this.platformTransactionManager = platformTransactionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -108,6 +105,8 @@ public class StartTxTransactionHandler implements TxTransactionHandler {
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
PlatformTransactionManager platformTransactionManager =
|
||||
TransactionManagerHelper.getTransactionManager(info.getTransactionManager());
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
|
||||
|
@ -57,10 +57,11 @@ public class AspectTransactionServiceImpl implements AspectTransactionService {
|
||||
Method thisMethod = clazz.getMethod(method.getName(), method.getParameterTypes());
|
||||
final String compensationId = CompensationLocal.getInstance().getCompensationId();
|
||||
final TxTransaction txTransaction = method.getAnnotation(TxTransaction.class);
|
||||
final String transactionManager = txTransaction.transactionManager();
|
||||
final int waitMaxTime = txTransaction.waitMaxTime();
|
||||
final PropagationEnum propagation = txTransaction.propagation();
|
||||
TransactionInvocation invocation = new TransactionInvocation(clazz, thisMethod.getName(), args, method.getParameterTypes());
|
||||
TxTransactionInfo info = new TxTransactionInfo(invocation, transactionGroupId, compensationId, waitMaxTime, propagation);
|
||||
TxTransactionInfo info = new TxTransactionInfo(invocation, transactionGroupId, compensationId, waitMaxTime, propagation, transactionManager);
|
||||
final Class css = txTransactionFactoryService.factoryOf(info);
|
||||
final TxTransactionHandler txTransactionHandler =
|
||||
(TxTransactionHandler) SpringBeanUtils.getInstance().getBean(css);
|
||||
|
Loading…
Reference in New Issue
Block a user