Home » Java » java – Nested transaction with jboss annotation @TransactionTimeout-Exceptionshub

java – Nested transaction with jboss annotation @TransactionTimeout-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I do have the question that’s bothering me. I did see a code that is running main Trasnaction and inside of that another Transaction. It looks like that:

@Asynchronous
@TransactionTimeout(value = 12, unit = TimeUnit.HOURS)
public void runExport(Long id) {
    /*
    *   some stuff that takes time
    */

    runCopy(id);
}

@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.HOURS)
public void runCopy(Long id) {
    /*
    *   some stuff that takes time
    */
    em.persist(copyObject);
    runRead(id);
}

@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.HOURS)
public void runRead(Long id) {
    /*
    *   some stuff that takes time
    */
    em.persist(runObject);
}
  • This is simplified code. But let me explain what it should do:
    • runExport method within main trasaction
    • do some time consuming operation
    • run runCopy method
    • in runCopy we should persist object to DB and see it after Transaction in runCopy is finished not after main transaction is
      finished
    • run runRead, and do similar stuff as before

But it is not working like that, because object that are persisted in runCopy and runRead are visible only after runExport transaction is over.

I understand why it behaves like that.
But I tried to fix it by adding @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)over last two methods. And according oracle doc it should suspend client transaction and start new one, and after it is finished resume client ones.
But it seems like it does not work like that, why is that? Does anyone could me with this one?

How to&Answers: