mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +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.datasource.FlexDataSource;
|
||||||
import com.mybatisflex.core.transaction.TransactionContext;
|
import com.mybatisflex.core.transaction.TransactionContext;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
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.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
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
|
* @author life
|
||||||
*/
|
*/
|
||||||
public class FlexSpringTransaction implements Transaction {
|
public class FlexSpringTransaction implements Transaction {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlexSpringTransaction.class);
|
private final FlexDataSource dataSource;
|
||||||
DataSource dataSource;
|
private final Map<String, Connection> connectionMap = new HashMap<>();
|
||||||
|
|
||||||
Map<String,Connection> connectionMap =new HashMap<>();
|
private boolean isTransaction = false;
|
||||||
|
private boolean autoCommit;
|
||||||
boolean isTransaction =false;
|
|
||||||
|
|
||||||
TransactionIsolationLevel level;
|
|
||||||
boolean autoCommit;
|
|
||||||
|
|
||||||
|
|
||||||
public FlexSpringTransaction(DataSource dataSource, TransactionIsolationLevel level,
|
public FlexSpringTransaction(FlexDataSource dataSource, boolean autoCommit) {
|
||||||
boolean autoCommit) {
|
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
this.level = level;
|
|
||||||
this.autoCommit = autoCommit;
|
this.autoCommit = autoCommit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
if (dataSource instanceof FlexDataSource) {
|
String dataSourceKey = DataSourceKey.get();
|
||||||
String dataSourceKey = DataSourceKey.get();
|
if (StringUtil.isBlank(dataSourceKey)) {
|
||||||
if (StringUtil.isBlank(dataSourceKey)) {
|
dataSourceKey = dataSource.getDefaultDataSourceKey();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (!isTransaction && !autoCommit){
|
if (!isTransaction && !autoCommit) {
|
||||||
getConnection().commit();
|
getConnection().commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void rollback() throws SQLException {
|
public void rollback() throws SQLException {
|
||||||
if (!isTransaction && !autoCommit){
|
if (!isTransaction && !autoCommit) {
|
||||||
getConnection().rollback();
|
getConnection().rollback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,14 +15,15 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.spring;
|
package com.mybatisflex.spring;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||||
import java.util.Properties;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
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.SpringManagedTransaction;
|
|
||||||
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author life
|
* @author life
|
||||||
*/
|
*/
|
||||||
@ -33,7 +34,7 @@ public class FlexTransactionFactory extends SpringManagedTransactionFactory {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
|
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