JdbcTemplate spring 事务控制

               

编程式事务:

public static PlatformTransactionManager getTransactionManager() {  return transactionManager; } @Resource(name="dsTransactionManager"public void setTransactionManager(PlatformTransactionManager transactionManager) {  ContractManageServiceImpl.transactionManager = transactionManager; }public boolean save(String reason,String attach,String operator,String mainId,String process,String incident){  HtxxStop bo = new HtxxStop();  boolean flag = false;  try{   final CMInstance instance = new CMInstance(reason,attach,operator,mainId,process,incident,bo.getOperateTime(),bo.getRemoved());      TransactionTemplate tt = new TransactionTemplate(transactionManager);   flag = tt.execute(new TransactionCallback<Boolean>() {    boolean flag = true;    public Boolean doInTransaction(TransactionStatus status) {     try{      JdbcTemplate jt = dbUtil.getJdbcTemplate();      instance.action(jt);     }catch(Exception e){      e.printStackTrace();      status.setRollbackOnly();      flag = false;     }     return flag;    }   });    }catch(Exception e){     }  return flag; }


 /**  */package com.wonders.stpt.contractManage.instance;import org.springframework.jdbc.core.JdbcTemplate;/**  * @ClassName: CMInstance  * @Description: TODO(这里用一句话描述这个类的作用)  * @author zhoushun  * @date 2013-6-13 上午11:03:13  *   */public class CMInstance private String reason, attach, operator, mainId, process, incident,operateTime,removed; private static final String insertSql = "insert into ht_xx_stop t " +   "(t.id,t.reason,t.attach,t.operator,t.operate_time,t.main_id,t.removed)values(sys_guid(),?,?,?,?,?,?)"private static final String updateSql1 = "update ht_xx h set  h.contract_name =  h.contract_name || '<font color=''red''>(此合同异常终止)</font>' , h.flag='99',h.removed=1 where h.id = ?"private static final String updateSql2 = "update incidents i set i.processname=? ,i.status = '33' where i.processname = ?  and i.incident = ?";  public CMInstance(String reason,String attach,String operator,String mainId,String process,String incident,String operateTime,String removed){  this.reason = reason;  this.attach = attach;  this.operator = operator;  this.mainId = mainId;  this.process = process;  this.incident = incident;  this.operateTime = operateTime;  this.removed = removed; }  public void action(JdbcTemplate jt) throws Exception{  int n0 = jt.update(insertSql,new Object[]{reason,attach,operator,operateTime,mainId,removed});  int n1 = jt.update(updateSql1,new Object[]{mainId});  int n2 = jt.update(updateSql2,new Object[]{process+"(删除)",process,incident});  if(n0 > 0 && n1 > 0 && n2>0){  }else{   throw new Exception("update failed");  } }}

声明式事务

@Transactional(value = "stfb-txManager",propagation=Propagation.REQUIRED,rollbackFor=Exception.class)@Service("coccMain")

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值