Hi, I declare and initialize a Singleton LockManager instance in DataInterface, RemoteDataAccess and LocalDataAcccess implement DataInterface. Since I use Singleton to control LockManager instance creation, and variables in an interface should be public, static, and final. So, I suppose there should be one LockManager instance shared by all remote accesses. Somehow, in the client, every time I get a remote connection (which is DataInterface since RemoteDataAccess sits in server, and that for hiding the distinction of local/remote connections), a new LockManager is created. In lock/unclock methods of RemoteDataAccess, I only use the LockManager instance created in DataInterface. I am confused about this. I believe something is wrong with my design. Any suggestions appreciated.
Holmes, I don't know the reason behind keeping an instance of the LockManager in the DataInterface. All you have to do is whenever you need a LockManager instance from the RemoteDataAccess, you make a call like: LockManager.getInstance();
Thanks, Sai: I did exactly like what you mentioned. Somehow, I did some checking inside getInstance() function: every time a client window starts up, (private static) LockManager variable got initialized. I am wondering if a Singleton instance only applied to a single VM, not for multiple VM's. Or somethig else plays a role in this thing. Here is my code: In LockManager class:
And in RemoteDataAccess class, I used LockManager.getInstance() to access lock and unlock methods. Everytime a remote connection is established, "Create new manager" will appear. Right now I am using multiple Data instances for multiple remote connections, but I don't think this has anything to do with it. [ June 19, 2002: Message edited by: Holmes Wong ]
Somehow, in the client, every time I get a remote connection (which is DataInterface since RemoteDataAccess sits in server, and that for hiding the distinction of local/remote connections), a new LockManager is created.
Sounds like your singleton is a singleton for a given connection only. If you want a single object to be shared by all connections, you need to create it on the server and pass a reference to it to all the connections. In my implementation, I actually instantiate LockManager from the Data constructor. Eugene.
Holmes, The value of instance is obviously null when you call getInstance() every time from the Connection object. Check for places where you might set the instance variable to null.
Holmes: getInstance method should be synchronized. If Lockmanager.getInstance is called in 3 different threads simultaneously there is chance that all 3 see instance initialized to null and all 3 can create new objects. This is called "Lazy initialization" problem.
Post by:autobot
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, that will fit in a stocking