mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: FlexSpringTransaction.java
This commit is contained in:
parent
2bf2bcedd9
commit
94bce8f1fb
@ -15,71 +15,40 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.spring;
|
package com.mybatisflex.spring;
|
||||||
|
|
||||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
|
||||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
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 org.apache.ibatis.transaction.Transaction;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
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 life
|
||||||
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class FlexSpringTransaction implements Transaction {
|
public class FlexSpringTransaction implements Transaction {
|
||||||
|
|
||||||
private final FlexDataSource dataSource;
|
private final FlexDataSource dataSource;
|
||||||
private final Map<String, Connection> connectionMap = new HashMap<>();
|
|
||||||
|
|
||||||
private boolean isTransaction = false;
|
public FlexSpringTransaction(FlexDataSource dataSource) {
|
||||||
private boolean autoCommit;
|
|
||||||
|
|
||||||
|
|
||||||
public FlexSpringTransaction(FlexDataSource dataSource, boolean autoCommit) {
|
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
this.autoCommit = autoCommit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
String dataSourceKey = DataSourceKey.get();
|
return dataSource.getConnection();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void commit() throws SQLException {
|
public void commit() throws SQLException {
|
||||||
if (isHoldConnection() && !isTransaction && !autoCommit) {
|
|
||||||
getConnection().commit();
|
getConnection().commit();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void rollback() throws SQLException {
|
public void rollback() throws SQLException {
|
||||||
if (isHoldConnection() && !isTransaction && !autoCommit) {
|
|
||||||
getConnection().rollback();
|
getConnection().rollback();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws SQLException {
|
public void close() throws SQLException {
|
||||||
@ -90,8 +59,4 @@ public class FlexSpringTransaction implements Transaction {
|
|||||||
public Integer getTimeout() throws SQLException {
|
public Integer getTimeout() throws SQLException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isHoldConnection() {
|
|
||||||
return !connectionMap.isEmpty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,23 +18,23 @@ package com.mybatisflex.spring;
|
|||||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||||
import org.apache.ibatis.session.TransactionIsolationLevel;
|
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||||
import org.apache.ibatis.transaction.Transaction;
|
import org.apache.ibatis.transaction.Transaction;
|
||||||
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
import org.apache.ibatis.transaction.TransactionFactory;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author life
|
* @author life
|
||||||
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
public class FlexTransactionFactory implements TransactionFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
|
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
|
@Override
|
||||||
public Transaction newTransaction(Connection conn) {
|
public Transaction newTransaction(Connection conn) {
|
||||||
throw new UnsupportedOperationException("New Spring transactions require a DataSource");
|
throw new UnsupportedOperationException("New Flex transactions require a DataSource");
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setProperties(Properties props) {
|
|
||||||
// not needed in this version
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class MultiDataSourceTester {
|
public class MultiDataSourceTester {
|
||||||
|
|
||||||
@ -58,6 +59,16 @@ public class MultiDataSourceTester {
|
|||||||
MessageCollector collector = new ConsoleMessageCollector();
|
MessageCollector collector = new ConsoleMessageCollector();
|
||||||
AuditManager.setMessageCollector(collector);
|
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
|
//默认查询 db1
|
||||||
System.out.println("\n------ds1");
|
System.out.println("\n------ds1");
|
||||||
|
|||||||
@ -130,4 +130,19 @@ public class AccountController {
|
|||||||
return ">>>>>ds: " + DataSourceKey.get();
|
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:
|
# init:
|
||||||
# schema-locations: classpath:schema.sql
|
# schema-locations: classpath:schema.sql
|
||||||
# data-locations: classpath:data.sql
|
# data-locations: classpath:data.sql
|
||||||
mybatis-flex:
|
#mybatis-flex:
|
||||||
admin-config:
|
# admin-config:
|
||||||
enable: true
|
# enable: true
|
||||||
endpoint: http://localhost/admin
|
# endpoint: http://localhost/admin
|
||||||
secret-key: secretKey
|
# secret-key: secretKey
|
||||||
mapper-locations:
|
# mapper-locations:
|
||||||
- classpath*:/mapper/*.xml
|
# - classpath*:/mapper/*.xml
|
||||||
# global-config:
|
# global-config:
|
||||||
# print-banner: false
|
# print-banner: false
|
||||||
# key-config:
|
# key-config:
|
||||||
@ -36,13 +36,13 @@ mybatis-flex:
|
|||||||
# url: jdbc:mysql://localhost:3306/flex_test
|
# url: jdbc:mysql://localhost:3306/flex_test
|
||||||
# username: root
|
# username: root
|
||||||
# password: 12345678
|
# password: 12345678
|
||||||
#mybatis-flex:
|
mybatis-flex:
|
||||||
# datasource:
|
datasource:
|
||||||
# ds3333:
|
ds1:
|
||||||
# url: jdbc:mysql://127.0.0.1:3306/flex_test
|
url: jdbc:mysql://127.0.0.1:3306/flex_test
|
||||||
# username: root
|
username: root
|
||||||
# password: 131496
|
password: 123456
|
||||||
# ds2:
|
ds2:
|
||||||
# url: jdbc:mysql://127.0.0.1:3306/flex_test1
|
url: jdbc:mysql://127.0.0.1:3306/flex_test
|
||||||
# username: root
|
username: root
|
||||||
# password: 131496
|
password: 123456
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user