首页 文章

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

提问于
浏览
2

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

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>
  • 交易经理

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>
  • 和服务层的声明性事务

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

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

public void saveEmployee(Long roleId,Long divId,Long areaId,Employee emp){Session 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 回答

  • 2

    你缺少抛出异常,添加throw e;在捕获块例如

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

相关问题