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 What to do if client closed? 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 "What to do if client closed?" Watch "What to do if client closed?" New topic
Author

What to do if client closed?

Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
I use rmi in my assignment,and I get a data connection from DBFactory. when client done all his work, he will close the client appliation. at this moment, what should be done? inform the server to close the connection? or rmi will deal with it automaticly?
I've thought about to make a connection pool, but if that too complicated?
Is there any better solution to this problem?
Waittinf for experts' reply, really thanks!
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Sean Lee:
I use rmi in my assignment,and I get a data connection from DBFactory. when client done all his work, he will close the client appliation. at this moment, what should be done?
Why, the client application should ensure that it calls close(), of course -- regardless of whether the database is local or remote.
I've thought about to make a connection pool, but if that too complicated?
It would complicate the design for performance reasons, which the instructions ask you not to do.
- Peter
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
thanks for your reply, but i'm still not very clear about closing.
surpose DBFactory create 3 Connections for 3 clients, if client a closed, what DBFactory should do? close that connection? or rmi will do that for me?
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Peter,
When you say close(), do you mean System.exit(0)? im confused where close() comes from.
Thanks,


-Matt<br /> SCJP2<br /> SCJD
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
As in DataInterface.close() -- clean up nicely when the client application exits.
I'm a bit confused now whether we are talking about the client or the server, by the way. The client would presumably have an "Exit" menu option and you can ensure that close() is called before you System.exit(0). You could add a shutdown hook for the server, but arguably cleanup is less important there. You certainly won't have to notify the clients; they'll get a nice exception next time they want to invoke the database.
- Peter
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Peter, I'm talking about the moment when client closing itself, what server should do.
Maybe it's because i'm not very familier with rmi. I think when DBFactory create a Connection for a client, there's a object created and run in the server. when client closing itself, I should inform the server to close the Connection object. am i right? yes, I know i should call a Connection's close method to do that. but how?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Sean Lee:
I think when DBFactory create a Connection for a client, there's a object created and run in the server. when client closing itself, I should inform the server to close the Connection object. am i right? yes, I know i should call a Connection's close method to do that. but how?
Calling close() is not the problem. It's just a Java method call and isn't obviously different from any ordinary method call (well, it may throw RemoteException, but that's it).
The only sensible thing the server-side implementation of close() might do is clean up any remaining locks held by the client.
I may understand your problem though: what happens to the Connection object itself? Like the method call, that isn't much different from what happens normally. RMI features a distributed garbage collector (DGC) which finds out that the client has released all references to your Connection, or exited altogether which boils down to the same thing. If the Connection implements Unreferenced, the unreferenced method is called. In any case, if you don't keep any further server-side references to the Connection, it will become eligible for garbage collection and ultimately be cleaned up.
In other words, beyond implementing a sensible close() method you don't have to anything.
Does that clarify things?
- Peter
[ January 04, 2003: Message edited by: Peter den Haan ]
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Peter, thanks.
now, i know rmi will do garbage collection for me to destory those unused Connection objects. rmi is really fantastic.
the last problem is still the close() method in Connection object.
as u know, all Connection objects are generated from DBFactory, and every one has a reference to a unique local Data object. when closing, we cannot close the Data object, but only close the Connection object itself. and as u said, GC will do that for me. so the only work i have to do is release all locks of this Connection.
but unlock method of LockManager doesn't have the function of unlocking by owner. we must give both record id and owner to run this method.
so I have to change the unlock method, am i right?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Sean Lee:
but unlock method of LockManager doesn't have the function of unlocking by owner. we must give both record id and owner to run this method.
Your call. You can either assume that clients will have cleaned up their locks before closing, and never crash, in which case you don't do anything --- or you assume that they may be buggy or crash, in which case your lock manager will have to have some kind of unlock by owner method.
Yet another design decision to make. It's almost real life
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What to do if client closed?
 
Similar Threads
Too Many Client Tasks
RMI Server problem
Server/Client simple doubt
The new I/O API
NX: sockets & ObjectStream