public class TransactionUtils extends Object
| 构造器和说明 |
|---|
TransactionUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
assertHasAffectedRow(int totalAffectedRow,
String errorMsg) |
static void |
assertHasAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier) |
static void |
assertNotAffectedRow(int totalAffectedRow,
String errorMsg) |
static void |
assertNotAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier) |
static void |
assertOneAffectedRow(int totalAffectedRow,
String errorMsg) |
static void |
assertOneAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier) |
static void |
assertWithinTransaction() |
static void |
assertWithoutTransaction() |
static void |
doAfterTransactionCommit(Collection<Runnable> actions) |
static void |
doAfterTransactionCommit(Runnable action)
在事务提交成功后再执行`action`,注意:
1)在单层事务中,`doAfterTransactionCommit`添加了一些`action`,后面的代码因异常回滚了这个事务,因事务没有提交(已回滚),这些`action`不会执行
2)在嵌套事务中,`doAfterTransactionCommit`添加了一些`action`,后面在嵌套事务内因异常回滚了嵌套事务,外层的事务提交成功后,这些`action`仍会执行
3)在嵌套事务中,`doAfterTransactionCommit`添加了一些`action`,后面在外层事务内因异常回滚了整个事务(嵌套和外层都会回滚),这些`action`不会执行
|
static <R> R |
doInNestedTransaction(org.springframework.transaction.support.TransactionTemplate transactionTemplate,
Throwables.ThrowingSupplier<R,Throwable> action,
Consumer<Throwable> errorHandler)
如果当前存在事务则开启一个嵌套事务,如果当前不存在事务则新建一个事务并运行。
|
static <R> R |
doInRequiresNewTransaction(org.springframework.transaction.PlatformTransactionManager txManager,
Throwables.ThrowingSupplier<R,Throwable> action,
Consumer<Throwable> errorHandler)
创建一个新事务,如果当前存在事务,则将这个事务挂起。
|
static boolean |
hasAffectedRow(int totalAffectedRow) |
static boolean |
isCurrentDoAfterCommit() |
static boolean |
isNotAffectedRow(int totalAffectedRow) |
static boolean |
isOneAffectedRow(int totalAffectedRow) |
static boolean |
isWithinTransaction() |
static boolean |
isWithoutTransaction() |
public static boolean isNotAffectedRow(int totalAffectedRow)
public static boolean isOneAffectedRow(int totalAffectedRow)
public static boolean hasAffectedRow(int totalAffectedRow)
public static void assertNotAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier)
public static void assertNotAffectedRow(int totalAffectedRow,
String errorMsg)
public static void assertOneAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier)
public static void assertOneAffectedRow(int totalAffectedRow,
String errorMsg)
public static void assertHasAffectedRow(int totalAffectedRow,
Supplier<String> errorMsgSupplier)
public static void assertHasAffectedRow(int totalAffectedRow,
String errorMsg)
public static void doAfterTransactionCommit(Collection<Runnable> actions)
public static void doAfterTransactionCommit(Runnable action)
在事务提交成功后再执行`action`,注意: 1)在单层事务中,`doAfterTransactionCommit`添加了一些`action`,后面的代码因异常回滚了这个事务,因事务没有提交(已回滚),这些`action`不会执行 2)在嵌套事务中,`doAfterTransactionCommit`添加了一些`action`,后面在嵌套事务内因异常回滚了嵌套事务,外层的事务提交成功后,这些`action`仍会执行 3)在嵌套事务中,`doAfterTransactionCommit`添加了一些`action`,后面在外层事务内因异常回滚了整个事务(嵌套和外层都会回滚),这些`action`不会执行
action - the action codepublic static boolean isCurrentDoAfterCommit()
public static boolean isWithinTransaction()
public static boolean isWithoutTransaction()
public static void assertWithinTransaction()
public static void assertWithoutTransaction()
public static <R> R doInRequiresNewTransaction(org.springframework.transaction.PlatformTransactionManager txManager,
Throwables.ThrowingSupplier<R,Throwable> action,
Consumer<Throwable> errorHandler)
内部事务与外部事务相互独立,互不依赖,互不影响。
txManager - the txManageraction - the actionerrorHandler - the error handlerpublic static <R> R doInNestedTransaction(org.springframework.transaction.support.TransactionTemplate transactionTemplate,
Throwables.ThrowingSupplier<R,Throwable> action,
Consumer<Throwable> errorHandler)
如果当前存在事务则开启一个嵌套事务,如果当前不存在事务则新建一个事务并运行。 1)内部事务为外部事务的一个子事务。 2)内部事务的提交/回滚不影响外部事务的提交/回滚 3)内部事务的提交/回滚最终依赖外部事务的提交/回滚。
transactionTemplate - the transaction templateaction - the actionerrorHandler - the error handler, execute after transaction rollbackCopyright © 2025. All rights reserved.