子线程异常抛出 及 主线程事务回滚

http://blog.csdn.net/mynameismt/article/details/51363120

多线程与事务回滚


业务模型期望的结果是这样的,检测子线程的异常情况,如果发生异常,主线程回滚,否则提交

大家都知道runnable有以下特点:

          1.业务处理出现checked exception必须在线程中捕获处理不允许抛出,否则影响run函数的覆盖

          2.如果线程抛出unchecked(runnable) exception,则线程终结,主线程不受影响

所以使用runnable,主线程压根不知道子线程的情况,事务更无从谈起。


所以使用Callable机制

两种方法

1 使用异常

FutureTask   

public V get() throws InterruptedException, ExecutionException

主线程    调用  get   直接抛出异常触发事务管理


2 使用返回

  1. if ("failed".equals(result)) {  
  2.     throw new RuntimeException();  
  3. }  
收到failed返回后,抛出异常触发事务管理
原文地址:https://www.cnblogs.com/silyvin/p/9106646.html