Reputation: 35
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>
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>
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); }
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();
}
}
Upvotes: 2
Views: 761
Reputation: 15698
You are missing to throw exception , add throw e; in catch block E.g.
catch(Exception e){
e.printStackTrace();
throw e;
}
Upvotes: 4