Announcement Announcement Module
Collapse
No announcement yet.
JPA - Hibernate - TransactionPropagation.REQUIRES_NEW doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA - Hibernate - TransactionPropagation.REQUIRES_NEW doesn't work

    Hi,

    I'm building an Java Web Application based on Spring (Web MVC) with a backend managed by JPA with Hibernate (using EntityManager). While developping, I use MySQL 5 Server (with InnoDB engine).

    I'm experiencing troubles with the transactions.

    I've a service method which imports a lot of data via web services. For each piece of data, a second method is called to do some operations in the database. The first method is annoted @Transactional(/* implicit required propagation */), the second is annoted with @Transactional(propagation = REQUIRES_NEW).

    In fact, data have to be computed in order of arrival. If a treatment fails, data already present should remain, but all the changes made during a data computing have to be removed.

    Code:
    MyServiceImpl implements MyService {
         @Override    
         @Transactional
         public void getLastChanges() {
             List<Change> changes = ...
    
             for(Change change : changes) {
                 insertChange(change);
             }
         }
    
         @Override
         @Transactional(propagation = REQUIRES_NEW)
         public void insertChange(Change change) {
             // insert a row in a table
             // update an other table
             // ...
             // after all, all is ok
         }   
    }
    With this example, if the service retrieves 5 changes, and the 3th change computing fails, the 2 firsts changes will be computed and saved in database. All updates made while computing the 3th change have to be cleared (rollback...).

    The problem is that the rollback seem to not be done.

    I wrote a little program that demonstrates this behavior. This program requires a MySQL 5 Server. It's available on Attachment or http://www.rclsilver.net/temp/test-jpa.zip

    All versions used in this application are the same as my final application.

    Thanks in advance...

    PS: i've posted the same message on Hibernate forums: https://forum.hibernate.org/viewtopi...4479&p=2452656
    Attached Files
    Last edited by rclsilver; Feb 19th, 2012, 11:38 AM. Reason: Adding URL

  • #2
    I suggest a search and a read of the Spring reference guide, especially the part explaining proxies and AOP.

    In short internal method calls aren't intercepted, so basically your @Transactional is useless on the insertChange method as that method is called internally and not externally.

    Comment


    • #3
      Thanks for your answer...

      I've exported the code of the method getLastChanges() in my scheduled method (@Scheduled) in an other bean. The scheduled method is also annoted with @Transactional (as the previous getLastChanges() method). In this way, all work fine. Thanks you for your answer... !

      Comment

      Working...
      X