• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateful and Stateless Session bean  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I want to know the basic difference between Stateless and stateful session bean. Their applicability and usage.
Preferably with an example.
Prasad
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you download Sun's Reference Implementation and experiment the examples. Actually, the documentation is a separate download. Several examples are given.
You can think about a Shopping cart to understand a stateful session bean.
Stateless session bean just do business processings. For example authenticating a user.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stateless Session EJBs operate at the method level. There can be many such beans in the pool and every time you issue a request, it may get serviced by a different one. That's because they keep no state info between requests.
Stateful means the opposite. The bean remembers "who you are", for the session only. There is no persistence unless that bean uses Entity EJBs but that's a separate question, IMO...
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good book on EJBs and J2EE also, is:
Mastering Enterprise JavaBeans and the Java 2 Platform, Enterprise Edition (with CD-ROM)
by Ed Roman.
 
Aaron Robinson
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this book is now free on Ed Romans www.theserverside.com
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am new to EJB and currently going through HFEJB. Actually I am using RI for practice. I created a shopping cart to test for stateless and statefull session bean. I created a client and called create() method two times on the same Home stub.

Bean:
I have a method named addToCart(Object) which stores items in instance variable ArrayList.

Client:
I called create() method twice on the same home so that I can see the effect of stateless session bean.

Can anyone please suggest any changes or mistakes that I am making so that I can clearly see the effect of stateless and statefull session bean ?

Waiting for your replies
~Rohit
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stateless beans are pooled and shared so calling create does not create an instance on the server.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradip
You are prefect !! Actually I wanted to test how the state in stateless bean is not being maintained by writing a simple program but I am unable to do so.
~Rohit
[ March 16, 2006: Message edited by: Rohit Bhagwat ]
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stateless session bean:

Do not have an internal state. Can be reused by different clients.
Need not be activated or passivated since the beans are pooled and reused.

Stateful session bean:

Do have an internal state. Reused by the same client.
Need to handle activation and passivation to conserve system memory since one session bean object per client.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello pillai

I agree with everyone , but I want to implement it, practically.I want to see the results of creating a stateless bean as against the statefull bean.
Can you suggest how can I implement the stateless bean >

~Rohit.
 
Hemanth Pallavajula
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohit,

As already mentioned in this thread, you just go ahead and implement what all you learn. No issues, if you face problem(s) while implementing. You can definitely come back to us.

All the best.
[ March 16, 2006: Message edited by: Hemanth Pallavajula ]
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hemanth
You rightly said as it is difficult to debug a problem without tryouts.
Following is the source code for the client program I have written.

public static void main(String[] args)
{
try
{
Context ic = new InitialContext();
Object o = ic.lookup("ShoppingCart");
ShoppingHomeInterface shop =
(ShoppingHomeInterface)
PortableRemoteObject.narrow(o,
ShoppingHomeInterface.class);
ShoppingComponentInterface sComponent = shop.create();

String str = "Close-up";

sComponent.addItemsToCart(str);

ShoppingComponentInterface sComponent1 = shop.create();

sComponent1.addItemsToCart(str);

ArrayList items = sComponent.getItemsFromCart();

System.out.print("sComponent :");
for(int i = 0; i < items.size(); i++)
{
System.out.print(items.get(i)+",");
}
System.out.println("");

ArrayList items1 = sComponent1.getItemsFromCart();

System.out.print("sComponent1 :");
for(int i = 0; i < items1.size(); i++)
{
System.out.print(items1.get(i)+",");
}
System.out.println("");
items1.clear();

System.out.println("items1.size() = "+items1.size());
System.out.println("items.size() = "+items.size());
for(int i = 0; i < items.size(); i++)
{
System.out.print(items.get(i)+",");
}

items.clear();
items1.clear();
}catch(NamingException e)
{
e.printStackTrace();
} catch (RemoteException e)
{
e.printStackTrace();
} catch (CreateException e)
{
e.printStackTrace();
}
}

Actually I was expecting that my ArrayList which is an instance member in the bean class will have different items as different instances will be created in the pool.However when I print the array list then I see all the manipulation being done on single instance.
Even after clearing the content of array list I am not able to get rid of the items present in the array list.
Is it the a single instance is created and once the client is finished with the work, the bean instance is again swapped back to the pool.When again I call a method then the same instance is taken back into service. Actaully I am completely baffled by seeing the output..Please help me

In Stateless bean is it that once the bean method is completed then immediately the bean is swapped into the pool ? and again when the same client calls the method then again some different or same bean is taken from the pool ? SO in the above example is it that since I am putting mulitple items in the cart by invoking the method multiple times, different beans will be maintaining the state ?

Waiting for your suggestions.

Thanks and Regards
Rohit.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In stateless session bean its the client's responsibility to send the required data. If you want to see separate value for instance variables you need to use SFSB as separate instances will be created for each create. For SLSB an instance from pool will be picked up to service the client. So in your case a single instance is used to service method invocation.
Also clearing the ArrayList does not reset the ArrayList at the bean(remote) side. It is separate instance.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Pradeep...

Want to add something to whole discussion.......(may be everyody will fill ya that's what we were saying)

Perhaps...what rohit wants to point out is why stateless is mainaining state?...& How to prove that its a truely stateless..(correct me if i am wrong) also he is talking about pool /Single instance etc...


See,
In a statless session bean(SLSB).....YOU CAN HAVE STATE(its not recommended although) OF AN OBJECT AS YOU ARE DEALING WITH A CLASS ONLY.THE POINT IS IT IS CONTEXT INCOMPLETE or you can say that each time you have to say to SLSB that Ok, take this data..process it ..& give me result...you can't depend on a server side object for maintaing your state as SLSB's are normally pooled & there is Instance Swapping also..so you can't make sure that each time a specific instance(say A) will be serving you it could be anybody from Pool (which is free:not in a method call/Transaction)

In a stateful session bean(SFSB) ,
A specific instance is given to you when you call create(...) & that instance serves your request(with or without Activation/Passivation etc.) ..so when you are talking to SFSB there is some context in a dialog ..it identifies you as against stateless..

SFSB is never pooled...so there is no Instance Swapping


Lets assume one simple example
say Rohit,Pradeep & I are in picture (No hard feelings please..)
I am talking to rohit

me:whats your name?
rohit: My name is rohit
m:how are you rohit?
r:fine,thank you
m:whts your favourite food?
r izza
m: what kind of pizza you like?
r:Mushroom Cheese Pizza
m:which is your favorite restaurent?
r:ABCXYZ
m:should we go there right now ?
r: why not? lets go & have a fun
{You can now understand that converstaional state is maintained here in this discussion....i am playing a role of client & Rohit is doing a job of SFSB.. i am asking 'n' number of questions once i know his name & some other details....i.e. Context Complete}



Now immidiately i look at Pradeep(who is not aware of this discussion / you may assume i call Pradeep ) & ask him one question
i.e. Should we go there right now?
p: what the hell you are talking about?
{Totally out of context..stateless my dear}


Shrinivas
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q :
In a stateful session bean(SFSB) ,
A specific instance is given to you when you call create(...) & that instance serves your request(with or without Activation/Passivation etc.) ..so when you are talking to SFSB there is some context in a dialog ..it identifies you as against stateless..

SFSB is never pooled...so there is no Instance Swapping
-------------------------------------------------------------------------

If the data with SFSB is passivated that means that instance is now free, hence can be given to another client(instance swapping?) now if previouse client comes his data will get activated by same instance or another???

Thanx,
Sachin!!!
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sachin,
Read the spec. carefully otherwise read Oreilly's EJB (Richard Manson)what it says once the instance is passivated (serialized)...instance from memeory is evacuated i.e. deleted so instance swapping doesn't occur....passivation occurs when EJB Container finds resourse crunch in terms of memory...DB connections ..socket connections etc..(Correct me if i am wrong)

I hope this helps
Shrinivas
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,
In Mastering EJB(By E d Roman page-83) I read that we can achive intance pooling in SFSB,in cost of Activation/passivation???


Regards,
Sachin!!!
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya Some servers might support instance pooling for SFSB but there is lot of overhead to use them that way....
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya Some servers might support instance pooling for SFSB but there is lot of overhead to use them that way....

Plz do not call me Sir (if in prev. message you are replying to my post)
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thanks guys , Now I will try for passing parameters to a method from client side to see the effect of Stateless Session Bean. May be that will give me a feel of stateless session bean.

Thanks very much.
~Rohit
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Just one more doubt..
Refering to above code..
ArrayList items = sComponent.getItemsFromCart(); // 1
ArrayList items = sComponent.getItemsFromCart(); // 2

In case of stateless bean, in the above statements, at 1 I understand that a new instance is used from the pool since client made a request. Now at statement 2 does the new bean instance is selected from the pool or the same instance which serviced the method at 1 is used to service same method call at 2 ? As per my understanding for server the client is nothing but the one who calls the method. So if I call the above two statements then the server knows that two clients have called the method getItemsFromCart, although the client program is the same.
Please correct me if I am wrong.
My next question is, Is the bean instance again swapped back to pool immediately after it completed the method call ? Is this is a guarantee that the following statements are executed in order ?
1. Client calls bean method
2. Bean is selected from the pool at server side
3. Bean services the method.
4. Method completes its execution and bean is swapped back to pool
5. client again calls the same method [Refering to above two statements]
6. Steps repreated from point 2 - 4.

I know I am wrong somewhere, and so my concepts are not clear and hence unable to create a simple stateless session bean.

Please help
Thanks and Regards
~Rohit.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends
I got answers to my question. I read the paragraph of "Stateless Session Beans" that is written on page 83 of Ed Roman and it says the following
"A stateless session bean is a bean that holds conversations that span a single method call. After each method call the container may choose to destroy a stateless session bean, or recreate it, clearing itself out of all information pertaining to past invocations. Expect your bean to forget everything after each method call.If your bean happens to hang around longer then great- but thats your container's decision and you should'nt rely on it"
The last sentence made me understand that RI is using single instance only for stateless bean and hence it showed me always the last value that I put in the cart.However this is container dependent.This solved my first doubt that I was not able to implement session bean as I was unable to see the effect of stateless bean. When just changed it from stateless to statefull I came to know the difference by seeing the output.

I know many of you know all this , but this information can be helpful for those who might have stuckup like me, and I feel that they shouldnt waste time like the one I have wasted.
Thanks all for your support and guidance. Hoping for the same for future questions.

~Rohit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!