This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
Sai Prasad
Ranch Hand
Joined: Feb 25, 2002
Posts: 560
posted
0
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();
Holmes Wong
Ranch Hand
Joined: Feb 18, 2002
Posts: 163
posted
0
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 ]
Holmes Wong
Ranch Hand
Joined: Feb 18, 2002
Posts: 163
posted
0
Could anyone tell what is wrong with my implementation or give an pointer? Thanks.
John Smith
Ranch Hand
Joined: Oct 08, 2001
Posts: 2937
posted
0
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.
Sai Prasad
Ranch Hand
Joined: Feb 25, 2002
Posts: 560
posted
0
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 Wong
Ranch Hand
Joined: Feb 18, 2002
Posts: 163
posted
0
Guys, thank you for giving pointers. I will try out these ideas.
arun s kumar
Greenhorn
Joined: Jul 23, 2002
Posts: 5
posted
0
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.