add Db.txWithResult method; close #I7DJBD

This commit is contained in:
开源海哥 2023-06-15 10:00:12 +08:00
parent ee0e3f7465
commit ddb239176a
3 changed files with 193 additions and 165 deletions

View File

@ -41,7 +41,8 @@ import java.util.function.Supplier;
*/ */
public class Db { public class Db {
private Db() {} private Db() {
}
private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>(); private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
static RowMapperInvoker defaultRowMapperInvoker; static RowMapperInvoker defaultRowMapperInvoker;
@ -73,6 +74,7 @@ public class Db {
public static int insert(String schema, String tableName, Row row) { public static int insert(String schema, String tableName, Row row) {
return invoker().insert(schema, tableName, row); return invoker().insert(schema, tableName, row);
} }
/** /**
* tableName 插入一条 row 数据 * tableName 插入一条 row 数据
* *
@ -131,6 +133,7 @@ public class Db {
mapper.insert(schema, tableName, row); mapper.insert(schema, tableName, row);
}); });
} }
/** /**
* 批量插入数据 * 批量插入数据
* *
@ -479,7 +482,6 @@ public class Db {
/** /**
* 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容 * 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
* *
*
* @param schema 模式 * @param schema 模式
* @param tableName 表名 * @param tableName 表名
* @param fieldName 字段名 * @param fieldName 字段名
@ -587,7 +589,6 @@ public class Db {
} }
/** /**
* 根据 map 来查询 1 条数据 * 根据 map 来查询 1 条数据
* *
@ -734,6 +735,7 @@ public class Db {
public static List<Row> selectListByCondition(String tableName, QueryCondition condition) { public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition)); return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition));
} }
/** /**
* 根据 condition 条件来查询数据列表 * 根据 condition 条件来查询数据列表
* *
@ -859,7 +861,6 @@ public class Db {
} }
/** /**
* 查询某列内容数据返回应该有 多行 1 * 查询某列内容数据返回应该有 多行 1
* *
@ -1111,17 +1112,31 @@ public class Db {
/** /**
* 进行事务操作 * 进行事务操作返回 null 或者 false 或者 抛出异常事务回滚
*
* @param supplier
*/ */
public static boolean tx(Supplier<Boolean> supplier) { public static boolean tx(Supplier<Boolean> supplier) {
return tx(supplier, Propagation.REQUIRED); return tx(supplier, Propagation.REQUIRED);
} }
/**
* 进行事务操作返回 null 或者 false 或者 抛出异常 事务回滚
*/
public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) { public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) {
Boolean result = TransactionalManager.exec(supplier, propagation); Boolean result = TransactionalManager.exec(supplier, propagation, false);
return result != null && result; return result != null && result;
} }
/**
* 进行事务操作和返回结果无关只有抛出异常时事务回滚
*/
public static <T> T txWithResult(Supplier<T> supplier) {
return txWithResult(supplier, Propagation.REQUIRED);
}
/**
* 进行事务操作和返回结果无关只有抛出异常时事务回滚
*/
public static <T> T txWithResult(Supplier<T> supplier, Propagation propagation) {
return TransactionalManager.exec(supplier, propagation, true);
}
} }

View File

@ -30,7 +30,8 @@ import java.util.function.Supplier;
*/ */
public class TransactionalManager { public class TransactionalManager {
private TransactionalManager() {} private TransactionalManager() {
}
private static final Log log = LogFactory.getLog(TransactionalManager.class); private static final Log log = LogFactory.getLog(TransactionalManager.class);
@ -55,7 +56,7 @@ public class TransactionalManager {
} }
public static Boolean exec(Supplier<Boolean> supplier, Propagation propagation) { public static <T> T exec(Supplier<T> supplier, Propagation propagation, boolean withResult) {
//上一级事务的id支持事务嵌套 //上一级事务的id支持事务嵌套
String currentXID = TransactionContext.getXID(); String currentXID = TransactionContext.getXID();
try { try {
@ -65,7 +66,7 @@ public class TransactionalManager {
if (currentXID != null) { if (currentXID != null) {
return supplier.get(); return supplier.get();
} else { } else {
return execNewTransactional(supplier); return execNewTransactional(supplier, withResult);
} }
@ -85,7 +86,7 @@ public class TransactionalManager {
//始终以新事务的方式运行若存在当前事务则暂停挂起当前事务 //始终以新事务的方式运行若存在当前事务则暂停挂起当前事务
case REQUIRES_NEW: case REQUIRES_NEW:
return execNewTransactional(supplier); return execNewTransactional(supplier, withResult);
//以非事务的方式运行若存在当前事务则暂停挂起当前事务 //以非事务的方式运行若存在当前事务则暂停挂起当前事务
@ -118,24 +119,32 @@ public class TransactionalManager {
} }
} }
private static Boolean execNewTransactional(Supplier<Boolean> supplier) { private static <T> T execNewTransactional(Supplier<T> supplier, boolean withResult) {
String xid = startTransactional(); String xid = startTransactional();
Boolean success = false; T result = null;
boolean rollbacked = false; boolean isRollback = false;
try { try {
success = supplier.get(); result = supplier.get();
} catch (Throwable e) { } catch (Throwable e) {
rollbacked = true; isRollback = true;
rollback(xid); rollback(xid);
throw new TransactionException(e.getMessage(), e); throw new TransactionException(e.getMessage(), e);
} finally { } finally {
if (success != null && success) { if (!isRollback) {
if (!withResult) {
if (result instanceof Boolean && (Boolean) result) {
commit(xid); commit(xid);
} else if (!rollbacked) { }
//null or false
else {
rollback(xid); rollback(xid);
} }
} else {
commit(xid);
} }
return success; }
}
return result;
} }

View File

@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
/** /**
* @author 王帅 * @author 王帅
@ -32,9 +33,12 @@ class ReflectTest {
@Test @Test
void test() { void test() {
Field field = ClassUtil.getAllFields(Account.class, f -> f.getName().equals("list")).get(0); List<Field> allFields = ClassUtil.getAllFields(Account.class);
for (Field field : allFields) {
Type type = TypeParameterResolver.resolveFieldType(field, Account.class); Type type = TypeParameterResolver.resolveFieldType(field, Account.class);
System.out.println(type); System.out.println("field: " + field+"----->Type:" + type);
}
} }
} }