|
@@ -0,0 +1,26 @@
|
|
|
+package com.sd.framework.util;
|
|
|
+
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
+import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
|
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
+
|
|
|
+public class TransactionUtil {
|
|
|
+
|
|
|
+ private final static TransactionTemplate transactionTemplate = SpringUtil.getBean(TransactionTemplate.class);
|
|
|
+
|
|
|
+ public static void execute(Runnable runnable) {
|
|
|
+ transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
|
|
+ @Override
|
|
|
+ protected void doInTransactionWithoutResult(@NotNull TransactionStatus status) {
|
|
|
+ try {
|
|
|
+ runnable.run();
|
|
|
+ } catch (Exception e) {
|
|
|
+ status.setRollbackOnly();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|