Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble having ultiple LockManager instances

 
Holmes Wong
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell what is wrong with my implementation or give an pointer? Thanks.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, thank you for giving pointers. I will try out these ideas.
 
arun s kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic