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.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.SeataMode;
import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig; 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.DataSourceProxy;

View File

@ -226,8 +226,12 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new FlexSqlSessionFactoryBean(); FlexSqlSessionFactoryBean factory = new FlexSqlSessionFactoryBean();
factory.setDataSource(dataSource); 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) { if (properties.getConfiguration() == null || properties.getConfiguration().getVfsImpl() == null) {
factory.setVfs(SpringBootVFS.class); factory.setVfs(SpringBootVFS.class);
} }

View File

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

View File

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

View File

@ -19,6 +19,8 @@ import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.datasource.FlexDataSource; import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.mybatis.FlexConfiguration; import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder; 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.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.cache.Cache; import org.apache.ibatis.cache.Cache;
@ -152,6 +154,21 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
private ObjectWrapperFactory objectWrapperFactory; 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. * Sets the ObjectFactory.
* *
@ -598,6 +615,16 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
// fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I70QWU // fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I70QWU
// 兼容SpringManagedTransactionFactory否则在使用JdbcTemplate,多数据源使用JdbcTemplate报错 // 兼容SpringManagedTransactionFactory否则在使用JdbcTemplate,多数据源使用JdbcTemplate报错
//fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J //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, targetConfiguration.setEnvironment(new Environment(this.environment,
// this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory, // this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,
// this.transactionFactory == null ? new JdbcTransactionFactory() : 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.mybatisflex.spring.boot; package com.mybatisflex.spring;
/** /**
* @author life * @author life

View File

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

View File

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

View File

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

View File

@ -4,21 +4,19 @@ mybatis-flex:
seata-config: seata-config:
enable: true #启动seata enable: true #启动seata
seata-mode: XA #xa或者ta seata-mode: XA #xa或者ta
datasource: # datasource:
stockdb: # stockdb:
url: jdbc:mysql://127.0.0.1:3306/db_stock # url: jdbc:mysql://127.0.0.1:3306/db_stock
username: root # username: root
password: 131496 # 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
spring: spring:
main: main:
allow-circular-references: true 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: seata:
enabled: true enabled: true
application-id: stock-service application-id: stock-service

View File

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

View File

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