2

I am new to Hibernate and when I am using transactions I met couple of questions. Previously, the situation is

@Transactional
Class A {
  methodA1(){
    DOs = fetchDOsFromDB();
    methodA2(DOs);
  }
    methodA2(DOs){
    ClassB.methodB1(DOs);
  }
}

Class B{
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  methodB1(DOs){
    dealWithDOs();
  }
}

But I found when the amount of DOs is huge, it will give connection is closed exception. So I am wondering if the root cause is I passed the DOs to methodB1 which bound with transaction in A and the transaction A can't be committed for a long time.

So this is the first question, I know @Transactional(propagation = Propagation.REQUIRES_NEW) will make the outer transaction be suspended but what does suspend exactly mean? Does it have timeout?

And if I don't pass DOs to classB, instead, I do

methodA1(){
DOs = fetchDOsFromDB();
List<Integer> list = getIds(DOs);
methodA2(list);
}

methodA2(list){
 ClassB.methodB1(list);
}

which means I am not relying on DOs, will this help?

Is there any relationship between the commit of transaction A and the running result of transaction B? In other words, does the commit of transaction A need to wait for the completion of methodB1?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
yilia
  • 125
  • 1
  • 6

1 Answers1

0

When you use Propagation.REQUIRES_NEW you are telling the persistence provider to start what is called a nested transaction. Oracle documentation has some good explanation of this topic:

  1. What does suspend exactly mean?

    While the nested (child) transaction is active, the parent transaction may not perform any operations other than to commit or abort, or to create more child transactions.

  2. Is there any relationship between the commitment of transaction A and the running result of transaction B?

    Committing a nested transaction has no effect on the state of the parent transaction. The parent transaction is still uncommitted. However, the parent transaction can now see any modifications made by the child transaction. Those modifications, of course, are still hidden to all other transactions until the parent also commits.

  3. In other words, does the commitment of transaction A need to wait for the completion of methodB1?

    If the parent transaction commits or aborts while it has active children, the child transactions are resolved in the same way as the parent. That is, if the parent aborts, then the child transactions abort as well. If the parent commits, then whatever modifications have been performed by the child transactions are also committed.

So, no A does not need to wait for B to commit, which can actually rollback and A can still commit.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63