Spring Data jpa,使用多个表进行事务管理

时间:2017-12-12 13:03:36

标签: java spring spring-boot spring-data-jpa

@Override
@Transactional
public AgentRegistrationWrapper register(Argument1 arg,Argument2 arg2) {
     addressProofService.save(addressProofInformation);
     contactService.save(contactInformation);
     int i = 1 /0; //Force fully send arithmatic Exception
     panCardService.save(panCardInformation);
}
@Transactional
public ContactInformation save(ContactInformation information) {

    ContactInformation contactInformation = contactRepository.save(information);
    return contactInformation;
}
@Override
@Transactional
public PANCardInformation save(PANCardInformation information)  {

    String fileName=fileUpalodService.uploadAddressProof(information.getPanCopyFile());
    if(fileName != null)
        information.setPanDocumentProof(fileName);
    return reposetory.save(information);
}
@Override
@Transactional
public DepositInformation save(DepositInformation depositInformation) {
    return reposetory.save(depositInformation);
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ResponseEntity<AgentRegistrationWrapper> registerAgent(
        @RequestParam(value="pan_scan_copy",required=false) MultipartFile panCopy,
        @RequestParam("agentInformation") String agentRegistrationStr,
        @RequestParam(value="address_proof",required=false) MultipartFile addressCopy,
        @RequestParam(value="nope_proof",required=false) MultipartFile nopeProofCopy )
        throws Exception  {


    ObjectMapper mapper = new ObjectMapper();
    AgentRegistration agentRegistration = mapper.readValue(agentRegistrationStr, AgentRegistration.class);
    AgentRegistrationWrapper checkAgentData = null;



    if (validate(agentRegistration))
        checkAgentData = agentRegistrationService
                .findByEmail(agentRegistration.getContactInformation().getEmail().toString().trim());//It is checking in database data exist or not ,if not it will register
    else
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

    if (checkAgentData == null) {

        AgentRegistrationWrapper agentRegistrationWrapper2 = agentRegistrationService.register(agentRegistration,
                panCopy, addressCopy,nopeProofCopy);

        if (agentRegistrationWrapper2 == null) {
            logger.error("No data found", agentRegistrationWrapper2);
            return new ResponseEntity<AgentRegistrationWrapper>(agentRegistrationWrapper2, HttpStatus.NO_CONTENT);
        } else {
            logger.debug("Inside the error");
            agentRegistrationWrapper2.getAgentRegistrations().getPanCardInformation().setPanCopyFile(null);
            //agentRegistrationWrapper2.getAgentRegistrations().getAddressProofInformation().setAddressCopyFile(null);
            return new ResponseEntity<AgentRegistrationWrapper>(agentRegistrationWrapper2, HttpStatus.OK);
        }
    } else {
        System.out.println(checkAgentData);
        checkAgentData.getAgentRegistrations().getPanCardInformation().setPanCopyFile(null);
        return new ResponseEntity<AgentRegistrationWrapper>(checkAgentData, HttpStatus.ALREADY_REPORTED);
    }

}

我有四个服务,那个寄存器是外部服务我在寄存器服务中调用三个服务我强制完全抛出算术异常 但是数据库没有回滚我已经使用了spring数据jpa repository。如果任何服务在其他数据库数据之间失败应该是rollback.And重要的是我有三个不同的表。

0 个答案:

没有答案