File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Timers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Timers" Watch "Timers" New topic


rudresh kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 83
Hi All,

i have few questions on Ejb3 timers, kindly clarify

a)supoose the timer is created for 5min and during that transaction rolled back @ 3rd min(rollback attribute is true), what will happen to timer, will it be recreated on its own? if so what will be the timer expiration, 5min or 2min
b)supoose the timer is created for 5min and suddenly the server crashed @ 3rd min, when server comes up again, what will happen to timer, will it be recreated on own?if so what will be the timer expiration, 5min or 2min

What about nested Timers, some where in the inner loops, runtime exception is thrown, what will be the behaviour

Thanks in advance
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226

I am new to EJB and was reading about Timers for the first time last night.

"Mastering EJBs" says
The creation of timers is supported within transactions.
Therefore, if an enterprise bean method that creates the timer is executed as
part of a transaction and if that transaction is rolled back, the timer creation is
rolled back too. Similarly, if an enterprise bean method cancels the timer by
calling cancel() on the Timer interface within a transaction and if that
transaction is rolled back, the container rolls back the timer cancellation as
well. The container restores the duration of the timer to the duration it would
have had, had it not been rolled back.
The timeout callback method can also be called within a transaction and
typically has transaction attributes of REQUIRES or REQUIRES_NEW. Hence, if
the transaction rolls back, the container will call the timeout callback method

which I think answers your first question.

EJB in Action says

However, unlike stateless session beans and MDBs, timers are persistent and can survive a container crash or restart.

which I think answers your second question.
I am unclear however whether you have to explicitly persist the Timer handle or whether the Container is just resiliant in this respect.
A quick test by writing a Timer would prove whether it worked for your Container - have you tried this?



28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 850
whenever the timer is created under any transaction, and that transaction rolls back due to the reasons stated by you, the timer creation will also be rolled back.

But if the timer is already created, and the time is elapsing then at the point of time, when the server crash, upon restart the container must try at least once to call the same timeout callback method with the same time elapse, when it was crashed.

SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog-
I agree. Here's the link:
subject: Timers
It's not a secret anymore!