Look at this example: Look up a session bean stub and make two method
calls on it (doesn't matter if it's the same method or not)
With a stateful bean the first method call can modify instance level
variables that the second method call could read those variables.
With a stateless bean you can't do this because even though you are
using the same stub to access the bean you may not be using the same
bean instance on the server. The server is free to swap out the bean
instance on the server side. The idea is that since not all clients are
simultaneously calling methods we can actually have less beans then
clients if the server passes the beans around.
I think what you need to understand is that with a stateful bean once
you look up it's stub you need to hang on to it. For instance you might
place it in a HttpSession if working on a web application. Avoid making
a JNDI lookup every time you want to make a method call.
-Chase
Roman Pohodzhai wrote:
>
> Sorry that I annoy you, but...
>
> In first your reply you said : " /Each/ /injection or lookup of a
> stateful session bean results in a new instance with a unique identity/".
>
> But in the last : "/They're stateful because if you invoke multiple
> times on the same EJB reference, each invocation will be handled by an
> instance with the same bean identity and same instance state/".
>
> As for me it's opposite things.
>
> Could you explain this?
>
>
>
> *From:* Kenneth.Saks_at_Sun.COM [mailto:Kenneth.Saks_at_Sun.COM]
> *Sent:* Thursday, November 01, 2007 9:16 PM
> *To:* rpohodzhai_at_spline-software.com
> *Cc:* ejb_at_glassfish.dev.java.net
> *Subject:* Re: EJB Stateful problem
>
>
>
> Roman Pohodzhai wrote:
>
>
>
>
>
> *From:* Roman Pohodzhai [mailto:rpohodzhai_at_spline-software.com]
> *Sent:* Thursday, November 01, 2007 3:36 PM
> *To:* Kenneth.Saks_at_Sun.COM <mailto:Kenneth.Saks_at_Sun.COM>
> *Subject:* RE: EJB Stateful problem
>
>
>
> Hello once more!
>
> It's very interesting for me J.
>
> Suppose I've lookuped some stateful EJB twice, hence EJB container has
> create two instances. My question is : "When this beans being
> destroyed, passivated?".
>
> In the absence of a specific client-initiated removal operation, it's
> up to the container to decide when to remove a stateful session
> bean. It's also up to the container to decide when, or if, to
> passivate a stateful session bean. Most containers have configuration
> attributes that control these decisions. In Glassfish, those are
> located in domain.xml and sun-ejb-jar.xml.
>
>
> It's interesting for me because when I look in monitoring section
> through admin console I see that two instances are created but no
> instance is removed or passivated. Why they are not removed, even
> after they can't be used? And why they are still called STATEFUL?
>
> They're stateful because if you invoke multiple times on the same EJB
> reference, each invocation will be handled by an instance with the
> same bean identity and same instance state. That's not true of
> stateless session bean invocations.
>
> It's up to the client to manage the stateful session bean reference.
> If you discard it, you have no way of ever referencing that stateful
> session bean. You can also explicitly remove the bean by designating
> a business method as an @Remote method. When such a business method
> is called, the container will automatically remove the stateful
> session bean after the method returns.
>
>
>
>
>
>
> *From:* Kenneth.Saks_at_Sun.COM <mailto:Kenneth.Saks_at_Sun.COM>
> [mailto:Kenneth.Saks_at_Sun.COM]
> *Sent:* Thursday, November 01, 2007 2:48 PM
> *To:* ejb_at_glassfish.dev.java.net <mailto:ejb_at_glassfish.dev.java.net>;
> rpohodzhai_at_spline-software.com <mailto:rpohodzhai_at_spline-software.com>
> *Subject:* Re: EJB Stateful problem
>
>
>
> Roman Pohodzhai wrote:
>
> Hello!
>
> I have some problem with Glassfish V2 B58g and Stateful ejb's.
>
> When I try to lookup any stateful bean from JNDI I always receive a
> new instance of ejb. I expected one instance per session but receive
> new one.
>
> Hi Roman,
>
> That's the spec-required behavior. Each injection or lookup of a
> stateful session bean results in a new instance with a unique
> identity. The current specification does not define any Singleton
> behavior. That's a feature under strong consideration for EJB 3.1.
>
>
>
>
> Thanks in advance!
>
>
>
> Sincerely,
>
> Roman Pohodzhai
>
>
>
> Spline-software : http://www.spline-software.com
> e-mail: _rpohodzhai_at_spline-software.com
> <mailto:rpohodzhai_at_spline-software.com>_
>
> skype account : rpohodzhai
>
>
>
>
>
>
>