• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session beans equal method.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have confusion for the answer for the below question. Can any one please explain...




Given the following @EJB declarations:
@EJB(beanName="foo") Foo ref1;
@EJB(beanName="foo") Foo ref2;

@EJB(beanName="bar") Bar ref3;
@EJB(beanName="bar") Bar ref4;
Where ejb "foo" is a Stateful Session bean with Local business interface Foo and ejb "bar" is a Stateless Session bean with Local business interface Bar
After injection has completed, what would be the output of the following code?
System.out.println(ref1.equals(ref2));
System.out.println(ref3.equals(ref4));

Answer : false, true

For business interfaces of EJB 3.0 Stateful Session beans, a new Stateful Session bean is created for each @EJB field/method injection. That means ref1 and ref2 refer to two different Stateful Session bean identities.

In the Stateless Session case, the two references are equivalent because they refer to the same business interface of the same bean.

Unlike the 2.x component view, the Object.equals() operation for local and remote business interface references is defined by the EJB specification.
 
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what the spec says :

3.4.5 Session Object Identity
A client can test two session bean business interface references for identity by means of the
Object.equals and Object.hashCode methods.
3.4.5.1 Stateful Session Beans
A stateful session object has a unique identity that is assigned by the container at the time the object is
created.
A client of the stateful session bean business interface can determine if two business interface
references refer to the same session object by use of the equals method.
For example,

All stateful session bean references to the same business interface for the same stateful session bean
instance will be equal. Stateful session bean references to different interface types or to different session
bean instances will not have the same identity.

3.4.5.2 Stateless Session Beans
All business object references of the same interface type for the same stateless session bean have the
same object identity, which is assigned by the container.
For example,

The equals method always returns true when used to compare references to the same business interface
type of the same session bean. Session bean references to either different business interface types
or different session beans will not be equal.

HTH
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I may add , in your example

  • ref1 and ref2 satisfy the condition that they're different session bean instances and hence they will have different identities => they're not equal.
  • ref3 and ref4 satisfy the condition in section 3.4.5.2 which says

    "All business object references of the same interface type for the same stateless session bean have the
    same object identity, which is assigned by the container"



    Hence, by that identity token, ref3 and ref4 are equal.



  •  
    Damodar Mukhopadhyay
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Jeff, I still have a question

    According to the specification its fine, I just wander why the stateless beans equal method returns true for the same interface? That means the container will create a single object for it? I am sure it will not.

    If I change the state of the stateless session bean interface say variable1, then the equal method should return false.. right?

    Can you please describe what is the significance of returning true for stateless session bean interface instance?


    Thanks in advance.


    Regards,
    Damodar...
     
    Ranch Hand
    Posts: 268
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    jeff,

    Thanks a lot for detailed answer .

    Damodar
    If I change the state of the stateless session bean interface say variable1, then the equal method should return false.. right?


    change the state of the stateless session bean -> it is not possible because a stateless session bean do not /can not have a state (as its name says StateLESS).
    As you are refering to an instance/class-level(static) variable -> its legal to have instance/ final static variable with stateLESS session bean to fullfill logical/business requirnment.
    But that does not mean that variable is taking care of state, container creates pool of instance of a SLSB. And when a request comes it picks the bean & forward it for client request (container does not care who is the client, same instance can serve multiple clients, so it is not possible to carry state of SLSB).
    To carry state of Session Bean you have StatefulSB, as its name says, stateful, it can carry the state as it serves to single client (here you can assume reference variable as client).

     
    jeff mutonho
    Ranch Hand
    Posts: 271
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Damodar Mukhopadhyay wrote:Thank you Jeff, I still have a question

    According to the specification its fine, I just wander why the stateless beans equal method returns true for the same interface? That means the container will create a single object for it? I am sure it will not.
    Regards,
    Damodar...



    We can't be sure of that Damodar .This falls in the realm of EJB Container implementation.The session bean pooling (and hence object creation) strategies used by various containers differ, but the important thing is that all EJB Containers have to comply with what the spec says about equality of stateless session beans. That's all that matters ... I think it would be an interesting exercise to see how stateless session bean pooling is done in say JBoss's EJB Container.

    Also consider the point mentioned by Deepika
     
    Damodar Mukhopadhyay
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Deepika, Thank you Jeff,
    Now It is clear to me
     
    reply
      Bookmark Topic Watch Topic
    • New Topic