fix:主事务内多个嵌套事务,导致影响主事务提交|回滚 (#533)

Co-authored-by: zhangpeng <xinniankuailezp@163.com>
This commit is contained in:
Z.P 2023-07-18 22:33:10 +08:00 committed by GitHub
parent a37ed90c80
commit b83572ca2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 17 deletions

View File

@ -117,7 +117,7 @@ public class ConnectionFactory {
Iterator<SavePointHolder> iterator = savePointHolders.iterator();
while (iterator.hasNext()) {
SavePointHolder savePointHolder = iterator.next();
if (savePointHolder.releaseSavepoint() <= 0) {
if (savePointHolder.releaseSavepoint()) {
iterator.remove();
}
}
@ -128,7 +128,7 @@ public class ConnectionFactory {
SavePointHolder savePointHolder = iterator.next();
ConnectionProxy connectionProxy = savePointHolder.getConnectionProxy();
markedConnectionProxy.add(connectionProxy);
if (savePointHolder.rollbackSavePoint() <= 0) {
if (savePointHolder.rollbackSavePoint()) {
iterator.remove();
}
}
@ -221,15 +221,7 @@ public class ConnectionFactory {
public static boolean hasSavepoint(String xid) {
Map<String, List<SavePointHolder>> savePointMap = SAVEPOINT_CONNECTION_HOLDER.get();
List<SavePointHolder> savePointHolders = savePointMap.get(xid);
if (savePointHolders == null || savePointHolders.isEmpty()) {
return false;
}
for (SavePointHolder savePointHolder : savePointHolders) {
if (!CollectionUtils.isEmpty(savePointHolder.getSavePoints())) {
return true;
}
}
return false;
return !CollectionUtils.isEmpty(savePointHolders);
}
}

View File

@ -16,6 +16,8 @@
package com.baomidou.dynamic.datasource.tx;
import lombok.extern.slf4j.Slf4j;
import java.sql.SQLException;
import java.sql.SQLTransientConnectionException;
import java.sql.Savepoint;
@ -27,6 +29,7 @@ import java.util.List;
*
* @author zp
*/
@Slf4j
public class SavePointHolder {
/**
* savepoint name prefix
@ -72,14 +75,15 @@ public class SavePointHolder {
* @return savepoint index
* @throws SQLException SQLException
*/
public int releaseSavepoint() throws SQLException {
public boolean releaseSavepoint() throws SQLException {
Savepoint savepoint = savepoints.pollLast();
if (savepoint != null) {
connectionProxy.releaseSavepoint(savepoint);
String savepointName = savepoint.getSavepointName();
return Integer.parseInt(savepointName.substring(SAVEPOINT_NAME_PREFIX.length()));
log.info("dynamic-datasource releaseSavepoint [{}]", savepointName);
return savepoints.isEmpty();
}
return -1;
return true;
}
/**
@ -88,14 +92,15 @@ public class SavePointHolder {
* @return savepoint index
* @throws SQLException SQLException
*/
public int rollbackSavePoint() throws SQLException {
public boolean rollbackSavePoint() throws SQLException {
Savepoint savepoint = savepoints.pollLast();
if (savepoint != null) {
connectionProxy.rollback(savepoint);
String savepointName = savepoint.getSavepointName();
return Integer.parseInt(savepointName.substring(SAVEPOINT_NAME_PREFIX.length()));
log.info("dynamic-datasource rollbackSavePoint [{}]", savepointName);
return savepoints.isEmpty();
}
return -1;
return true;
}
/**