!243 修复非事务提交数据源因为提交报错问题

Merge pull request !243 from life/main
This commit is contained in:
Michael Yang 2023-08-04 08:11:11 +00:00 committed by Gitee
commit 648bac61fc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 13 additions and 6 deletions

View File

@ -224,15 +224,12 @@ public class FlexDataSource extends AbstractDataSource {
} }
private static class ConnectionHandler implements InvocationHandler { private static class ConnectionHandler implements InvocationHandler {
private static final String[] proxyMethods = new String[]{"commit", "rollback", "close", "setAutoCommit"}; private static final String[] proxyMethods = new String[]{"commit", "rollback", "close", "setAutoCommit"};
private final Connection original; private final Connection original;
private final String xid; private final String xid;
public ConnectionHandler(Connection original, String xid) { public ConnectionHandler(Connection original, String xid) {
closeAutoCommit(original); closeAutoCommit(original);
this.original = original; this.original = original;
this.xid = xid; this.xid = xid;
} }

View File

@ -16,6 +16,7 @@
package com.mybatisflex.spring; package com.mybatisflex.spring;
import com.mybatisflex.core.datasource.FlexDataSource; import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.transaction.TransactionContext;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -30,7 +31,7 @@ public class FlexSpringTransaction implements Transaction {
DataSource dataSource; DataSource dataSource;
boolean isTransaction; boolean isTransaction =false;
TransactionIsolationLevel level; TransactionIsolationLevel level;
boolean autoCommit; boolean autoCommit;
@ -46,6 +47,10 @@ public class FlexSpringTransaction implements Transaction {
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
if (dataSource instanceof FlexDataSource) { if (dataSource instanceof FlexDataSource) {
this.autoCommit = this.dataSource.getConnection().getAutoCommit();
if ( TransactionContext.getXID() != null){
isTransaction = true;
}
return dataSource.getConnection(); return dataSource.getConnection();
}else{ }else{
throw new SQLException("The datasource must be FlextDataSource"); throw new SQLException("The datasource must be FlextDataSource");
@ -56,13 +61,18 @@ public class FlexSpringTransaction implements Transaction {
@Override @Override
public void commit() throws SQLException { public void commit() throws SQLException {
if (!isTransaction && !autoCommit){
getConnection().commit(); getConnection().commit();
} }
}
@Override @Override
public void rollback() throws SQLException { public void rollback() throws SQLException {
if (!isTransaction && !autoCommit){
getConnection().rollback(); getConnection().rollback();
} }
}
@Override @Override
public void close() throws SQLException { public void close() throws SQLException {