mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
chore(sql): 提供当前事务实现思路
This commit is contained in:
@@ -138,7 +138,7 @@ public class EasySQLDemo {
|
||||
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
||||
int id = sqlManager.createInsert("users")
|
||||
.columns("username", "phone", "email", "registerTime")
|
||||
.params("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.values("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.returnGeneratedKey() // 设定在后续返回自增主键
|
||||
.execute((exception, action) -> {
|
||||
// 处理异常
|
||||
@@ -149,7 +149,7 @@ public class EasySQLDemo {
|
||||
try {
|
||||
int userID = sqlManager.createInsert("users")
|
||||
.columns("username", "phone", "email", "registerTime")
|
||||
.params("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.values("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.returnGeneratedKey().execute();
|
||||
|
||||
System.out.println("新用户的ID为 " + userID);
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package cc.carm.lib.easysql;
|
||||
|
||||
import cc.carm.lib.easysql.api.action.SQLTransaction;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface DemoTransaction {
|
||||
|
||||
SQLTransactionResult commitTransaction(Consumer<SQLTransaction> consumer);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package cc.carm.lib.easysql;
|
||||
|
||||
import cc.carm.lib.easysql.api.transaction.SQLSavepoint;
|
||||
import cc.carm.lib.easysql.api.transaction.SQLTransaction;
|
||||
|
||||
public class TransactionTest {
|
||||
|
||||
|
||||
public void demo() {
|
||||
|
||||
|
||||
try (SQLTransaction transaction = createTransaction()) {
|
||||
|
||||
transaction.updateInto("A")
|
||||
.set("name", "CARM")
|
||||
.addCondition("name", "CUMR")
|
||||
.build().execute();
|
||||
|
||||
SQLSavepoint pointA = transaction.savepoint("TEST");
|
||||
|
||||
transaction.insertInto("A")
|
||||
.columns("name")
|
||||
.values("TEST")
|
||||
.execute();
|
||||
|
||||
try {
|
||||
transaction.commit(); // 提交
|
||||
} catch (Exception ex) {
|
||||
transaction.rollback(pointA); // 出错回滚到pointA
|
||||
transaction.commit(); // 提交出错前的内容
|
||||
}
|
||||
|
||||
|
||||
pointA.release(); // release savepoint (结束后也会被自动释放)
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected SQLTransaction createTransaction() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user