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