File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes trouble having ultiple LockManager instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "trouble having ultiple LockManager instances" Watch "trouble having ultiple LockManager instances" New topic
Author

trouble having ultiple LockManager instances

Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
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
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
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
Could anyone tell what is wrong with my implementation or give an pointer? Thanks.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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
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
Guys, thank you for giving pointers. I will try out these ideas.
arun s kumar
Greenhorn

Joined: Jul 23, 2002
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble having ultiple LockManager instances