即使在回滚后,Spring声明事务回滚也会失败

时间:2015-01-14 14:56:08

标签: java spring transactions

问题是甚至在说明为Exception.class回滚后仍然没有事务 rollbacked。

1.我的数据源

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <beans:property name="url" value="jdbc:mysql://localhost:3306/salesforce" />
              <beans:property name="username" value="root" />
            <beans:property name="password" value="root" />
            <beans:property name="defaultAutoCommit" value="false"/>
        </beans:bean>
  1. 交易管理器

                                                              org.hibernate.dialect.MySQLDialect                   20                   真正                   更新                           

    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>
    
  2. 和服务层的声明性交易

    @Transactional(传播= Propagation.REQUIRED,的rollbackFor = Exception.class)         public void saveEmployee(Long roleId,Long divId,Long areaId,Employee emp){             // TODO自动生成的方法存根             employeeDao.saveEmployee(roleId,divId,areaId,emp);         }

  3. 保存员工后,我尝试更新字段,一旦我得到空指针 异常认为它是滚动支持但不是方法是:

    public void saveEmployee(Long roleId,Long divId,Long areaId,Employee emp){     会话session = sessionFactory.getCurrentSession();     EmployeeRole empRole = null;     除法div = null;     面积= null;     员工cord = null;     String materialPath = null;

    try{
        empRole = (EmployeeRole) session.get(EmployeeRole.class, roleId);
        div = (Division) session.get(Division.class, divId);
        area = (Area) session.get(Area.class, areaId);
        emp.setArea(area); 
        emp.setDivision(div);
        emp.setEmployeeRole(empRole);
        long employId = (Long) session.save(emp);
    
        cord = (Employee) session.get(Employee.class, emp.getEmployeeCoordinaterId());
        materialPath = cord.getMaterialPath()+"."+employId;       
        emp.setMaterialPath(materialPath);
        emp.setEmployeeId(employId);
        session.saveOrUpdate(emp);      
    }
    catch(Exception e){
        e.printStackTrace();
    }
    

    }

1 个答案:

答案 0 :(得分:2)

你缺少抛出异常,添加throw e;在catch block E.g。

catch(Exception e){
    e.printStackTrace();
    throw e;
}