EJB transaction

There two way using trasanction in EJB(Session Bean). One way is that I can handle transaction by UserTransaction through JNDI or by configuring transaction attribute in ejb-jar.xml. When should I use first way ? when should I use the second way? The second question is:Can I use UserTransaction in javabean by JNDI lookup , suppose that the javabean run inside application server? Thanks

When should I use first way ? when should I use the second way? UserTransaction is supposed to be used only when the developer wants to take care of transaction semantics... when to start/end a transaction... for example EJB's with Bean-Managed Persistance...OR say Session EJB where you might want to start a transaction in one method and commit in some other...Can I use UserTransaction in javabean by JNDI lookup , suppose that the javabean run inside application server? yes, you can lookup for UserTransaction and use it...

We use UserTransaction in session bean, when the dev wants to write the transaction logic. Becoz some transaction logic may be fixed, irrespective of the business situations. Suppose if we take a simple bank aplication, the "withDraw" method always need a trasaction to be set in a unique way. so if the transaction logic needs to be changed from deployment to deployment, we go for Container managed. i.e XML files to configure. otherwise we can go for writing the transaction logic embed inside the code. Is is confused still..... hello_nandu@yahoo.com

Originally posted by RaviShanker Tippa: To be Precise, If the transaction is of type Bean Managed,then u can use UserTransaction.This apply only to sessionBeans not Entity Beans as they always are COntainer Managed Transactions.

I thought Entity Beans can be Bean Managed also. As far as Session Beans are concerned, I think only statefull session beans could be stored in Persistent storage. Can anybody throw some light weather Session beans (Statefull) could be Bean Managed?

Originally posted by Maggy Zhang: When should I use first way ? when should I use the second way?

BMT allows you to have a better control over the scope of the transaction, also if you�re using statefull session beans you can use it to make a transaction scope around more than one method call, with CMT your control over transactions is restricted to methods.

The second question is:Can I use UserTransaction in javabean by JNDI lookup , suppose that the javabean run inside application server?

The specification API provides it and the method to get it stays like this(this method belongs to the EJBContext interface):

Yogesh Mathur
Greenhorn

Joined: Apr 18, 2002
Posts: 17

posted May 11, 2002 02:38:00

0

Originally posted by Yogesh Mathur:

I thought Entity Beans can be Bean Managed also. As far as Session Beans are concerned, I think only statefull session beans could be stored in Persistent storage. Can anybody throw some light weather Session beans (Statefull) could be Bean Managed?

I am sorry. I am new to EJB. I got confused between Persistence and Transactions. Actually its analogous- session beans can have CMP, CMT & BMT whereas entity beans can have BMP, CMP & CMT. Sorry for such silly mistake.