-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
新增多数据源事务传播机制 #406
Merged
Merged
新增多数据源事务传播机制 #406
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Conflicts: # src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java # src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourcePropertiesCustomizer.java
多数据源事务传播机制 |
在玩呢? |
你好,有看么 |
恩,改动比较大,暂时不会合并。空了会详细测试。先放这里 |
好的,需要改动或兼容可以@ |
有冲突 |
之前是基于3.4改的,我看下3.5 |
# Conflicts: # src/main/java/com/baomidou/dynamic/datasource/tx/TransactionalTemplate.java
更新了下comment ,冲突主要是方法参数加入了xid,可以替换。还有个冲突是原代理类中的事务的方法抽取到了事务模板方法类中 |
有考虑么,冲突可以先用我的版本试试 |
…ce-spring-boot-starter � Conflicts: � src/main/java/com/baomidou/dynamic/datasource/ds/AbstractRoutingDataSource.java � src/main/java/com/baomidou/dynamic/datasource/tx/ConnectionFactory.java � src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java
merge |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
1.主要提取DynamicLocalTransactionInterceptor中的方法到TransactionalTemplate
TransactionalInfo transactionInfo = transactionalExecutor.getTransactionInfo();
if (!StringUtils.isEmpty(TransactionContext.getXID())) {
return transactionalExecutor.execute();
}
boolean state = true;
Object o;
String xid = LocalTxUtil.startTransaction();
try {
o = transactionalExecutor.execute();
} catch (Exception e) {
state = !isRollback(e, transactionInfo);
throw e;
} finally {
if (state) {
LocalTxUtil.commit(xid);
} else {
LocalTxUtil.rollback(xid);
}
}
return o;
原代理方法改为获取注解属性对象的封装
Method method = methodInvocation.getMethod();
final DSTransactional dsTransactional = method.getAnnotation(DSTransactional.class);
2.AbstractRoutingDataSource获取数据源中获取事务的数据源的方法加入xid
ConnectionProxy connection = ConnectionFactory.getConnection(ds);
return connection == null ? getConnectionProxy(ds, determineDataSource().getConnection()) : connection;
ConnectionProxy connection = ConnectionFactory.getConnection(xid, ds);
return connection == null ? getConnectionProxy(xid,ds, determineDataSource().getConnection()) : connection;
3,。增加事务异常类TransactionException
public class TransactionException extends RuntimeException {
public TransactionException(String message) {
super(message);
}
}
4.ConnectionFactory 对于事务数据源的处理通过xid保存 通过xid获取指定事务
public class ConnectionFactory {
5.DsPropagation 事务传播机制枚举
6.src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java 事务工具提交关闭事务加入xid进行获取
public static void startTransaction() {
if (!StringUtils.isEmpty(TransactionContext.getXID())) {
log.debug("dynamic-datasource exist local tx [{}]", TransactionContext.getXID());
public static String startTransaction() {
String xid = TransactionContext.getXID();
if (!StringUtils.isEmpty(xid)) {
log.debug("dynamic-datasource exist local tx [{}]", xid);
} else {
String xid = UUID.randomUUID().toString();
xid = UUID.randomUUID().toString();
TransactionContext.bind(xid);
log.debug("dynamic-datasource start local tx [{}]", xid);
}
return xid;
}
7.事务传播机制使用的事务暂停xid对象 用于事务暂停和恢复xid
package com.baomidou.dynamic.datasource.tx;
import javax.annotation.Nonnull;
public class SuspendedResourcesHolder {
/**
* The xid
*/
private String xid;
}
8。TransactionalExecutor 事务执行器接口
9.TransactionalInfo 对事务注解中属性的封装
10.TransactionalTemplate 事务模板方法
对事务的操作抽取到模板方法中实现。后续可自定义扩展模板方法
处理异常和事务传播机制
package com.baomidou.dynamic.datasource.tx;
import com.baomidou.dynamic.datasource.exception.TransactionException;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.Objects;
@slf4j
public class TransactionalTemplate {
}