mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
update TransactionalManager
This commit is contained in:
parent
5ff724e119
commit
ae8e02de66
@ -28,7 +28,6 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@ -154,7 +153,6 @@ public class FlexDataSource extends AbstractDataSource {
|
||||
//do nothing
|
||||
return null;
|
||||
}
|
||||
System.out.println(">>>>>>invoke: " + method.getName() + " args: " + Arrays.toString(args));
|
||||
return method.invoke(original, args);
|
||||
}
|
||||
|
||||
|
||||
@ -26,7 +26,6 @@ import org.apache.ibatis.util.MapUtil;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -395,34 +394,29 @@ public class Db {
|
||||
|
||||
public static boolean tx(Supplier<Boolean> supplier) {
|
||||
//上一级事务的id,支持事务嵌套
|
||||
String prevXID = TransactionContext.getXID();
|
||||
String higherXID = TransactionContext.getXID();
|
||||
try {
|
||||
String xid = UUID.randomUUID().toString();
|
||||
TransactionContext.hold(xid);
|
||||
String xid = TransactionalManager.startTransactional();
|
||||
Boolean success = false;
|
||||
boolean rollbacked = false;
|
||||
try {
|
||||
success = supplier.get();
|
||||
} catch (Exception e) {
|
||||
rollbacked = true;
|
||||
TransactionContext.release();
|
||||
TransactionalManager.rollback(xid);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (success != null && success) {
|
||||
//必须优先 release 掉 xid,才能正常 commit()
|
||||
TransactionContext.release();
|
||||
TransactionalManager.commit(xid);
|
||||
} else if (!rollbacked) {
|
||||
TransactionContext.release();
|
||||
TransactionalManager.rollback(xid);
|
||||
}
|
||||
}
|
||||
return success != null && success;
|
||||
} finally {
|
||||
//恢复上一级事务
|
||||
if (prevXID != null) {
|
||||
TransactionContext.hold(prevXID);
|
||||
if (higherXID != null) {
|
||||
TransactionContext.hold(higherXID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,5 +32,4 @@ public class TransactionContext {
|
||||
XID_HOLDER.set(xid);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@ import org.apache.ibatis.logging.LogFactory;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -34,6 +35,7 @@ public class TransactionalManager {
|
||||
private static final ThreadLocal<Map<String, Map<String, Connection>>> CONNECTION_HOLDER
|
||||
= ThreadLocal.withInitial(ConcurrentHashMap::new);
|
||||
|
||||
|
||||
public static void hold(String xid, String ds, Connection connection) {
|
||||
Map<String, Map<String, Connection>> holdMap = CONNECTION_HOLDER.get();
|
||||
Map<String, Connection> connMap = holdMap.get(xid);
|
||||
@ -63,6 +65,12 @@ public class TransactionalManager {
|
||||
}
|
||||
|
||||
|
||||
public static String startTransactional() {
|
||||
String xid = UUID.randomUUID().toString();
|
||||
TransactionContext.hold(xid);
|
||||
return xid;
|
||||
}
|
||||
|
||||
public static void commit(String xid) {
|
||||
release(xid, true);
|
||||
}
|
||||
@ -71,8 +79,10 @@ public class TransactionalManager {
|
||||
release(xid, false);
|
||||
}
|
||||
|
||||
|
||||
private static void release(String xid, boolean commit) {
|
||||
//先release,才能正常的进行 commit 或者 rollback.
|
||||
TransactionContext.release();
|
||||
|
||||
Exception exception = null;
|
||||
Map<String, Map<String, Connection>> holdMap = CONNECTION_HOLDER.get();
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user