【发布时间】:2019-03-05 11:27:45
【问题描述】:
我在春季编写了运行良好的休息服务。
现在,我需要在向用户返回响应之前添加执行一些数据库事务。
此数据库事务独立于检索到的响应。
例如,
@PostMapping("login")
public TransactionResponse loginAuthentication(@Valid @RequestBody LoginRequestBody loginRequest) {
TransactionResponse transactionResponse = new TransactionResponse();
try {
transactionResponse = loginService.validateUser(loginRequest);
//independent transaction needs to be executed in a separate thread
loginSerice.addLoginLog(transactionResponse);
//return below response without waiting to compelete above log transaction
return transactionResponse;
}
catch (Exception e) {
return CommonUtils.setErrorResponse(transactionResponse, e);
}
}
我在 spring mvc link 中阅读了异步控制器。虽然控制器 在单独的线程中执行相应的功能,但我不想等待数据库事务完成。 从服务层得到响应后,应立即转发给用户。
任何建议!
Spring 版本是 4.3
【问题讨论】:
-
看看spring的@Async注解
标签: spring asynchronous callable