2

The problem is even after stating to rollback for Exception.class still transaction is not rollbacked.

1.My datasource

<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. Transaction Manager

    org.hibernate.dialect.MySQLDialect 20 true update

    <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. and the declarative transaction at service layer

    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) public void saveEmployee(Long roleId, Long divId, Long areaId, Employee emp) { // TODO Auto-generated method stub employeeDao.saveEmployee(roleId, divId, areaId, emp); }

  3. and after saving the employee i try to update a field and once i got null pointer exception thought it was roll backed but it was not the method is :

    public void saveEmployee(Long roleId, Long divId, Long areaId,Employee emp) { Session session = sessionFactory.getCurrentSession(); EmployeeRole empRole = null; Division div = null; Area area = null; Employee 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 Answer 1

4

You are missing to throw exception , add throw e; in catch block E.g.

catch(Exception e){
    e.printStackTrace();
    throw e;
}
2
  • Thanks a lot mate very silly of me sorry cant up vote low on points.how can it catch if i dont throw ha ha. Jan 14, 2015 at 15:09
  • @user3829082 Glad it helps :) . Remember by accepting an answer you also gain point :)
    – sol4me
    Jan 14, 2015 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.