Search code examples
springmultithreadingspring-transactionsspring-annotationsxml-configuration

Spring @Async annotation


I have a question about Spring @Async annotation. I have a controller autowired a service(GnInsuranceDetailsService)

@RequestMapping(value="/agn/AP0W01A_010/insertDetail.do")
public ResponseEntity<?> insertDetail(@ModelAttribute("dto")GnInsuranceDetailsDTO dto,HttpSession session) throws Exception {
    gnInsuranceDetailsDTO.setBkFlag(getAgnUserInfo(session).getBkFlag());
    gnInsuranceDetailsService.insertGnInsuranceDetail(dto);//open another thread to insert data
    MessageDTO dto = new MessageDTO(AgnConstant.INSERT_SUCCESS);
    return new ResponseEntity<MessageDTO>(dto,HttpStatus.OK);// increase return time for client
    }

And the Service insertGnInsuranceDetail method I declare @Async up method.

@Transactional(readOnly = false)
@Async
public void insertGnInsuranceDetail(GnInsuranceDetailsDTO gnInsuranceDetailsDTO) throws Exception{
GnInsuranceDetails entity = gnInsuranceDetailsDTO.convert();
gnInsuranceDetailsDAO.save(detailsEntity);
}

I put the @Async for the service method to increase controller response time for client side,but it does not work as I think. Do I lose somethings?Or How can I modify in the easiest way to use?


Solution

  • You would not loose anything, when you put @Async in the method service will be executed in a different thread, Controllers insertDetail method will not be returned until your insertGnInsuranceDetail is returned or thrown any exception.