Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Second transaction in the background in an EJB3 application?

 
Agoston Bejo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Is there any way (preferrably with container-managed persistence, but even manually) to implement 2 transactions in EJB3 that fullfil the following criteria:


a)
pseudo-code:

T1 {
doSomething1
start T2 in background
doSomething2
commit
}

T2 {
doSomething
commit
}


b)
conditions: T2 "depends" on T1 in the sense that

1. T2 starts during T1
2. if T1 is rolled back ==> T2 must be rolled back, too
(==> this implies: T2 ends after T1 OR T2 waits for T1 to finish before committing)
3. if T2 is rolled back ==> doesn't cause T1 to rollback


c)
graphically:
("_" = "commit", "." = waiting)





The hardest part seems to me to make T2 wait until T1 finishes (in the otherwise unlikely but possible case that T1 finishes first). You can safely presume that the two transactions run on the same JVM, but of course a solution that doesn't rely on this presumption would be even better.

I have tried to find ways for T2 to somehow monitor T1, but on my first half-hearted try I couldn't find anything.



Any ideas?

Thanks,
Agoston
 
Agoston Bejo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you Coderanch guys get rid of this Javarebel spam? I see it in messages all the time here.
 
Michal Nalewajko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can solve this problem with the asynchrous message (JMS). You are sending the message from the middle of code but in fact the container will send it after T1's commit (T1 is transactional). So if T1 is rollbacked then the message will not be sent. If there is no rollback message will be sent and consumed by T2. Rollback of T2 doesn't affect T1.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic