简体   繁体   中英

How does a session bean work?

Let's say I have one stateful session bean deployed in my application. In my client application I try to use this session bean with the help of JNDI. After I have got the stub of the bean (not the actual bean itself) and initialized some fields of that bean, i try to get this bean a second time using JNDI.

So what bean I will get from app server? Will it be the same bean twice or I will i work with a second instance of the bean? If I will get the same bean twice, how can I get two different instances of the same bean from app server? I am just confused what the bean itself is. If possible provide some resources.

Let's say that you obtain a reference to a remote bean instance through JNDI. Now, let's say that you invoke two business methods on the remote bean, methodA and methodB .

The (main) difference between a stateful and a stateless bean is this:

  • If your bean is stateless, then methodA and methodB are not guaranteed to be running on exactly the same bean instance at the server side.
  • If your bean is stateful, then methodA and methodB are guaranteed to be running on exactly the same bean instance at the server side.

From the client's side, note that all you have is just a reference to an obscure object at the server side. Invoking multiple methods on that "stub" - even if it's the same stub object - doesn't guarantee that you'll be dealing with precisely the same object at the server side, unless your bean is stateful.

Now, more specifically, to your question. You say you have a Stateful bean deployed on the server and you obtained a reference to a bean instance through JNDI. To do that, you must have used the create method on the EJB's home interface. The create method returned a stub to you, and that stub maps to an instance on the server side.

Next time you call create on the home interface, you'll get a different stub pointing to a different bean on the server side.

If you want to maintain access to the previous bean, then you need to somehow keep a reference to that remote object.

  • If you're using EJB 2.x, then you shouldn't "cache" the stub anywhere; instead, get a reference to the bean's handle and store the handle somewhere.
  • If you're using EJB 3.x, then you can simply store a reference to the stub.

A stateful session bean is an enterprise bean (EJB component) that acts as a server-side extension of the client that uses it. The stateful session bean is created by a client and will work for only that client until the client connection is dropped or the bean is explicitly removed.

That means, that for the same client no matter ho many times you are going to acquire from the server the same instance must be returned. Otherwise, you could not rely on the state.

Please also see Oracle documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM