Announcement Announcement Module
Collapse
No announcement yet.
Transaction synchronization for "inner" transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction synchronization for "inner" transaction

    Dear Spring community,

    I need somebody's help to implement correctly the following scenario:
    • The application starts the transaction, which is associated with new Hibernate session S to read items using the Hibernate's ScrollableResults.
    • The application reads items (with some lazy collections) from and updates the DB for each item. The transaction should be committed after each item.
    Looks like very simple scenario, but things get complicated if I would like to use Spring Batch for 2nd step. Spring Batch starts a new sub-transaction programmatically for each read/write loop cycle and the picture looks like following:

    Code:
    T1 {
     - Open session S1
     - Open read-only cursor via S1
    
     loop in T2 {
       - Open/re-use S2 for T2
       - Read item from S1
       - Update item back to DB via S2
       - T2.commit()
     }
    }
    In above scenario:
    1. In case T2.propagation = REQUIRES_NEW, transaction T1 is suspended, new independent transaction T2 is created and new Hibernate session S2 is created. In this case if lazy property was loaded with session S1, it can't be re-associated and persisted in session S2, because S1 is still opened.
    2. In case T2.propagation = REQUIRED, S1 is re-used (so S2 = S1) and transaction T2 is created with newTransaction=false and newSynchronization=false. So when T2 is committed, the transaction is really not committed and no synchronizations are called. This causes Bring Batch to fail.
    3. In case T2.propagation = NESTED leads to nearly the same result as previous: the synchronizations are not called for nested commit.
    I need something like in 2nd case, with shared Hibernate session (S1 = S2), with and with T2 <=> T1, so when T2.commit() happens, the transaction is really committed and synchronizations are notified, and the Hibernate session continues with the same JDBC connection.

    What is the right way to implement this in Spring?
    (reference post in Spring Batch).
    Last edited by dma_k; Mar 19th, 2011, 05:37 PM.

  • #2
    After looking at String sources, I may say, I need a mechanism, that allows the transaction to be committed several times during the lifespan of the same Hibernate session, with synchronization mechanisms (e.g. TransactionSynchronizationUtils.triggerBeforeCommi t()) invoked.

    Any ideas?

    Comment

    Working...
    X