I tried a simple scenario related to declarative transaction, but it doesn't seems to work. I would appreciate if anyone could explain me why it doesn't work and how could I make it work ?
I have a class as follows :
public class EmployeeManager{
....DAO object initialized
....
//calls the addEmployee on the DAO object
public void addEmployee(Employee emp) throws SQLException
{
emd.addEmployee(emp);
throw new SQLException("There is a problem after adding");
}
//calls both the add/promote employee
public void test(Employee emp)
{
try{
addEmployee(emp);
}catch(SQLException ee)
{
}
promoteEmployee(emp);
}
//calls the promoteEmployee on the DAO object
public void promoteEmployee(Employee emp) throws SQLException
{
emp.setAge(222);
emd.promoteEmployee(emp);
throw new SQLException("There is a problem after promoting");
}
}
I'm making use of the TransactionProxyFactoryBean and the target is the EmployeeManager class.
<props>
<prop key="test">PROPAGATION_REQUIRED,+java.sql.SQLExcep tion</prop>
<prop key="addEmployee">PROPAGATION_REQUIRED,+java.sql.S QLException</prop>
<prop key="promoteEmployee">PROPAGATION_REQUIRES_NEW,-java.sql.SQLException</prop>
</props>
I have a sample test class and from which I call the EmployeeManager.test() and I EXPECT that an new employee would be added and it would not be promoted, as according to the transaction attirbutes in the XML file, when the addEmployee() would be committed when the SQLException is thrown and as the promoteEmployee() is started in the new transaction (PROPAGATION_REQUIRES_NEW) and hence when SQLException is thrown, it should be rolledback.
But it doesn happen as I expect, and both addEmployee() and promoteEmployee() both gets commited and when i modify the transaction attributes as follows :
<prop key="test">PROPAGATION_REQUIRED,-java.sql.SQLException</prop>
both the addEmployee() and promoteEmployee() gets rolledback.
Queries :
1) Can anyone throw light on the above behaviour of Spring Declarative transaction?
2) Also, in the above when I say PROPOGATOION_REQUIRES_NEW for promoteEmployee(), would it start a flat transaction or would it be a nested transaction?
3) How can I achieve what I expect ?
Thanks a ton,
Amit


Reply With Quote