Search code examples
javaspringhibernateautocommit

spring + hibernate: manually commit the transaction at end


I am using spring mvc and hibernate

@Controller
public class COACategoriesController {

protected static Logger log = Logger.getLogger(COACategoriesController.class);


@Resource(name="COACategoriesService")
private COACategoriesService obj_coacs;
@Resource(name="COAMaintenanceService")
private COAMaintenanceService obj_coams;

 @RequestMapping(value = "/addCoaCategory", method = RequestMethod.POST)
 public String addCoaCategory(@RequestParam("conCatName") String coaCatName, Model model) {

     Date sysdate = null;
     String Message="";
     try{

     sysdate = new Date();


     COACategoriesModel model1 = new COACategoriesModel( coaCatName, 1, "", sysdate , 0);

     COAMaintenanceModel account =  new COAMaintenanceModel();
        account.setDiscription("Test Description");
        account.setCategoryId(model1);

        Message="Fail-First";
        obj_coacs.AddCOACategories(model1);


        Message="Fail-Second";
        obj_coams.AddCOAMaintenance (account);


        Message="Add Successfully";
     }catch(Exception ex){
         log.error("Exception.."+ex);
         model.addAttribute("success", Message);
     }



        return "fin/category";
    }



}

How I commit the transaction manually as all the transactions save successfully, if any transaction fails to insert , rollback all the transactions in catch block. ?

I am using spring mvc and hibernate


Solution

  • I would prefer to create a separate method (combining your 2 methods) in some service to handle all necessary transactions there.