因为在spring中,事务是自动处理的,在过程中不能使用rollback,commit等语句。
现在将如何手动处理,介绍如下:
一,在service层,
1.首先引入从spring中的事务处理器,这个是从xml文件中定义的事务管理器名称
@Autowired private DataSourceTransactionManager transactionManager;
2.在service层加入如下代码
public int addZhouyiYao(ZhouyiYao zhouyiYao) throws Exception { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); // explicitly setting the transaction name is something that can be done only programmatically def.setName("SomeTxName"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = transactionManager.getTransaction(def); try { // execute your business logic here zhouyiYaoMapper.addYao(zhouyiYao); zhouyiYaoMapper.updateYaoOrder(zhouyiYao.getId(), zhouyiYao.getId()*10); //为了保证这两个处理语句的一致性 } catch (Exception ex) { transactionManager.rollback(status); throw ex; } transactionManager.commit(status); return zhouyiYao.getId(); }
二、在controller层,因为有异常抛出,所以要处理一下
int addZhouyiYao = 0 ; try { addZhouyiYao = zhouyiYaoService.addZhouyiYao(zhouyiYao); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); errors.addError(new FieldError("zhouyiYao", "yaoId", "*事务一致性处理错误")); }
controller后面的代码是
if (errors.getErrorCount() > 0) { mv.addObject("zhouyiIndex", zhouyiIndexService.getZhouyiIndexs()); //预先加载的本页的其他类别值 mv.setViewName("admin116/zhouyiYaoAdd"); for (FieldError err : errors.getFieldErrors()) { System.out.println("getField:== " + err.getField() + " getDefaultMessage:== " + err.getDefaultMessage() + " getObjectName:== " + err.getObjectName() + " getCode:== " + err.getCode()); } return mv; } mv.setViewName("redirect:/admin116/getYaos?pn="+addZhouyiYao); return mv;
这样,就可以有错误提示了。
本段代码主要从spring的官方文档中提出。
=================================================
另一分不带返回值的代码示例
service部分
public void addZhouyiContent(ZhouyiContent zhouyiContent) throws Exception { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); // explicitly setting the transaction name is something that can be done only programmatically def.setName("SomeTxName"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = transactionManager.getTransaction(def); try { // execute your business logic here zhouyiContentMapper.addZhouyiContent(zhouyiContent); zhouyiContentMapper.updateZhouyiContentOrder(zhouyiContent.getId(), zhouyiContent.getId()*10); } catch (Exception ex) { transactionManager.rollback(status); throw ex; //ex.printStackTrace(); } transactionManager.commit(status); }
controller部分
@RequestMapping("/zhouyiContentSave") public ModelAndView zhouyiContentSave(@Valid ZhouyiContent zhouyiContent,BindingResult errors,@SessionAttribute("authorId") Integer authorId) { ModelAndView mv = new ModelAndView(); try { zhouyiContentService.addZhouyiContent(zhouyiContent); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); errors.addError(new FieldError("zhouyiContent", "info", "*事务一致性处理错误")); } if(errors.getErrorCount()>0) { mv.addObject("tips", null); mv.addObject("errors", "*添加保存异常!"); mv.addObject("zhouyiAuthor", zhouyiAuthorService.getZhouyiContentAuthorById(authorId)); mv.addObject("zhouyiYao",zhouyiYaoService.getZhouyiYaos() ); mv.setViewName("admin116/zhouyiContentAdd"); return mv; } mv.addObject("tips", zhouyiContent.getId()+"条,添加成功!"); mv.setViewName("forward:/admin116/zhouyiContentAdd"); return mv; }