Spring 即使在声明回滚之后,声明性事务回滚仍然失败

Spring Declarative Transaction Rollback fails for even after stating to roll back

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

1.My 数据源

<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; 面积area = null; 员工代码=空; 字符串 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();
    }
    

    }

你缺少抛出异常,添加抛出e;在 catch 块中,例如

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