I'm a newbie who has no Java experience but is lucky enough to pass the SCJP. After trying different approaches to lock the data file and collections, I'm now going for the synchronized public method. Andrew has setDatabaseLocked() method in the data class, which locks the recordNumberLock. I wounder what is the best way to do a clean exit if we don't have any lock?
Me too! My shutdown hook synchronizes on the Data class object and calls a method called saveRecords() (I'm not sure this is the name of the method as I don't have the code with me right now) which writes the records from the memory cache back to the .db file.
I added a shutdown hook to my data class so that I can close the database when the application exists. I did not catch the records in the database so I did not need to write any records back to the database file when the application shuts down.
Roel De Nijs wrote:I used a shutdown hook to handle a clean exit on shutdown of the standalone client application or the network server
I did the same - ShutdownHook for standalone and server mode. Didn't define any for networked client - I have a thin client, so no locks can be acquired directly on the client side.
OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Joined: Jul 10, 2010
Thanks all you guys for replying, and no problem Mxolisi, I haven't thought it through before asking.
My problem is this, for the data class:
- I don't have any lock on the collections, I synchronize all the public methods specified by the provided interface.
- On the other hand, the example in Andrew's great book has recordNumbersLock and does not synchronize its public methods.
Now, Andrew has the CleanExit.run() ultimately locking recordNumbersLock before exit. I wanna do a clean exit too, but my code has no such recordNumbersLock to lock. What should I do?
Reading all your comments, I think the answer is simply to have another public synchronzied method in the Data class, which closes the RandomAccessFile (provided that I didn't cache the records). The shut down hook will just invoke that method through some intermediate method of GuiController.
Jason Ma wrote:Reading all your comments, I think the answer is simply to have another public synchronzied method in the Data class, which closes the RandomAccessFile (provided that I didn't cache the records). The shut down hook will just invoke that method through some intermediate method of GuiController.
The shut down hook will just invoke that method through some intermediate method of GuiController
How is this possible? If the shutdown hook in Data.java is triggered when the JVM is shutting down how can a method in GUIController call the shutdown hook?
Is it not enough to have the shutdown hook in the Data class constructor which calls saveRecords? This would always be called when the application is shutdown. I'm just trying to understand why we might need an explicit call in the GUIController.thanks
Colin Duggan wrote:Is it not enough to have the shutdown hook in the Data class constructor which calls saveRecords? This would always be called when the application is shutdown.
That's what I did too and should be enough. But the saveRecords-method is a method of the public interface of the Data class (actually my custom interface I created) which is not really needed if you call this method from the shutdown hook. But adding this method to its public interface it could be called for periodic saving (e.g. each 10 minutes) all records in a later version of the application