flyingfox
flyingfox

Reputation: 13506

How to commit or rollback transaction in SpringMVC controller method

I am modify some old code and met a weird problme:

I need to commit or rollback the transaction based on the service method return result in my controller method manually. Below is the code that I want to archive.Is it possible to do it?

Note: I know the most suitable way is to do some modification in the service code and let the service code determine rollback or commit.But due to some reasons I can't modify the service code,I can only modify the controller code.

@RequestMapping("processData")
public void processData(String id){
    String result = dataService(id);
    if("success".equals(result)){
        //commit transaction
    }else{
       //rollback transaction
    }
   //since there some other codes in controller method,so exception might not be a good choice
   //.....
}

Upvotes: 0

Views: 528

Answers (1)

qdongxu
qdongxu

Reputation: 414

use @Transactional(rollbackFor=Exception.class) , and there's some more attributes, you can check spring documents.

Upvotes: 1

Related Questions