this is a stateful session bean, since you are keeping a state --- a variable oTimeoutAccording to J2EE tutorial: A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation.
stateless不能保存状态,而你的bean好像是stateful的,所以才会出现这样的结果。
you can try1. call remove() on the bean 2. set a small timeout on the session bean
according to this guy: http://groups.google.com/groups?hl=en&threadm=3BBE8207.E8B35F8F%40magnaspeed.net&rnum=16&prev=/groups%3Fq%3Dstateless%2Bsession%2Bbean%2Bstate%26start%3D10%26hl%3Den%26selm%3D3BBE8207.E8B35F8F%2540magnaspeed.net%26rnum%3D16 The EJB 1.1 spec says that stateless SessionBeans may maintain state between method calls, although there is no guarantee that each subsequent method call from a client will be directed to the same SessionBean instance. So if I understand your question correctly, yes, you should be able to count on the container not resetting the bean instance variables once they have been set to some value by the bean. Note however that the container is allowed to call ejbRemove() on a stateless SessionBean instance at any time, and also to create new SessionBean instances at will (for example, in response to decreasing or increasing load from clients). In the create case, the container will call ejbCreate() on the newly-created instance.So as long as you set the instance variables in your ejbCreate() method, and clean up any necessary resources in your ejbRemove() method, you can count on the container not messing with your instance variables in the SessionBean instance. Note that they aren't really read-only, in that your SessionBean code can certainly modify them at any time depending on what you want to do.Randy Schnier WebSphere EJB Container developmentDate: 2001-10-05 21:15:22 PST
freekevin(kevin): 还记得那几个空的bean class的方法么 public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {}
A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation.
2. set a small timeout on the session bean
http://groups.google.com/groups?hl=en&threadm=3BBE8207.E8B35F8F%40magnaspeed.net&rnum=16&prev=/groups%3Fq%3Dstateless%2Bsession%2Bbean%2Bstate%26start%3D10%26hl%3Den%26selm%3D3BBE8207.E8B35F8F%2540magnaspeed.net%26rnum%3D16
The EJB 1.1 spec says that stateless SessionBeans may maintain state
between method calls, although there is no guarantee that each subsequent method call from a client will be directed to the same SessionBean instance. So if I understand your question correctly, yes, you should be able to count on the container not resetting the bean instance variables once they have been set to some value by the bean. Note however that the container is allowed to call ejbRemove() on a stateless SessionBean instance at any time, and also to create new SessionBean instances at will (for example, in response to decreasing or increasing load from clients). In the create case, the container will call ejbCreate() on the newly-created instance.So as long as you set the instance variables in your ejbCreate() method, and clean up any necessary resources in your ejbRemove() method, you can count on the container not messing with your instance variables in the SessionBean instance. Note that they aren't really read-only, in that your SessionBean code can certainly modify them at any time depending on what you want to do.Randy Schnier
WebSphere EJB Container developmentDate: 2001-10-05 21:15:22 PST
当你第一次调用你的Stateless Session Bean,AppServer产生了这个Bean并在你调用后放入缓冲池了,这个Bean的Context中保持了有关这个Bean的环境变量信息(而不是Bean内部的数据);当你第二次调用的时候,AppServer从缓冲池中再把这个Bean激活(因为我想这时除了你,不会有别人在调用它了吧),所以你会发现它保存了上次的状态。你可以试试第三次调用的时候是否为1500。所以如果你应该在每次调用的时候清空你所要设置的状态。
public void ejbCreate()throws CreateException {
System.out.println("ejbCreate()");
this.oTimeout = 0;
}
另外,测试程序中使用了Remove函数,在控制台中看到只有一次ejbCreate()?
上述情况是怎么产生的?怎么消除?
只是bdsc()老兄的一些细节的说法俺不赞成,“实际上ejb container是从你的bean继承了一个类来生成对象的,同时加入了一些系统调用的方法”,俺不认为是这样的。
还记得那几个空的bean class的方法么
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
如ejbCreate()里面什么代码都没有你不觉得奇怪吗?
再想一下CMP 的entity bean,你的find方法什么也不需要写,可你照样可以找到你所要的entity bean,why?因为所有的工作由ejb container完成了。