Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restrictions on Transactions

 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

can anione explain me with example on statement(regarding Restrictions on Transactions) @
EJB 2.0 spec >>> page 82 >> 7.6.4 Restrictions on Transactions >> second Restrictions . it says


If SB instance is participating in a Txn , its an err for a client to invoke a method on SB instance such that the txn attrribute in DD wud cause
container to execute the method in difrent txn ctx or in unspecified txn ctx....



binoj V
 
Keerthi P
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a scenario where two or more threads attempt to invoke business methods on a session object using the same reference to EJBObject.
Here is a simulation:


Note that both the threads need not call the same business method. And, I have used Threads to simulate parallel execution of methods on a session object. This is analogous to a scenario where a user manipulates his shopping cart simultaneously. In one window, he adds some items to his shopping cart, while in the other, he removes some items from the cart. It is possible that while the add(Item item) method is still running, the remove(Item item) method gets called. The container throws RemoteException in this rare case.
 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
thanx keethi for ur explanation with the example!!!
binoj
 
shankar valiinaykam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keerthi,

Firstly, HF EJB says that we are not supposed to write any thread related code such as synchronized, wait, notify, Thread class...To be frank with you I laughed in my mind "Ohoh! So, I can escape from thread codes in EJB, hurrah!!!".....But then ur code made me sit up and see whatz that thread code inside SB.

Secondly, SB does not know whether business methods call from client through the body guard(EJBObject) is coming from one or two windows. Both methods are from same client AFTER ALL!!! It is as good as calling "add an item" to cart first and the calling "remove the item" from cart from a single window. And I dont know how container will handle, if in case these method calls are going simultaneously from two windows from a single client. I want to avoid thread codes as HF says, haa....

Finally, let me give my opinion on that sentence from spec. Stateful bean can leave a tx open in a method. If a client calls a method from stateful bean which opens a tx and then calls a method which does not use same tx, error will be thrown. Bcos, opened tx is not yet commited/rolled back. But client is trying to use some other tx. This is my humble opinion.

Please correct me if I am wrong.

Regards,
Sankar
 
shankar valiinaykam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:roll: :roll: :roll:





Keerthi & Binoj, waiting for your reply here.....
Sankar
 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Firtst of all i wan2 let u kno that the discussion is abt SFSB since for SLSB each method invocation is a new REQ to the container and wil b(may b) handled by the new instance.


Firstly, HF EJB says that we are not supposed to write any thread related code such as synchronized, wait, notify, Thread class...To be frank with you I laughed in my mind "Ohoh! So, I can escape from thread codes in EJB, hurrah!!!".....But then ur code made me sit up and see whatz that thread code inside SB.


U can write thread based pgms using EJB but not withing UR bean class but in client code.



Secondly, SB does not know whether business methods call from client through the body guard(EJBObject) is coming from one or two windows. Both methods are from same client AFTER ALL!!!

Yes U R right.


It is as good as calling "add an item" to cart first and the calling "remove the item" from cart from a single window.

No U r wrong , V R talking abt concurrent invocation of methods. Here in this case if U r in txn of "add an item" to cart and if u call "remove the item" container will thro RemoveExc..n


And I dont know how container will handle, if in case these method calls are going simultaneously from two windows from a single client. I want to avoid thread codes as HF says, haa....

Contianre wil throw RemoteExce..n or EJBExce..n
To achieve same result U can create threads in client code.


Stateful bean can leave a tx open in a method. If a client calls a method from stateful bean which opens a tx and then calls a method which does not use same tx, error will be thrown. Bcos, opened tx is not yet commited/rolled back. But client is trying to use some other tx. This is my humble opinion.

Yes U R right.But V r not discussing abt that. V R talking abt the folowing sentence from EJB 2.0 spec.:


If SB instance is participating in a Txn , its an err for a client to invoke a method on SB instance such that the txn attrribute in DD wud cause
container to execute the method in difrent txn ctx or in unspecified txn ctx....


In order understand this scenario consider following code snippet:
(1)@ ur client side u have folowin classes
import x.y.z......
.......
public class MyThread1 implements Runnable{
MyCompObj obj =null;
............................
public MyThread(){}

public MyThread(Object obj){
obj =(MyCompObj)obj;
}
public void run(){
obj.bizMthod1();
}
}

public class MyThread2 implements Runnable{
MyCompObj obj =null;
............................
public MyThread(){}

public MyThread(Object obj){
obj =(MyCompObj)obj;
}
public void run(){
obj.bizMthod2();
}
}
*****************************************************************
(2)in ur ejb-jar.xml
<ejb-jar>
<enterprise-beans>
....................
...........................
<enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MBean</ejb-name>
<method-name>bizMthod1</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>Bean1</ejb-name>
<method-name>bizMthod2</method-name>
</method>
<trans-attribute>RequiresNew</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
*****************************************************************
(3) in UR client pgm u have

EJBObject sharedObj = getEJBObjectFromHome(); // getting UR componet intf obj from Home obj
MyThread1 t1 = new MyThread1(sharedObj);
MyThread2 t2 = new MyThread2(sharedObj);
t1.start(); // This will cause container to execute bizMthod1 and SB instance is associating in a Txn with txn attr Required

t2.start(); // with this txn attr in DD is causing container to execute 2nd method in difrent txn

In this scenario container wil thro RemoteExc..n (Remote Client)or EJBExc..n(Local client) back to the client....



thanx & rgds
Binoj V
 
Keerthi P
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Binoj. I'm really glad that you understood what I conveyed.
 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please accept my apologies to have misspelt the name of bean in container txn of ejb-jar.cml.


*****************************************************************
(2)in ur ejb-jar.xml(modified)
<ejb-jar>
<enterprise-beans>
....................
...........................
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Bean1</ejb-name>
<method-name>bizMthod1</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>Bean1</ejb-name>
<method-name>bizMthod2</method-name>
</method>
<trans-attribute>RequiresNew</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
*****************************************************************

Binoj V
 
shankar valiinaykam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oooops!!!I have done a terrible mistake of forgetting the fact that EJB client need not be another bean. It could be servlet or simple java application. Thanks a lot, Binoj for pointing that!!!

So, I understood that SB instance cannot run multiple transactions in parallel. Also, I could correlate this to 'I' in ACID test of a safe transaction. Transactions have to be isolated so that they dont interrupt with each other.

Thanks to both of you guys, keerthi and binoj once again,
Sankar
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic