Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Transactions " Watch "Transactions " New topic
Author

Transactions

khaled Jamal
Ranch Hand

Joined: Feb 16, 2009
Posts: 64
Hi,

An enterprise bean's timeout callback method can only have a REQUIRED, REQUIRES_NEW, or NOT_SUPPORTED transaction attribute

Let me know please why An enterprise bean's timeout callback supports REQUIRES_NEW

Thanks


SCJP 6, SCBCD 5, SCWCD 5



Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3112
    
  42

The REQUIRES_NEW is useful here, if the timeout method is used to perform some *risky* task. For an example, if this timeout method is used to update email addresses on a separate database, it is a good practice to use the REQUIRES_NEW attribute here, so it is executed within a new transaction. If the transaction is marked for the rollback in this case, it will rollback the new transaction, and will not cause any side effects with the bean. But if you used REQUIRED attribute here, and if the transaction is rolledback, the timer will be implicitly canceled with transaction rollback. So, if you used this timer to complete some periodic tasks, further invocations on that will not be done.

Devaka.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
khaled Jamal
Ranch Hand

Joined: Feb 16, 2009
Posts: 64
Thanks a lot Devaka, I got it
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
But if you used REQUIRED attribute here, and if the transaction is rolledback, the timer will be implicitly canceled with transaction rollback. So, if you used this timer to complete some periodic tasks, further invocations on that will not be done.


Devaka,
The @Timeout method is the method which gets triggered periodically, the actually timer creation is usually done in a different business method. So if the transaction rolls back in the @Timeout method, the timer should not be cancelled. Here is some supporting documentation from the spec. An interesting sidenote is that in both the cases of Required and Requires_New, a new transaction is created for the execution of the @Timeout method.

18.2.5 Transactions
An enterprise bean typically creates a timer within the scope of a transaction. If the transaction is then
rolled back, the timer creation is rolled back.

An enterprise bean typically cancels a timer within a transaction. If the transaction is rolled back, the
container rescinds the timer cancellation.

The timeout callback method is typically has transaction attribute REQUIRED or REQUIRES_NEW
(Required or RequiresNew if the deployment descriptor is used to specify the transaction
attribute). If the transaction is rolled back, the container retries the timeout.


Note that the container must start a new transaction if the REQUIRED (Required) transaction
attribute is used. This transaction attribute value is allowed so that specification of a
transaction attribute for the timeout callback method can be defaulted.
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
But if you used REQUIRED attribute here, and if the transaction is rolledback, the timer will be implicitly canceled with transaction rollback. So, if you used this timer to complete some periodic tasks, further invocations on that will not be done.


Devaka,
The @Timeout method is the method which gets triggered periodically, the actually timer creation is usually done in a different business method. So if the transaction rolls back in the @Timeout method, the timer should not be cancelled. Here is some supporting documentation from the spec. An interesting sidenote is that in both the cases of Required and Requires_New, a new transaction is created for the execution of the @Timeout method.

18.2.5 Transactions
An enterprise bean typically creates a timer within the scope of a transaction. If the transaction is then
rolled back, the timer creation is rolled back.

An enterprise bean typically cancels a timer within a transaction. If the transaction is rolled back, the
container rescinds the timer cancellation.

The timeout callback method is typically has transaction attribute REQUIRED or REQUIRES_NEW
(Required or RequiresNew if the deployment descriptor is used to specify the transaction
attribute). If the transaction is rolled back, the container retries the timeout.


Note that the container must start a new transaction if the REQUIRED (Required) transaction
attribute is used. This transaction attribute value is allowed so that specification of a
transaction attribute for the timeout callback method can be defaulted.
Marcin Faryna
Greenhorn

Joined: Jul 16, 2009
Posts: 15
Yes, I understand this way:

1.Timer is created within transaction so rollback of this transaction imply timer creation will not happend.
2.Timer has its own transaction when it's timeOut method is invoked. What is is interesting here in spec "The timeout callback method is typically has transaction attribute REQUIRED or REQUIRES_NEW (Required or RequiresNew if the deployment descriptor is used to specify the transaction attribute)", REQUIRED suggests that timer timeOut method may join some other transaction which i think is not possible?

SCJP 6, SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transactions