0

My method calls are like follow.

@TransactionAttribute(TransactionAttributeType.SUPPORTS
void M1() {
   M2();
   M3();
}

@TransactionAttribute(TransactionAttributeType.REQUIRED)    
void M2(){
   //saving x on data base
}

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
void M3(){
    //accessing x from data base
}

The issue is, some times value x is unavailable at method M3.

Can any body say whats the possible issue here ?

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
Amith Jayasekara
  • 421
  • 1
  • 4
  • 11

1 Answers1

0

There are two cases in your example, result is depending on whether is transaction already started in M1 or not, let me show you how it works

public void M1() {
   //transaction doesn't exist now
   M2();
   //transaction context has been created and ended due to REQUIRED attribute
   M3(); 
   //now you see result in DB because it was commited in M2s transaction 
}

public void M1() {
   //transaction already exists from method which called M1
   M2(); 
   //joins the transactional context
   M3();  //gets attached to the same context
   // and now whole transaction gets commited and only now you can be sure that you can read from DB safely
}

I hope it's helpful for you, solution would be marking M1 REQUIRED as well.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • thank you very much, I'll check second scenario with my total code :) – Amith Jayasekara Jan 06 '14 at 10:42
  • one more question. can I resolve this by adding a return type to M2 and call M3 based on return value of M2 – Amith Jayasekara Jan 06 '14 at 10:43
  • @Petr Mensik. As M3 join M2 transaction, shouldn't it have access to uncommitted changes inside the same transaction ? I suppose this depend on the isolation level and maybe also on the underlying database behavior. – Gab Jan 06 '14 at 10:48
  • @Gab That's also true, maybe can OP tell us what his isolation level is – Petr Mensik Jan 06 '14 at 11:37
  • @amith to be honest, I am not sure (it's been some time since I worked with EJBs), try this out – Petr Mensik Jan 06 '14 at 11:37
  • http://stackoverflow.com/questions/20948872/isolation-within-the-same-transaction/20950016?noredirect=1#20950016 – Gab Jan 06 '14 at 13:38