mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
refactor: FlexSpringTransaction.java
This commit is contained in:
parent
2bf2bcedd9
commit
94bce8f1fb
@ -15,70 +15,39 @@
|
||||
*/
|
||||
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.util.StringUtil;
|
||||
import org.apache.ibatis.transaction.Transaction;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* spring 事务支持,解决 issues https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||
* spring 事务支持,解决 issues: https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||
*
|
||||
* @author life
|
||||
* @author michael
|
||||
*/
|
||||
public class FlexSpringTransaction implements Transaction {
|
||||
|
||||
private final FlexDataSource dataSource;
|
||||
private final Map<String, Connection> connectionMap = new HashMap<>();
|
||||
|
||||
private boolean isTransaction = false;
|
||||
private boolean autoCommit;
|
||||
|
||||
|
||||
public FlexSpringTransaction(FlexDataSource dataSource, boolean autoCommit) {
|
||||
public FlexSpringTransaction(FlexDataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
this.autoCommit = autoCommit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
String dataSourceKey = DataSourceKey.get();
|
||||
if (StringUtil.isBlank(dataSourceKey)) {
|
||||
dataSourceKey = dataSource.getDefaultDataSourceKey();
|
||||
}
|
||||
|
||||
Connection connection = connectionMap.get(dataSourceKey);
|
||||
if (connection == null) {
|
||||
connection = this.dataSource.getConnection();
|
||||
connectionMap.put(dataSourceKey, connection);
|
||||
}
|
||||
|
||||
this.autoCommit = connection.getAutoCommit();
|
||||
|
||||
if (TransactionContext.getXID() != null) {
|
||||
this.isTransaction = true;
|
||||
}
|
||||
|
||||
return connection;
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
if (isHoldConnection() && !isTransaction && !autoCommit) {
|
||||
getConnection().commit();
|
||||
}
|
||||
getConnection().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws SQLException {
|
||||
if (isHoldConnection() && !isTransaction && !autoCommit) {
|
||||
getConnection().rollback();
|
||||
}
|
||||
getConnection().rollback();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,8 +59,4 @@ public class FlexSpringTransaction implements Transaction {
|
||||
public Integer getTimeout() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isHoldConnection() {
|
||||
return !connectionMap.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,23 +18,23 @@ package com.mybatisflex.spring;
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||
import org.apache.ibatis.transaction.Transaction;
|
||||
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
||||
import org.apache.ibatis.transaction.TransactionFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author life
|
||||
* @author michael
|
||||
*/
|
||||
public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
||||
public class FlexTransactionFactory implements TransactionFactory {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
|
||||
return new FlexSpringTransaction((FlexDataSource) dataSource, autoCommit);
|
||||
return new FlexSpringTransaction((FlexDataSource) dataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,14 +42,6 @@ public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
||||
*/
|
||||
@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
|
||||
throw new UnsupportedOperationException("New Flex transactions require a DataSource");
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,6 +29,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class MultiDataSourceTester {
|
||||
|
||||
@ -58,6 +59,16 @@ public class MultiDataSourceTester {
|
||||
MessageCollector collector = new ConsoleMessageCollector();
|
||||
AuditManager.setMessageCollector(collector);
|
||||
|
||||
Db.tx(new Supplier<Boolean>() {
|
||||
@Override
|
||||
public Boolean get() {
|
||||
Db.selectAll(null, "tb_account");
|
||||
DataSourceKey.use("ds2");
|
||||
Db.selectAll(null, "tb_account");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//默认查询 db1
|
||||
System.out.println("\n------ds1");
|
||||
|
||||
@ -130,4 +130,19 @@ public class AccountController {
|
||||
return ">>>>>ds: " + DataSourceKey.get();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/multids")
|
||||
@Transactional
|
||||
@UseDataSource("ds1")
|
||||
public String multids(){
|
||||
Db.selectAll("tb_account");
|
||||
|
||||
DataSourceKey.use("ds2");
|
||||
|
||||
Db.selectAll("tb_account");
|
||||
|
||||
Db.updateById("tb_account",Row.ofKey("id",1).set("user_name","newUserName"));
|
||||
return "ok";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ spring:
|
||||
# init:
|
||||
# schema-locations: classpath:schema.sql
|
||||
# data-locations: classpath:data.sql
|
||||
mybatis-flex:
|
||||
admin-config:
|
||||
enable: true
|
||||
endpoint: http://localhost/admin
|
||||
secret-key: secretKey
|
||||
mapper-locations:
|
||||
- classpath*:/mapper/*.xml
|
||||
#mybatis-flex:
|
||||
# admin-config:
|
||||
# enable: true
|
||||
# endpoint: http://localhost/admin
|
||||
# secret-key: secretKey
|
||||
# mapper-locations:
|
||||
# - classpath*:/mapper/*.xml
|
||||
# global-config:
|
||||
# print-banner: false
|
||||
# key-config:
|
||||
@ -36,13 +36,13 @@ mybatis-flex:
|
||||
# url: jdbc:mysql://localhost:3306/flex_test
|
||||
# username: root
|
||||
# password: 12345678
|
||||
#mybatis-flex:
|
||||
# datasource:
|
||||
# ds3333:
|
||||
# url: jdbc:mysql://127.0.0.1:3306/flex_test
|
||||
# username: root
|
||||
# password: 131496
|
||||
# ds2:
|
||||
# url: jdbc:mysql://127.0.0.1:3306/flex_test1
|
||||
# username: root
|
||||
# password: 131496
|
||||
mybatis-flex:
|
||||
datasource:
|
||||
ds1:
|
||||
url: jdbc:mysql://127.0.0.1:3306/flex_test
|
||||
username: root
|
||||
password: 123456
|
||||
ds2:
|
||||
url: jdbc:mysql://127.0.0.1:3306/flex_test
|
||||
username: root
|
||||
password: 123456
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user