This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean Manage Transaction in Stateful Bean

 
Amirr Rafique
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stateful
@TransactionManagement(BEAN)
public class TestBean implements Test
{
@Resource
private UserTransaction transaction;
public void someOperation(Timer timer)
{
transaction.begin();
// Do some logic here.
}
}
Which of the following statements are true regarding the above code?
a. The Container will automatically commit the transaction at the end of the method.
b. The Container will throw EJBException since the transaction is not ended.
c. The behavior of the above code will vary from Container to Container.
d. None of the above.

Why b is given as correct answer
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although it is NOT MUST for a STATEFUL Session Bean instance to commit/rollback a transaction before the business method returns ( the business method that transaction begin() ). It is a MUST to commit/rollback a transaction at the last business method returns of the whole transaction (since stateful session bean might have multiple client call, which most probably in different business method), because every transaction have its transaction demarcation/boundaries.

so for these question, since there is only one method in the TestBean class, the transaction completed in the same method it begin. Having no commit() in that method, will cause a system exception to be thrown, which is wrapped in EJBException. (exception which is RemoteException and RuntimeException is wrapped in EJBException)


Please correct me if I am wrong , thank y
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

IMHO you deal here with a BTM and in this case you (or more precisely the bean) are (is) responsible for the transaction life cycle. So if you start it you must also end it (commit it).

Regards,
Mihai
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Taking into consideration a statefull bean with a EXTENDED persistence context do we also have to commit the user transaction in each single method?

Regards
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krzysztof,


Why do you think about extended persistence context ? The only relation is the fact that extended persistence context can be used only with SFSB this is a feature but not a must. In this example there is no persistence context.

Regards,
Mihai
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic