Announcement Announcement Module
Collapse
No announcement yet.
Problem with declarative tx!!! How does it work..Pls help. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with declarative tx!!! How does it work..Pls help.

    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

  • #2
    debug loggin

    Have you tried debug logging to see what Spring does. If it starts a transaction.

    And maybe you need a * after the name in the method names that should be transactional. That is what I have seen from the samples around:

    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>

    Comment


    • #3
      PROPAGATION_REQUIRES_NEW worked :-)

      The *, is a wildcard character, which is required when you have multiple method with the name starting with "find", in your example. If you specify just *, it would mean that it would be applied to all the methods of the target class / target proxy class.

      In my case, since I have just three methods, I don't necessarily need to use the *. I would think, not using wildcard character should not make any difference.

      The PROPAGATION_REQUIRES_NEW worked as desired when I had used the JDBC 3.0 drivers (from Data Direct for both SQLServer and Oracle).

      The PROPAGATION_REQUIRES_NEW would always start a flat transaction and for Nested Transaction, we would need to use the PROPAGATION_NESTED.

      The PROPAGATION_NESTED, however didn't work inspite of the using the database and JDBC drivers that supports nested transaction.

      Can anyone help me with the PROPAGATION_NESTED Tx issue ?

      Thanks a ton,
      Amit

      Comment


      • #4
        I haven't tested the nested transactions with the Data Direct drivers, but since we are using JDBC API calls, it should work as long as the database and the driver support savepoints. Could you post the log at the DEBUG level from a test run?

        A PROPAGATION_REQUIRES_NEW will suspend the current transaction and start a new one - the two transactions are independent of each other.

        It seems a bit odd to commit a transaction on a SQLException since the database operation might have failed. Could you post your code and application context so it is easier to see what is going on?

        Comment

        Working...
        X