Announcement Announcement Module
Collapse
No announcement yet.
How use JTA with JpaPagingItemReader when calls getTransaction()? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How use JTA with JpaPagingItemReader when calls getTransaction()?

    Per spec, cannot call em.getTransaction() when in a JTA transaction.

    Since JpaPagingItemReader.doReadPage() has:
    Code:
    EntityTransaction tx = entityManager.getTransaction();
    Running with JTA yields this problem:
    Code:
    10:28:40.408 [main] INFO  atomikos: createCompositeTransaction ( 300000 ): created new ROOT transaction with id cmr0000100073
    10:28:40.413 [main] DEBUG o.s.b.repeat.support.RepeatTemplate: Starting repeat context.
    10:28:40.413 [main] DEBUG o.s.b.repeat.support.RepeatTemplate: Repeat operation about to start at count=1
    10:28:40.413 [main] DEBUG o.s.b.i.database.JpaPagingItemReader: Reading page 0
    10:28:40.417 [main] DEBUG o.s.b.repeat.support.RepeatTemplate: Handling exception: java.lang.IllegalStateException, caused by: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
    10:28:40.417 [main] DEBUG o.s.b.repeat.support.RepeatTemplate: Repeat is complete according to policy and result value.
    10:28:40.418 [main] DEBUG o.s.b.repeat.support.RepeatTemplate: Handling fatal exception explicitly (rethrowing first of 1): java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
    10:28:40.418 [main] DEBUG o.s.b.core.step.tasklet.TaskletStep: Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
    10:28:40.418 [main] DEBUG o.s.b.core.step.tasklet.TaskletStep: Rollback for RuntimeException: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
    10:28:40.421 [main] DEBUG o.s.t.support.TransactionTemplate: Initiating transaction rollback on application exception
    java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
    How do we use JTA with the JpaPagingItemReader?

  • #2
    I copied JpaPagingItemReader locally and changed
    Code:
    EntityTransaction tx = entityManager.getTransaction();
    tx.begin();
    to
    Code:
    entityManager.joinTransaction();
    and deleted
    Code:
    tx.commit();
    Are there any negative ramifications of this change?

    As an enhancement to JpaPagingItemReader, would be good to improve its extendability (particularly with regard to overriding doReadPage()) - get methods for instance variables such as entityManager, or even extract transaction start and end to methods to enable override just them.

    Comment

    Working...
    X