Spring-Service-事务中线程异常执行事务回滚的方式
2017-08-15 14:48
756 查看
方式一: 使用Callable, 利用Callable的返回值判断是否需要进行事务回滚
ExecutorService service = Executors.newCachedThreadPool(); Future<Integer> submit = service.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { System.out.println("bla bla ..."); return 5 * 3; } }); try { if (submit.get() == 15) { throw new RunTimeException("操作失败!"); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }
方式二: 使用FutureTask
Callable<Integer> integerCallable = new Callable<Integer>() { @Override public Integer call() throws Exception { return 10; } }; ExecutorService executor = Executors.newCachedThreadPool(); FutureTask<Integer> futureTask = new FutureTask<>(integerCallable); try { Object o = executor.submit(futureTask).get(); throw new RuntimeException(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executor.shutdown();
相关文章推荐
- Spring配置事务 service 异常捕获回滚问题
- Spring学习笔记——Spring事务仅仅对执行时异常回滚
- spring事务,service中捕获异常不抛出,不回滚
- Spring配置事务 service 异常捕获回滚问题
- spring 回滚事务 异常处理 手动回滚 公司通用处理方式
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring管理事务默认回滚的异常是什么?
- spring事务控制的方法中抛出异常不回滚
- 【转】Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务管理只对出现运行期异常进行回滚
- spring的service中抛出异常后不回滚?
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- spring事物回滚机制 (事务异常回滚,捕获异常不抛出就不会回滚)
- Spring事务异常回滚,try catch 捕获异常不回滚
- Spring管理事务默认回滚的异常是什么?
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚 unchecked和checked