gpt4 book ai didi

java - Spring + hibernate : manually commit the transaction at end

转载 作者:行者123 更新时间:2023-12-01 18:51:35 27 4
gpt4 key购买 nike

我正在使用 spring mvc 和 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";
}



}

当所有事务保存成功时,我如何手动提交事务,如果任何事务插入失败,则回滚catch block 中的所有事务。 ?

我正在使用 spring mvc 和 hibernate

最佳答案

我更愿意在某些服务中创建一个单独的方法(结合您的 2 个方法)来处理那里的所有必要事务。

关于java - Spring + hibernate : manually commit the transaction at end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com