mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
refactor: optimize FlexSpringTransaction.java
This commit is contained in:
parent
0bdc6c6182
commit
e7660b6b81
@ -19,74 +19,64 @@ 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.List;
|
||||
import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||
import org.apache.ibatis.transaction.Transaction;
|
||||
import org.mybatis.logging.Logger;
|
||||
import org.mybatis.logging.LoggerFactory;
|
||||
import org.mybatis.spring.transaction.SpringManagedTransaction;
|
||||
|
||||
/**
|
||||
* spring事务支持,解决issusehttps://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||
* spring 事务支持,解决 issues https://gitee.com/mybatis-flex/mybatis-flex/issues/I7HJ4J
|
||||
*
|
||||
* @author life
|
||||
*/
|
||||
public class FlexSpringTransaction implements Transaction {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlexSpringTransaction.class);
|
||||
DataSource dataSource;
|
||||
private final FlexDataSource dataSource;
|
||||
private final Map<String, Connection> connectionMap = new HashMap<>();
|
||||
|
||||
Map<String,Connection> connectionMap =new HashMap<>();
|
||||
|
||||
boolean isTransaction =false;
|
||||
|
||||
TransactionIsolationLevel level;
|
||||
boolean autoCommit;
|
||||
private boolean isTransaction = false;
|
||||
private boolean autoCommit;
|
||||
|
||||
|
||||
public FlexSpringTransaction(DataSource dataSource, TransactionIsolationLevel level,
|
||||
boolean autoCommit) {
|
||||
public FlexSpringTransaction(FlexDataSource dataSource, boolean autoCommit) {
|
||||
this.dataSource = dataSource;
|
||||
this.level = level;
|
||||
this.autoCommit = autoCommit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
if (dataSource instanceof FlexDataSource) {
|
||||
String dataSourceKey = DataSourceKey.get();
|
||||
if (StringUtil.isBlank(dataSourceKey)) {
|
||||
dataSourceKey = ((FlexDataSource) 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){
|
||||
isTransaction = true;
|
||||
}
|
||||
return connection;
|
||||
}else{
|
||||
throw new SQLException("The datasource must be FlexDataSource");
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
if (!isTransaction && !autoCommit){
|
||||
if (!isTransaction && !autoCommit) {
|
||||
getConnection().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws SQLException {
|
||||
if (!isTransaction && !autoCommit){
|
||||
if (!isTransaction && !autoCommit) {
|
||||
getConnection().rollback();
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,14 +15,15 @@
|
||||
*/
|
||||
package com.mybatisflex.spring;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import org.apache.ibatis.session.TransactionIsolationLevel;
|
||||
import org.apache.ibatis.transaction.Transaction;
|
||||
import org.mybatis.spring.transaction.SpringManagedTransaction;
|
||||
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author life
|
||||
*/
|
||||
@ -33,7 +34,7 @@ public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
||||
*/
|
||||
@Override
|
||||
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
|
||||
return new FlexSpringTransaction(dataSource,level,autoCommit);
|
||||
return new FlexSpringTransaction((FlexDataSource) dataSource, autoCommit);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user