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 NX: hooks and DB locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: hooks and DB locks" Watch "NX: hooks and DB locks" New topic
Author

NX: hooks and DB locks

Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 171
Hello, my dear Ranchers,
Currently I am doing nothing in my project to prevent data corruption arising from the server closing down with networked clients still attached. I have a server GUI, so when the close option is taken, I start the close down operation.
My first question is, should I take action by specifying a ShutdownHook or just continue the close down operation in the current thread? I assume that a ShutdownHook caters for situations in which an application ends in a controlled or uncontrolled manner, so my instinct is to use the Hook.
Secondly, I am thinking of locking the database as part of the shutdown procedure. The idea is that by the time I have locked all the records, I can be sure that no client is in the middle of an update. My concern is whether this processing, if running in a separate thread as part of the hook, will finish before the connection exception is issued to the client. Will a hook help? That is, will it ensure the lock process completes before the network goes down?
Any thoughts on this will be appreciated.
Simon
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Simon,
The earlier assignments (Fly By Night Services) did not require a GUI on the server, and many people who have worked with server side applications dislike having GUIs (I can give reasons if you like), so they used the shutdown hook to allow a clean way for the server application to shut down.
However since you are required to have a GUI, the shutdown hook is not as relevant - you can do the shutdown from the GUI itself.
Where the shutdown hook will still benefit those with GUIs, is that the server operating system can then initiate a shutdown of your server application - it does not need manual intervention (for example, doing a clean shutdown if power goes out (requiring signalling from the UPS)). I would consider this a necessity in a real server application.
But this is going a bit beyond the requirements of the assignment. You do not need to worry about how to handle such problems in this assignment, and you will not get extra marks for doing so. (If you do want to explore this, though, this project can be a nice place for you to experiment).
If you did have both the GUI shutdown option, and a shutdown hook, then I would recommend you take care to ensure that you do not have two bits of code calling the shutdown routines simultaneously (my preferred option would be to have the GUI just exit, leaving the shutdown hook to do the real work - this would have to be well documented though).
---
Rather than locking all the individual records, you might want to just set a flag to say that shutdown is proceeding, and refuse to grant any more locks and/or do any more writes while that flag is set. Then as soon as you know that there are no more writes currently in progress, you can shut down. This means that you could shut down while locks are still granted, but you know that the database is in a clean state.
Going back to my example of the UPS trying to shut down your application - in this case, you only have a limited time to do clean up operations before the UPS runs out of power. So you do not want to be waiting while trying to get all the individual locks. The sooner you know you can safely shut down the better.
And even if a client owns a lock, and issues the write request between the time that you started the shutdown and the time you have completed the shutdown, it still wont matter: the flag will have stopped the update of the file (so your data file is intact), and your client will (eventually) get an exception, so they will know that their booking did not succeed.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 171
Thanks Andrew,
You have clarified the issues in your usual lucid style!
Regards
Simon
 
jQuery in Action, 2nd edition
 
subject: NX: hooks and DB locks
 
Similar Threads
Logger / ShutdownHook
Logging in a Shutdown Hook
Inner Classes and logging...
How to do a clean exit?
Server shutdown