aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes UserTransaction in CMT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "UserTransaction in CMT" Watch "UserTransaction in CMT" New topic
Author

UserTransaction in CMT

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I do get a UserTransaction within a Container Managed Transaction:



This works! but I am confused - I have thought that UserTransaction is only available when setting my bean to BMT: javax.ejb.TransactionManagementType.BEAN.

So is this code well?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

NOT_SUPPORTED refers to the calling code. Try calling updateUser in a transaction and see what happens. You can do what you like (within the limit of the EJB spec.) in the method (including what you've done) though your code is ultimately pointless, you could just as well have declared a differernt transaction attribute.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I do not understand that.

though your code is ultimately pointless


I am calling the method updateUser - and not using a UserTransaction, the exception "no transaction is active" is thrown - this is clear, as I have marked the method as "NOT_SUPPORTED".

When using a UserTransaction in this method , then all works, as the Transaction is supported by UserTransaction rather than Container.

So is this statement right:

"When annotating the method with @TransactionAttribute(javax.ejb.TransactionAttributeType.NOT_SUPPORTED) in a CMP-Bean,
then then I can get a Transaction within the Method via UserTransaction."

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What I meant was what is the point of defining a method with a "not supported" transaction attribute, then start a transaction in the method itself? OK, this will suspend any client transaction before starting your own transaction but I can't think of a reason why you might want to have behaviour like that?


I am calling the method updateUser - and not using a UserTransaction, the exception "no transaction is active" is thrown - this is clear, as I have marked the method as "NOT_SUPPORTED".

When using a UserTransaction in this method , then all works, as the Transaction is supported by UserTransaction rather than Container

Not sure I undestand what you are saying here. If the container throws an exception calling the method this would be the container doing something that the specification doesn't strictly require it to do. Like I said earlier, not supported refers to the calling code, not the implementation of the method. There is a section of the specification that talks about running methods in an unspecified transaction context (which is what you are doing here) which basically says that how the container should handle this is up to it.


"When annotating the method with @TransactionAttribute(javax.ejb.TransactionAttributeType.NOT_SUPPORTED) in a CMP-Bean,
then then I can get a Transaction within the Method via UserTransaction."

Hard to say - CMP is EJB 2 and before the days of annotations. Assuming you mean CMT, the answer is "possibly". But if you ask the question "why would I do this in the first place?" you may find your question become much less of a worry.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
oh I have meant CMT (not CMP).


What I meant was what is the point of defining a method with a "not supported" transaction attribute, then start a transaction in the method itself?


I have something like this:






I get this error, when flushing twice within a method:



So I wanted to try to use UserTransaction to be able to create a new Transaction after a closed Transaction.

Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Paul Sturrock wrote:If the container throws an exception calling the method this would be the container doing something that the specification doesn't strictly require it to do.
Hi Paul, I agree that using a UserTansaction in a NOT_SUPPORTED method of a CMT bean doesn't make sense. But nevertheless, at least if a method of the looked up UserTransaction is invoked the spec forces the app server to throw some exception. Or what's your interpretation of the spec ?
EJB 3.0 spec, section 13.3.4 (Enterprise Beans using CMT)
The enterprise bean's business methods [...] must not attempt to obtain or use the javax.transaction.UserTransaction interface.

EJB 3.0 spec, section 16.12 (UserTransaction interface)
The container must not make the UserTransaction interface available to the enterprise beans that are not allowed to use this interface.

Nimo, what app server did you use ? What happens if you call for example getStatus() on the UserTransaction you looked up ?

SCJP 5 (98%) - SCBCD 5 (98%)
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I am using jboss as with Hibernate and Seam.

However, I do not use UserTransaction - I solved my problem by using Seams Extended Persistence Context with manual flushing. With it, I can flush within a method more than once and each flush represents one (new) transaction.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ralph Jaus wrote:
Paul Sturrock wrote:If the container throws an exception calling the method this would be the container doing something that the specification doesn't strictly require it to do.
Hi Paul, I agree that using a UserTansaction in a NOT_SUPPORTED method of a CMT bean doesn't make sense. But nevertheless, at least if a method of the looked up UserTransaction is invoked the spec forces the app server to throw some exception. Or what's your interpretation of the spec ?
EJB 3.0 spec, section 13.3.4 (Enterprise Beans using CMT)
The enterprise bean's business methods [...] must not attempt to obtain or use the javax.transaction.UserTransaction interface.

EJB 3.0 spec, section 16.12 (UserTransaction interface)
The container must not make the UserTransaction interface available to the enterprise beans that are not allowed to use this interface.

Nimo, what app server did you use ? What happens if you call for example getStatus() on the UserTransaction you looked up ?

Turns out I don't know the spec. as well as I think. Sorry to mislead you Nimo, and thanks for the correction Ralph.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UserTransaction in CMT
 
Similar Threads
Nested Transaction in JTA
UserTransaction from Servlet in WebSphere, UserTransaction bound w/o any configuring
UserTransaction from Servlet in WebSphere, UserTransaction bound w/o any configuring
Help of JTA in EJB
UserTransaction in Hibernate