seata兼容spring单数据源情况,修改事例兼容

This commit is contained in:
life 2023-08-08 22:21:53 +08:00
parent ecd6ec879e
commit 6b81a678d5
12 changed files with 78 additions and 44 deletions

View File

@ -19,6 +19,7 @@ import com.mybatisflex.core.datasource.DataSourceBuilder;
import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.datasource.DataSourceManager;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.spring.SeataMode;
import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig;
import com.mybatisflex.spring.datasource.DataSourceAdvice;
import io.seata.rm.datasource.DataSourceProxy;

View File

@ -226,8 +226,12 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new FlexSqlSessionFactoryBean();
FlexSqlSessionFactoryBean factory = new FlexSqlSessionFactoryBean();
factory.setDataSource(dataSource);
if (properties.getSeataConfig() !=null && properties.getSeataConfig().isEnable()){
factory.setSeata(true);
factory.setSeataMode(properties.getSeataConfig().getSeataMode());
}
if (properties.getConfiguration() == null || properties.getConfiguration().getVfsImpl() == null) {
factory.setVfs(SpringBootVFS.class);
}

View File

@ -17,6 +17,7 @@ package com.mybatisflex.spring.boot;
import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.spring.SeataMode;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.mapping.ResultSetType;

View File

@ -40,6 +40,12 @@
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>

View File

@ -19,6 +19,8 @@ import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.cache.Cache;
@ -152,6 +154,21 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
private ObjectWrapperFactory objectWrapperFactory;
private boolean seata = false;
private SeataMode seataMode = SeataMode.AT;
public void setSeata(boolean seata) {
this.seata = seata;
}
public void setSeataMode(SeataMode seataMode) {
this.seataMode = seataMode;
}
/**
* Sets the ObjectFactory.
*
@ -598,6 +615,16 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
// fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I70QWU
// 兼容SpringManagedTransactionFactory否则在使用JdbcTemplate,多数据源使用JdbcTemplate报错
//fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
// 兼容单数据源seata
if (!(dataSource instanceof FlexDataSource)){
if (seata){
if (seataMode == SeataMode.XA){
dataSource = new DataSourceProxyXA(dataSource);
}else {
dataSource = new DataSourceProxy(dataSource);
}
}
}
targetConfiguration.setEnvironment(new Environment(this.environment,
// this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,
// this.transactionFactory == null ? new JdbcTransactionFactory() : this.transactionFactory,

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.spring.boot;
package com.mybatisflex.spring;
/**
* @author life

View File

@ -4,21 +4,19 @@ mybatis-flex:
seata-config:
enable: true #启动seata
seata-mode: XA #xa或者ta
datasource:
accountdb:
url: jdbc:mysql://127.0.0.1:3306/db_account
username: root
password: 131496
# datasource:
# accountdb:
# url: jdbc:mysql://127.0.0.1:3306/db_account
# username: root
# password: 131496
spring:
main:
allow-circular-references: true
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/db_account?useSSL=false&serverTimezone=UTC
# username: root
# password: 131496
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_account?useSSL=false&serverTimezone=UTC
username: root
password: 131496
seata:
enabled: true
application-id: account-service

View File

@ -51,6 +51,12 @@
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>

View File

@ -4,21 +4,19 @@ mybatis-flex:
seata-config:
enable: true #启动seata
seata-mode: XA #xa或者ta
datasource:
orderdb:
url: jdbc:mysql://127.0.0.1:3306/db_order
username: root
password: 131496
# datasource:
# orderdb:
# url: jdbc:mysql://127.0.0.1:3306/db_order
# username: root
# password: 131496
spring:
main:
allow-circular-references: true
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/db_order?useSSL=false&serverTimezone=UTC
# username: root
# password: 131496
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_order?useSSL=false&serverTimezone=UTC
username: root
password: 131496
seata:
enabled: true
application-id: order-service

View File

@ -4,21 +4,19 @@ mybatis-flex:
seata-config:
enable: true #启动seata
seata-mode: XA #xa或者ta
datasource:
stockdb:
url: jdbc:mysql://127.0.0.1:3306/db_stock
username: root
password: 131496
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/db_stock?useSSL=false&serverTimezone=UTC
# username: root
# password: 131496
# datasource:
# stockdb:
# url: jdbc:mysql://127.0.0.1:3306/db_stock
# username: root
# password: 131496
spring:
main:
allow-circular-references: true
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_stock?useSSL=false&serverTimezone=UTC
username: root
password: 131496
seata:
enabled: true
application-id: stock-service

View File

@ -91,12 +91,6 @@
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>

View File

@ -1,3 +1,4 @@
mybatis-flex:
seata-config:
enable: true #启动seata