我有2个DAO分别是 IndepotOrderDao和 InDepotRecordDao 都继承JdbcDaoSupport  public class IndepotOrderDaoImpl extends JdbcDaoSupport{  saveInDepotOrder(IndepotOrder indepotOrder ){
   sql="insert into indepotOrder(name) value(?)";
   Object[] args={"orderName"};
   this.getJdbcTemplate().update(sql, args);
  }
  
  updateInDepotOrder(IndepotOrder indepotOrder ){
   sql="update indepotOrder set name=?";
   Object[] args={indepotOrder.getName()};
   this.getJdbcTemplate().update(sql, args);
  }
  
}public class InDepotRecordDaoImpl extends JdbcDaoSupport{
  
  saveInDepotRecord(InDepotRecord inDepotRecord ){
   sql="insert into inDepotRecord(name) value(?)";
   Object[] args={inDepotRecord.getName()};
   this.getJdbcTemplate().update(sql, args);
  }
  
  updateInDepotRecord(IndepotOrder inDepotRecord ){
   sql="update inDepotRecord set name=?";
   Object[] args={inDepotRecord.getName()};
   this.getJdbcTemplate().update(sql, args);
  }
}
Service调用这两个daopublic Class IndepotServiceImp{
   
  @Resource
  private IndepotOrderDao indepotOrderDao; 
  @Resource
  private InDepotRecordDao inDepotRecordDao; 
  
  //方法1
  @Transactional
  public void add(IndepotOrder indepotOrder,InDepotRecord inDepotRecord){
  indepotOrderDao.saveInDepotOrder(indepotOrder);//(1)
  inDepotRecordDao.saveInDepotRecord(inDepotRecord);//(2)
  }  //方法2
  inDepotRecord void update(IndepotOrder indepotOrder,InDepotRecord inDepotRecord){    indepotOrderDao.updateInDepotOrder(indepotOrder);//(1)
    inDepotRecordDao.updateInDepotRecord(inDepotRecord);//(2)
  }}
spring 事务管理器配置如下<!-- 配置dataSource事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注释扫描事务功能 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
方法1中的 (1)(2)任意一个发生异常数据库都不会有记录插入而且异常会打印到web页面中方法2中 如果(1)发生异常 (2)还会执行并把数据库中的记录更新了异常打印在控制台里  请教:如何让方法2也像方法一那样呢?(1)(2)中有一个发生异常就都不执行
  难道spring对数据库update 和 insert 处理级别不一样  

解决方案 »

  1.   


      //方法1
      @Transactional
      public void add(IndepotOrder indepotOrder,InDepotRecord inDepotRecord){
      indepotOrderDao.saveInDepotOrder(indepotOrder);//(1)
      inDepotRecordDao.saveInDepotRecord(inDepotRecord);//(2)
      }  //方法2
      这里不用写注解吗?
      inDepotRecord void update(IndepotOrder indepotOrder,InDepotRecord inDepotRecord){    indepotOrderDao.updateInDepotOrder(indepotOrder);//(1)
        inDepotRecordDao.updateInDepotRecord(inDepotRecord);//(2)
      }
    注解的事务 我没用过 不过看你写的 2个方法也不一样呀?
      

  2.   

    我看几个方法的最大区别也是sql变量内容不同,我对spring也不熟,我觉得spring不太可能对这方面会有所区别对待
    还是要跟进查清异常发生的地点,不同原因导致的异常可能最终表现也不尽相同吧
      

  3.   

    异常抛出的都是 org.springframework.dao.DataIntegrityViolationException的时候 执行insert 异常直接抛到 页面上去执行 update异常在控制台里被抛出
      

  4.   

    如果是unchecked异常, 自动回滚. 如果要回滚checked异常, @Transactional(rollbackFor = {CheckedException.class}).Spring文档有详细说明
      

  5.   

    DataIntegrityViolationException 异常的父类就是 runtimeException 是unchecked 类型的
    这个我加了不好用
    @Transactional(rollbackFor = Exception.class)我感觉spring容器 
    update(){
        update1();
        update2();
    }
    展开后为 update(){
         try{ 
             update1();
             }
         catch(Exception e){
            rollback();
           e.print();
          }finally{.....}      try{ 
             update2();
             }
         catch(Exception e){
           rollback();
           e.print().
          }finally{.....}} 如果要是这样 update(){
         try{ 
             update1();
             update2();
             }
         catch(Exception e){
           rollback();
           e.print();
          }finally{.....}} 就应该没问题 
      

  6.   

    我感觉这2个update方法根本就没在一个事务中
      

  7.   

    要不你别用注解了 在service层加上事务就行了
      

  8.   

    第二个方法上面有  @Transactional吗?看看