Results 1 to 4 of 4

Thread: Transaction atomicity with Spring transactional annotation

  1. #1

    Default Transaction atomicity with Spring transactional annotation

    Hi,

    I am working the transaction atomicity using Spring . Below is what I am doing

    public void runBatchJob() {
    while (true) {
    // generate work
    doWork(unitOfWork);
    }
    }

    @transactional(..,rollbackFor="RuntimeException.cl ass")
    private void doWork(UnitOfWork work) {
    dao.m1(data1);
    dao.m2(data2);
    dao.m3(data3);
    dao.m4(data4);
    }


    where the DAO functions are defined:
    @Transactional
    public void m1(Data data) {
    ...
    }

    @Transactional
    public void m2(Data data) {
    ...
    }

    @Transactional
    public void m3(Data data) {
    ...
    }
    @Transactional
    public void m4(Data data) {
    ...
    }


    In applicationContext.xml:
    <tx:annotation-driven transaction-manager="jtaTransactionManager"/>

    Here if the dao.m1,m2&m3 are successful and m4 is failed then we need to rollback the entire transaction we shouldnot commit the transactions for m1,m2&m3 as well. We are using JTATransactionManager which by default supports 2 phase commit. But still using the above the transaction rollback is not happening eventhough a RuntimeException has been thrown?

    One more thing is how we can catch the RuntimeException and print the log for the above? whether we need to include this try catch block like

    @transactional(..,rollbackFor="RuntimeException.cl ass")
    private void doWork(UnitOfWork work) {
    try{
    dao.m1(data1);
    dao.m2(data2);
    dao.m3(data3);
    dao.m4(data4);
    }catch(RuntimeException e){

    //log the details
    }

    Please clarify the above 2 problems.

    Thanks.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Please use [ code[/code ] tags when posting code/xml/stacktraces that way it remains readable...

    This question has been answered numerous times before so please use the forum search... Also I suggest a read of the reference guide especially the part that explains AOP and how it works in Spring.

    In short Spring uses proxies and as a result only method calls INTO the object are intercepted INTERNAL method calls are not. So the calling of your private method will never be transactional, you basically have 4 individual transactions.

    Never catch the exception as that breaks proper transaction management the transaction manager needs to see the exception else it cannot handle it (rollback). You can log but also rehtrow the exception to not break tx management.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks Marten. That was a typo it is not a private method it is a public method

    @transactional(..,rollbackFor="RuntimeException.cl ass")
    public void doWork(UnitOfWork work) {
    dao.m1(data1);
    dao.m2(data2);
    dao.m3(data3);
    dao.m4(data4);
    }

    But still I am facing the issues where the rollback is not happening? Is there any configuration we need to do for that in respect to the TransactionManager or whether I need to use a different TransactionManager?

    My search on this didn't produce proper results any links are really appreciated.

    Regarding the logging the error message since try catch will break the transaction management, the only option is to re-throw the exception?
    @transactional(..,rollbackFor="RuntimeException.cl ass")
    public void doWork(UnitOfWork work) throws RuntimeException{
    dao.m1(data1);
    dao.m2(data2);
    dao.m3(data3);
    dao.m4(data4);
    }

    Thanks for the quick reply. Please clarify.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Again use [ code][/code ] tags when posting code...

    It doesn't matter if it isprivate or not, it is still an internal method call and that will never be intercepted... As mentioned before read the AOP chapter of the reference gudie.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •