简体   繁体   English

会话bean如何工作?

[英]How does a session bean work?

Let's say I have one stateful session bean deployed in my application. 假设我在应用程序中部署了一个有状态会话Bean。 In my client application I try to use this session bean with the help of JNDI. 在我的客户端应用程序中,我尝试在JNDI的帮助下使用此会话bean。 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. 在获得bean的存根(而不是实际bean本身)并初始化了bean的某些字段之后,我尝试使用JNDI第二次获取该bean。

So what bean I will get from app server? 那么我将从应用程序服务器获得什么bean? Will it be the same bean twice or I will i work with a second instance of the bean? 是两次相同的bean,还是我将使用bean的第二个实例? If I will get the same bean twice, how can I get two different instances of the same bean from app server? 如果我将两次获得相同的bean,如何从应用程序服务器中获得同一bean的两个不同实例? I am just confused what the bean itself is. 我只是对bean本身感到困惑。 If possible provide some resources. 如果可能,请提供一些资源。

Let's say that you obtain a reference to a remote bean instance through JNDI. 假设您通过JNDI获得了对远程bean实例的引用。 Now, let's say that you invoke two business methods on the remote bean, methodA and methodB . 现在,让我们说,你调用的远程豆,两个业务方法methodAmethodB

The (main) difference between a stateful and a stateless bean is this: 有状态和无状态Bean之间的(主要)区别是:

  • 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. 如果你的bean是无状态的,则methodAmethodB 但不保证完全相同的bean实例在服务器端运行。
  • If your bean is stateful, then methodA and methodB are guaranteed to be running on exactly the same bean instance at the server side. 如果你的bean是有状态的,则methodAmethodB保证完全相同的bean实例在服务器端运行。

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. 在那个“存根”上调用多个方法-即使它是同一个存根对象-也不保证您将在服务器端处理完全相同的对象, 除非您的bean是有状态的。

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. 您说您在服务器上部署了一个有状态的bean,并且通过JNDI获得了对bean实例的引用。 To do that, you must have used the create method on the EJB's home interface. 为此,必须在EJB的home接口上使用create方法。 The create method returned a stub to you, and that stub maps to an instance on the server side. create方法向您返回了一个存根,该存根映射到服务器端的一个实例。

Next time you call create on the home interface, you'll get a different stub pointing to a different bean on the server side. 下次您在home接口上调用create时,将在服务器端得到一个指向不同 bean的不同存根。

If you want to maintain access to the previous bean, then you need to somehow keep a reference to that remote object. 如果要保持对上一个bean的访问,则需要以某种方式保留对该远程对象的引用。

  • If you're using EJB 2.x, then you shouldn't "cache" the stub anywhere; 如果您使用的是EJB 2.x,则不应将存根“缓存”在任何地方。 instead, get a reference to the bean's handle and store the handle somewhere. 相反,获取对Bean句柄的引用,并将该句柄存储在某处。
  • If you're using EJB 3.x, then you can simply store a reference to the stub. 如果使用的是EJB 3.x,则只需存储对存根的引用。

A stateful session bean is an enterprise bean (EJB component) that acts as a server-side extension of the client that uses it. 有状态会话Bean是企业Bean(EJB组件),充当使用它的客户端的服务器端扩展。 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. 有状态会话Bean由客户端创建,并且仅对该客户端有效,直到客户端连接断开或显式删除该Bean。

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 . 另请参阅Oracle文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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