带有实例变量的无状态会话bean [英] Stateless session bean with instance variables

查看:107
本文介绍了带有实例变量的无状态会话bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无状态会话bean,它包含一个公有方法,几个私有方法和一些实例级变量。下面是一个伪代码示例。

I have a stateless session bean that contains one public method, several private methods, and some instance level variables. Below is a pseudo code example.

private int instanceLevelVar

public void methodA(int x) { 
  this.instanceLevelVar = x;
  methodB();
}

private void methodB() {
  System.out.println(instanceLevelVar);
}

我看到的是methodB打印的值没有通过进入MethodA。最好的我可以告诉它是来自同一个bean的其他实例的打印值。什么会导致这种情况?

What I'm seeing is that methodB is printing values that weren't passed into MethodA. As best I can tell it's printing values from other instances of the same bean. What would cause this?

我应该指出代码按99.9%的预期工作。然而,.01%对我造成了一些严重的问题/疑虑。

I should point out the code works as expected 99.9% of the time. However, the .01% is causing some serious issues / concerns for me.

据我所知,如果我有不同的公共方法,那么我可能无法获得相同的bean调用,这会导致这种行为。但是,在这种情况下,唯一的调用是单一的公共方法。请问容器(Glassfish在这种情况下)仍然在私有方法调用之间交换bean?

I understand that if I had different public methods then I might not get the same bean back between calls, which would result in this behavior. However, in this case the only call is to the single public method. Will the container (Glassfish in this case) still swap the beans out between private method calls?

(编辑)我将class level更名为instance level引起了一些混淆。

(edit) I renamed "class level" to "instance level" as this was causing some confusion.

推荐答案

我根本就不打扰在无状态会话bean中使用实例变量。不管你遇到的问题的原因是什么,它可能不是你想要做的事情。只需尝试使用本地变量,或者在您从无状态会话bean业务方法调用的帮助程序类中定义实例变量。

I would just not bother using instance variable in stateless session bean at all. Regardless of what the cause of the issue you have encountered, it's probably not something you would want to do anyway. Just try using local variables throughout or define instance variables in helper classes you are calling from the stateless session bean business methods.

这篇关于带有实例变量的无状态会话bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆