It's not a secret anymore!*
The moose likes EJB and other Java EE Technologies and the fly likes Stateless session bean test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateless session bean test" Watch "Stateless session bean test" New topic
Author

Stateless session bean test

Ravi Singh
Ranch Hand

Joined: Aug 02, 2004
Posts: 51
I executed the following code as a stateless session bean test:

InitialContext ctx = new InitialContext(); // Get initial context
CaseEJBHome home = (CaseEJBHome)ctx.lookup("CaseEJB"); // Lookup home
caseEJB = home.create(); // Create stateless session bean

caseEJB.remove() // Remove newly created stateless session bean
caseEJB.remove() // Remove again, the newly created bean

I was expecting the second remove statement to throw an exception as the session bean has previously bean removed but it doesn't.

Is there any reason why no exception is thrown?
Brian Tinnel
Ranch Hand

Joined: Aug 25, 2003
Posts: 69
The remove of a Stateless Session Bean acts like a NOP. It doesn't really do anything. Therefore, you can call it as many times as you like and nothing should happen.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Originally posted by Brian Tinnel:
The remove of a Stateless Session Bean acts like a NOP. It doesn't really do anything. Therefore, you can call it as many times as you like and nothing should happen.


So you want to say that if i call business methods on a Stateless Session Bean, after invoking remove() method, it will work?


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Alex Sharkoff
Ranch Hand

Joined: Apr 11, 2004
Posts: 209
Hi Prakash,

Yes, it will work.

As Brian mentioned EJBObject#remove() call on a stateless session bean's component interface will not anything or in other words a remove() method will not be invoked on the actual SessionBean. It is up to the container to remove a stateless session bean from the pool.

As you might know a component interface is not linked to a particular instance of a stateless session bean. When a client calls a business method on the comp interface, container gets any bean of this type from the pool and invokes a method (i.e. a method invoked by a client on the comp interface) on it. As soon as the method is completed container returns the method result to the client and places the bean back to the pool.

Hope it helps,



Alex (SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJD 1.4)
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Thanks Alex,
I just tested it on OC4J and its working
[ September 16, 2004: Message edited by: Prakash Dwivedi ]
Ravi Singh
Ranch Hand

Joined: Aug 02, 2004
Posts: 51
Alex,

I thought invoking the create() method for a stateless session bean created an ejbObject for the client on the server, and then when the client invoked a method a bean from the instance pool was 'swapped' into the ejbObject of the client and serviced the request before being placed back into the instance pool?

Then when you invoke remove(), it is the 'ejbObject' which is removed.

Or is this just a conceptual view of things?
Brian Tinnel
Ranch Hand

Joined: Aug 25, 2003
Posts: 69
I guess you can think of it that way, but it isn't accurate and that could cause some errors in your code because your concept does not match reality. The danger in not knowing how a bean really works is that you might use an instance variable to not realize that when you store data in it, that data is available to any client which happens to get the same bean instance.

So it is important to know that a remove does not really remve anything, and that a create really doesn't create anything (from the standpoint of a Stateless Session Bean).
 
jQuery in Action, 2nd edition
 
subject: Stateless session bean test