浏览代码

jfinal 4.9

James 5 年之前
父节点
当前提交
4a7a13b4c8
共有 1 个文件被更改,包括 32 次插入4 次删除
  1. 32 4
      src/main/java/com/jfinal/plugin/activerecord/DbPro.java

+ 32 - 4
src/main/java/com/jfinal/plugin/activerecord/DbPro.java

@@ -795,10 +795,6 @@ public class DbPro {
 		}
 	}
 	
-	public boolean tx(int transactionLevel, IAtom atom) {
-		return tx(config, transactionLevel, atom);
-	}
-	
 	/**
 	 * Execute transaction with default transaction level.
 	 * @see #tx(int, IAtom)
@@ -807,6 +803,38 @@ public class DbPro {
 		return tx(config, config.getTransactionLevel(), atom);
 	}
 	
+	public boolean tx(int transactionLevel, IAtom atom) {
+		return tx(config, transactionLevel, atom);
+	}
+	
+	/**
+	 * 主要用于嵌套事务场景
+	 * 
+	 * 实例:https://jfinal.com/feedback/4008
+	 * 
+	 * 默认情况下嵌套事务会被合并成为一个事务,那么内层与外层任何地方回滚事务
+	 * 所有嵌套层都将回滚事务,也就是说嵌套事务无法独立提交与回滚
+	 * 
+	 * 使用 txInNewThread(...) 方法可以实现层之间的事务控制的独立性
+	 * 由于事务处理是将 Connection 绑定到线程上的,所以 txInNewThread(...)
+	 * 通过建立新线程来实现嵌套事务的独立控制
+	 */
+	public void txInNewThread(IAtom atom) {
+		Thread thread = new Thread(() -> {
+			tx(config, config.getTransactionLevel(), atom);
+		});
+		thread.setDaemon(true);
+		thread.start();
+	}
+	
+	public void txInNewThread(int transactionLevel, IAtom atom) {
+		Thread thread = new Thread(() -> {
+			tx(config, transactionLevel, atom);
+		});
+		thread.setDaemon(true);
+		thread.start();
+	}
+	
 	/**
 	 * Find Record by cache.
 	 * @see #find(String, Object...)