mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
修复issuse(https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J)以及多数据源JdbcTemplate事务下使用报错
This commit is contained in:
parent
1f79c451ed
commit
35086138f9
@ -0,0 +1,70 @@
|
|||||||
|
package com.mybatisflex.spring;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||||
|
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||||
|
import com.mybatisflex.core.transaction.TransactionContext;
|
||||||
|
import com.mybatisflex.core.transaction.TransactionalManager;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||||
|
import org.apache.ibatis.transaction.Transaction;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spring事务支持,解决issusehttps://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||||
|
* @author life
|
||||||
|
*/
|
||||||
|
public class FlexSpringTransaction implements Transaction {
|
||||||
|
|
||||||
|
DataSource dataSource;
|
||||||
|
|
||||||
|
boolean isTransaction;
|
||||||
|
|
||||||
|
TransactionIsolationLevel level;
|
||||||
|
boolean autoCommit;
|
||||||
|
|
||||||
|
|
||||||
|
public FlexSpringTransaction(DataSource dataSource, TransactionIsolationLevel level,
|
||||||
|
boolean autoCommit) {
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
this.level = level;
|
||||||
|
this.autoCommit = autoCommit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
if (dataSource instanceof FlexDataSource) {
|
||||||
|
return dataSource.getConnection();
|
||||||
|
}else{
|
||||||
|
throw new SQLException("The datasource must be FlextDataSource");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commit() throws SQLException {
|
||||||
|
getConnection().commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rollback() throws SQLException {
|
||||||
|
getConnection().rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SQLException {
|
||||||
|
getConnection().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getTimeout() throws SQLException {
|
||||||
|
return getConnection().getNetworkTimeout();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -89,6 +89,7 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray;
|
|||||||
* @author Jens Schauder
|
* @author Jens Schauder
|
||||||
* @author 王帅
|
* @author 王帅
|
||||||
* @author miachel
|
* @author miachel
|
||||||
|
* @author life
|
||||||
*/
|
*/
|
||||||
public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
|
public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
|
||||||
implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
|
implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
|
||||||
@ -595,9 +596,12 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean
|
|||||||
|
|
||||||
// 事务由 flex 管理了,无需使用 SpringManagedTransactionFactory,否则会造成在同一个事务下,无法切换数据源的问题
|
// 事务由 flex 管理了,无需使用 SpringManagedTransactionFactory,否则会造成在同一个事务下,无法切换数据源的问题
|
||||||
// fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I70QWU
|
// fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I70QWU
|
||||||
|
// 兼容SpringManagedTransactionFactory否则在使用JdbcTemplate,多数据源使用JdbcTemplate报错
|
||||||
|
//fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||||
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,
|
||||||
|
this.transactionFactory == null ? new FlexTransactionFactory() : this.transactionFactory,
|
||||||
dataSource instanceof FlexDataSource ? dataSource : new FlexDataSource(FlexConsts.NAME, dataSource)));
|
dataSource instanceof FlexDataSource ? dataSource : new FlexDataSource(FlexConsts.NAME, dataSource)));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,39 @@
|
|||||||
|
package com.mybatisflex.spring;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.util.Properties;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||||
|
import org.apache.ibatis.transaction.Transaction;
|
||||||
|
import org.mybatis.spring.transaction.SpringManagedTransaction;
|
||||||
|
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author life
|
||||||
|
*/
|
||||||
|
public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
|
||||||
|
return new FlexSpringTransaction(dataSource,level,autoCommit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Transaction newTransaction(Connection conn) {
|
||||||
|
throw new UnsupportedOperationException("New Spring transactions require a DataSource");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setProperties(Properties props) {
|
||||||
|
// not needed in this version
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -26,6 +26,7 @@ import com.mybatisflex.test.mapper.MyAccountMapper;
|
|||||||
import com.mybatisflex.test.model.Account;
|
import com.mybatisflex.test.model.Account;
|
||||||
import com.mybatisflex.test.model.AccountDto;
|
import com.mybatisflex.test.model.AccountDto;
|
||||||
import com.mybatisflex.test.service.AccountService;
|
import com.mybatisflex.test.service.AccountService;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -47,9 +48,19 @@ public class AccountController {
|
|||||||
@Resource
|
@Resource
|
||||||
AccountService accountService;
|
AccountService accountService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/account/add")
|
@PostMapping("/account/add")
|
||||||
String add(@RequestBody Account account) {
|
@Transactional
|
||||||
|
public String add(@RequestBody Account account) {
|
||||||
|
jdbcTemplate.queryForObject("select count(*) from tb_account",Integer.class);
|
||||||
|
DataSourceKey.use("ds2");
|
||||||
|
jdbcTemplate.update("INSERT INTO `flex_test`.`tb_account` ( `user_name`, `age`, `birthday`, `gender`, `is_delete`) VALUES ( '王五', 18, '2023-07-04 15:00:26', NULL, 000);");
|
||||||
|
DataSourceKey.use("ds3333");
|
||||||
|
accountMapper.insert(account);
|
||||||
|
DataSourceKey.use("ds2");
|
||||||
accountMapper.insert(account);
|
accountMapper.insert(account);
|
||||||
return "add ok!";
|
return "add ok!";
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user