加入事物都成功则成功,其中一个环节失败则回滚
@Autowired
private UserTestService userTestService;
@Autowired
private UserTestItemService userTestItemService;
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
@RequestMapping(value = "/save1/{size1}")
public String save(@PathVariable("size1") Integer size1) throws ExecutionException, InterruptedException {
List<TransactionStatus> statusList = new ArrayList<>();
long startTime = System.currentTimeMillis();
log.info("开始时间[{}]", startTime);
List<UserTest> userTests = createDate(size1);
int nThreads = 10;
final int size = userTests.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<CompletableFuture<Integer>> futures = new ArrayList<>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List<UserTest> allusersEntityImportList = userTests.subList(size / nThreads * i, size / nThreads * (i + 1));
CompletableFuture<Integer> exceptionally = CompletableFuture.supplyAsync(() -> {
statusList.add(platformTransactionManager.getTransaction(new DefaultTransactionDefinition(3)));
log.info("主表线程名称是=[{}],线程id=[{}]",Thread.currentThread().getName(),Thread.currentThread().getId());
userTestService.save(allusersEntityImportList);
return allusersEntityImportList;
}).thenApply(list -> {
try {
int a=10/0;
log.info("从表线程名称是=[{}],线程id=[{}]",Thread.currentThread().getName(),Thread.currentThread().getId());
List<UserTestItem> dateItem = createDateItem(list);
int save = userTestItemService.save(dateItem);
return save;
}catch (Exception e){
throw new NullPointerException(e.getMessage());
}
}).exceptionally(error -> {
System.out.println("发生了异常信息");
throw new NullPointerException(error.getMessage());
});
futures.add(exceptionally);
}
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
}catch (Exception e){
log.error("1111111");
statusList.forEach(i->platformTransactionManager.rollback(i));
}
statusList.forEach(i->platformTransactionManager.commit(i));
executorService.shutdown();
long endTime = System.currentTimeMillis();
log.info("结束时间[{}]", startTime - endTime);
return "2002";
}
**单个CompletableFuture controll代码 注意事务注解Transactional要写在servicce 写在controller会失效 **
@RequestMapping(value = "/save2/{size2}")
public String save(@PathVariable("size2") Integer size2) {
long startTime = System.currentTimeMillis();
log.info("开始时间[{}]", startTime);
List<UserTest> userTests = createDate(size2);
int nThreads = 10;
final int size = userTests.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<CompletableFuture<Integer>> futures = new ArrayList<>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List<UserTest> allusersEntityImportList = userTests.subList(size / nThreads * i, size / nThreads * (i + 1));
CompletableFuture<Integer> exceptionally = CompletableFuture.supplyAsync(() -> {
log.info("主表线程名称是=[{}],线程id=[{}]", Thread.currentThread().getName(), Thread.currentThread().getId());
userTestService.save(allusersEntityImportList);
return 1;
}).exceptionally(error -> {
System.out.println("发生了异常信息[]===");
throw new NullPointerException(error.getMessage());
});
futures.add(exceptionally);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
executorService.shutdown();
long endTime = System.currentTimeMillis();
log.info("结束时间[{}]", startTime - endTime);
return "2002";
}
service代码
@Transactional(rollbackFor = Exception.class)
@Override
public List<UserTest> save(List<UserTest> userTests) {
userTestMapper.insertList(userTests);
List<UserTestItem> dateItem = createDateItem(userTests);
if (Thread.currentThread().getId()==90){
int a=10/0;
}
userTestItemService.save(dateItem);
return null;
}
|