aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Help of JTA in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Help of JTA in EJB" Watch "Help of JTA in EJB" New topic
Author

Help of JTA in EJB

Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56


I can get the value of varialbe "userTransaction"(com.ibm.ws.Transaction.JTA.UserTransactionImpl@xxxxxxxx) in jsp file, but can not get it in source code (EJB) with below exception:
Name "comp/UserTransaction" not found in context "java:".

What is the root reason? Is it due to websphere implementation (WAS 6.0)?...Thanks in advance!


<i>Mike</i>
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

From your EJB, lookup the UserTransaction from the Global JNDI namespace, instead of java: namespace, as follows:



I guess this should work.


[My Blog] [JavaRanch Journal]
Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56
Still not:
UserTransaction: First component in name UserTransaction not found.

"UserTransaction" has NOT been in configured in any file. The material in the link says it should be embedded in app server such as weblogic, but how about websphere, how to get it? Why can get it in jsp?
Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56
Is the below ok:
javax.transaction.UserTransaction userTransaction = getSessionContext().getUserTransaction();//call in EJB (BMT)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

You are able to look it from your jsp using java:comp/UserTransaction because i guess you have mentioned a resource-ref entry in your application's web.xml pointing to the UserTransaction. Can you confirm this?
When you use the resource-ref in your application's web.xml, the resource is made available at java:comp namespace.

As far as accessing the UserTransaction in Websphere is concerned, i will have to check it.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

Originally posted by Mike Sever:
Is the below ok:
javax.transaction.UserTransaction userTransaction = getSessionContext().getUserTransaction();//call in EJB (BMT)


Yes, that will work and infact is a better way of accessing the UserTransaction.
Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56
For web.xml, there is no "resource-ref" in it, maybe embedded in websphere..

Another exception occurs when calling UserTransaction.commit() (after getting sessionContext.getUserTransaction()):
java.lang.IllegalStateException: No Global Transaction exists to commit.
at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java:234)
at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java:251)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

Another exception occurs when calling UserTransaction.commit() (after getting sessionContext.getUserTransaction()):
java.lang.IllegalStateException: No Global Transaction exists to commit.
at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java:234)
at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java:251)


I guess you have not started the transaction. Here's what the code should look like if you are using BMT:

Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56
Thanks, it's helpful!

And if using CMT, there are two ways available after testing:
The first is:
(1) comment sentences of commit and rollback in DAO
(2) catch exception from DAO and call below in ejb:
sessionContext.setRollbackOnly();

The second is: change the value of "<trans-attribute>" from "Requried" to "Supports" in ejb-jar.xml

What is better in the two one? Is there any risk of calling "setRollbackOnly()" or using "Supports"?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

If i understand you right, what you are trying to achieve is - in a CMT rollback a transaction on exception. And in case there are no exceptions, you are expecting the transaction to be committed.

You can do the following:



The other alternative is to throw a exception which extends from java.lang.RuntimeException from the bean. In such a case the bean will rollback the transaction.



This article on Handling Exceptions has more info.

The second is: change the value of "<trans-attribute>" from "Requried" to "Supports" in ejb-jar.xml


Have a look at:
Trans-attributes to know the meaning of "Supports". I guess you would not want to change the trans-attribute to "Supports"
Mike Sever
Ranch Hand

Joined: Jul 24, 2006
Posts: 56
Clear, throw exception from DAO make much clear structure.
 
Don't get me started about those stupid light bulbs.
 
subject: Help of JTA in EJB