seata事务的支持

This commit is contained in:
life 2023-08-07 15:36:17 +08:00
parent cf79c27ab7
commit 347478e846
4 changed files with 91 additions and 0 deletions

View File

@ -91,6 +91,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies> </dependencies>

View File

@ -19,7 +19,10 @@ import com.mybatisflex.core.datasource.DataSourceBuilder;
import com.mybatisflex.core.datasource.DataSourceDecipher; import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.datasource.DataSourceManager; import com.mybatisflex.core.datasource.DataSourceManager;
import com.mybatisflex.core.datasource.FlexDataSource; import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig;
import com.mybatisflex.spring.datasource.DataSourceAdvice; import com.mybatisflex.spring.datasource.DataSourceAdvice;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
@ -53,6 +56,8 @@ public class MultiDataSourceAutoConfiguration {
private final Map<String, Map<String, String>> dataSourceProperties; private final Map<String, Map<String, String>> dataSourceProperties;
private final SeataConfig seataConfig;
//数据源解密器 //数据源解密器
protected final DataSourceDecipher dataSourceDecipher; protected final DataSourceDecipher dataSourceDecipher;
@ -62,6 +67,7 @@ public class MultiDataSourceAutoConfiguration {
) { ) {
dataSourceProperties = properties.getDatasource(); dataSourceProperties = properties.getDatasource();
dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable(); dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
seataConfig = properties.getSeataConfig();
} }
@Bean @Bean
@ -76,6 +82,13 @@ public class MultiDataSourceAutoConfiguration {
for (Map.Entry<String, Map<String, String>> entry : dataSourceProperties.entrySet()) { for (Map.Entry<String, Map<String, String>> entry : dataSourceProperties.entrySet()) {
DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); DataSource dataSource = new DataSourceBuilder(entry.getValue()).build();
if (seataConfig.isEnable()){
if (seataConfig.getSeataMode() ==SeataMode.XA){
dataSource = new DataSourceProxyXA(dataSource);
}else {
dataSource = new DataSourceProxy(dataSource);
}
}
if (flexDataSource == null) { if (flexDataSource == null) {
flexDataSource = new FlexDataSource(entry.getKey(), dataSource); flexDataSource = new FlexDataSource(entry.getKey(), dataSource);
} else { } else {

View File

@ -122,6 +122,19 @@ public class MybatisFlexProperties {
*/ */
private CoreConfiguration configuration; private CoreConfiguration configuration;
/**
* A Configuration object for seata
*/
private SeataConfig seataConfig;
public SeataConfig getSeataConfig() {
return seataConfig;
}
public void setSeataConfig(SeataConfig seataConfig) {
this.seataConfig = seataConfig;
}
public Map<String, Map<String, String>> getDatasource() { public Map<String, Map<String, String>> getDatasource() {
return datasource; return datasource;
} }
@ -890,4 +903,38 @@ public class MybatisFlexProperties {
} }
/**
* Seata 配置
*
* @author life
*/
public static class SeataConfig{
/**
* 是否开启
*/
private boolean enable = false;
/**
* 事务模式支持只支持XA或者AT
*/
private SeataMode seataMode = SeataMode.AT;
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public SeataMode getSeataMode() {
return seataMode;
}
public void setSeataMode(SeataMode seataMode) {
this.seataMode = seataMode;
}
}
} }

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.spring.boot;
/**
* @author life
*/
public enum SeataMode {
XA,
AT
}