这个Java单例是否可以在WebSphere 6中重复重建? [英] Can this Java singleton get rebuilt repeatedly in WebSphere 6?

查看:150
本文介绍了这个Java单例是否可以在WebSphere 6中重复重建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试追踪我们系统中的问题,以下代码让我担心。主servlet中的doPost()方法发生以下情况(名称已被更改以保护有罪):

I'm trying to track down an issue in our system and the following code worries me. The following occurs in our doPost() method in the primary servlet (names have been changed to protect the guilty):

...
if(Single.getInstance().firstTime()){
   doPreperations();
}
normalResponse();
...

单身'单身'看起来像这样:

The singleton 'Single' looks like this:

private static Single theInstance = new Single();

private Single() {
...load properties...
}

public static Single getInstance() {
    return theInstance;
}

使用静态初始化程序而不是检查null getInstance()方法中的theInstance,是否可以一遍又一遍地重建?

With the way this is set to use a static initializer instead of checking for a null theInstance in the getInstance() method, could this get rebuilt over and over again?

PS - 我们正在使用Java 1.4上的App运行WebSphere 6

PS - We're running WebSphere 6 with the App on Java 1.4

推荐答案

我在Sun的网站上发现了这个:

I found this on Sun's site:


由不同类加载器同时加载的多个单例



当两个类加载器加载一个类时,
实际上有两个
类的副本,并且每个人都可以拥有自己的
Singleton实例。这是
在某些servlet引擎
(例如iPlanet)中运行的servlet特别相关
,其中每个
servlet默认使用自己的类
loader。访问联合Singleton的两个不同的servlet
将在
中获得两个不同的对象。

Multiple Singletons Simultaneously Loaded by Different Class Loaders

When two class loaders load a class, you actually have two copies of the class, and each one can have its own Singleton instance. That is particularly relevant in servlets running in certain servlet engines (iPlanet for example), where each servlet by default uses its own class loader. Two different servlets accessing a joint Singleton will, in fact, get two different objects.

多个类加载器通常出现更多
比你想象的还要多。当
浏览器从网络
加载类以供applet使用时,它们为每个服务器
地址使用
单独的类加载器。类似地,Jini和RMI
系统可以使用单独的类
加载器来为他们下载类文件的不同代码库

如果您自己的系统使用自定义类
加载器,则所有相同的问题可能会产生

Multiple class loaders occur more commonly than you might think. When browsers load classes from the network for use by applets, they use a separate class loader for each server address. Similarly, Jini and RMI systems may use a separate class loader for the different code bases from which they download class files. If your own system uses custom class loaders, all the same issues may arise.

如果由不同的类加载器加载,
两个具有相同名称的类,即使是
相同的包名,也被视为
distinct - 即使实际上它们是
byte-by-byte相同类。
不同的类加载器代表
区分
类的不同命名空间(即使类的
名称相同),因此两个
MySingleton 类实际上是
distinct。 (参见参考资料中的类加载器作为
命名空间机制。)
由于两个Singleton对象属于
两个同名的类,因此乍一看它将显示

两个相同
类的Singleton对象。

If loaded by different class loaders, two classes with the same name, even the same package name, are treated as distinct -- even if, in fact, they are byte-for-byte the same class. The different class loaders represent different namespaces that distinguish classes (even though the classes' names are the same), so that the two MySingleton classes are in fact distinct. (See "Class Loaders as a Namespace Mechanism" in Resources.) Since two Singleton objects belong to two classes of the same name, it will appear at first glance that there are two Singleton objects of the same class.

引用

除了上面的问题,如果 firstTime()未同步,那么你也可能遇到线程问题。

In addition to the above issue, if firstTime() is not synchronized, you could have threading issues there as well.

这篇关于这个Java单例是否可以在WebSphere 6中重复重建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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