How can I set status in DB to "in progress" while executing the method, and set it to "success" once the method is executed?

Viewed 25

I want to enter "in progress" into database and roll it back to it's earlier state if there is some exception. Currently, I'm trying to do it by using thread in java. But it's not rolling back if there's an Exception. Here is the code,

ClientConfig clientConfig = clientConfigRepository.getOne(clientId.longValue());
Thread newThread = new Thread(() -> {
            clientConfig.setDeploymentStatus("in progress");
            clientConfigRepository.save(clientConfig);
        });
        newThread.start();
some code....
some code...
if(true){
            clientConfig.setDeploymentStatus("deployed");
            clientConfig.setLastDeployedDate(new Date());
            clientConfigRepository.save(clientConfig);
            return new ResponseEntity(restResponse.createSuccessIdResponse(
                    " Successfully completed Deployment of Configs. ", clientId, ""), HttpStatus.OK);
        }else
        {
            clientConfig.setDeploymentStatus("deployment failed");
            clientConfigRepository.save(clientConfig);
            return new ResponseEntity(restResponse.commonErrorResponse(
                    " Deploy Config implementation failed ", "{}", HttpStatus.INTERNAL_SERVER_ERROR.toString()), HttpStatus.INTERNAL_SERVER_ERROR);
        }

the above code is working fine if there's no exception. But if there is, DB is not rolling back to earlier state, But I can't have "In progress" in the DB if anything goes wrong. Would appreciate any help!

0 Answers
Related