aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Second transaction in the background in an EJB3 application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Second transaction in the background in an EJB3 application?" Watch "Second transaction in the background in an EJB3 application?" New topic
Author

Second transaction in the background in an EJB3 application?

Agoston Bejo
Greenhorn

Joined: Nov 23, 2007
Posts: 8
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

Joined: Nov 23, 2007
Posts: 8
Can't you Coderanch guys get rid of this Javarebel spam? I see it in messages all the time here.
Michal Nalewajko
Greenhorn

Joined: Mar 07, 2013
Posts: 7
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.
 
jQuery in Action, 2nd edition
 
subject: Second transaction in the background in an EJB3 application?