This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes doubt at respect TransactionRequiredException 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 "doubt at respect TransactionRequiredException" Watch "doubt at respect TransactionRequiredException" New topic

doubt at respect TransactionRequiredException

Rafael Rocha

Joined: Jul 07, 2006
Posts: 20
in question enthuware simulator, exactly in custom test with 20 question, in question seven, exist one case about call method on the EntityManager, that remove, persist, refresh and merger, for example. ok

my doubt about that exception, in this case, in response for simulator:
The container throws the TransactionRequiredException if a transaction-scoped persistence context is used, and the EntityManager persist, remove, merge, or refresh method is invoked when no transaction is active.
otherwise, if i have been used transaction "extended" for persistence context, what occurs?

SCJP 1.5 : SCWCD 1.4 : SCBCD 5.0
Rafael Rocha

Joined: Jul 07, 2006
Posts: 20
other question, is that in question is not mentioned used session bean for local and remote, in this case, how i know exactly exception?

below this question
A bean method has just called entityManager.remove(student); where student is a valid entity. Assuming that the bean uses transaction scoped persistence manager and the call is made when no transaction is active, what will be the outcome?
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Can I assume the following code for your above question:

In the above code, whenever the method deleteEmployee is called, it won't participate in the transaction. Would it throw exception if we call it?
I don't think so. It works normally. If emp object is attached(I am assuming as in your question, it is specifies that the object is a valid entity) then it will be removes from the database.

Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Vinay Nath
Ranch Hand

Joined: Jul 06, 2008
Posts: 85
hey Rafael,

that is actually an interesting question.

The reason why this requirement of calling remove when a transaction exist is because in case of transaction scoped persistent context, the life of persistent context is tightly coupled with that of transaction. A persistent context gets created at the start of a method when transaction also is created. And when transaction commits persistent context is also closed.

So it doesnt make sense to call em.remove() when no transaction is there as it is useless without a transaction.

In case of "extended", an extended context is seperated from transaction so you can call em.remove() without a transaction and it will be okay.

hope that helps...

SCDJWS 5.0, SCBCD 5.0, SCWCD 5.0, SCJP 5.0
I agree. Here's the link:
subject: doubt at respect TransactionRequiredException
It's not a secret anymore!