This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Is it possible to use a cache for the assignment and not to implement a shutdown hook. My approach is-
I have a DBFileAccess class with two method to load the database records into a cache and a persist database method to load the cache into the database file. For this, in my load Database method -
For persist Cache to Database file
My Data class is Singleton, with all methods synchronized and at application startup, in Data class private constructor, I call the loadDbRecords from the DBFileAccess class.
After each update, delete or create operation I persist database records back to the file instead of persisting records at the time of server shutdown. My server shutdown is simply System.exit(0) as my RandomAccessFile is closed after each read and write. Is this a valid approach to pass the exam? I know there will be slow performance issues but as it is not a criteria in this exam, can I overlook it? Any advise in this direction highly appreciated.
This is one of the ideas I've thought about. The rationale as I saw it is that you only take the performance hit on data writes. Data reads are likely to be much more common, so you really want those to be faster. So, you load the entire database into the cache at startup, and then write changes to both the cache and data file. But you never read from the file after the initial load.
I still haven't decided for certain yet (though I'm leaning towards the "write on shutdown" approach).
Just a remark: in a (room) booking system I guess there will be significant more updates than when your database file contains customers (with name, address, phone numbers,...). But of course the number of reads would still outshine the number of writes.
For completeness - the advantage (as I see it) of the continuous writing is that you don't lose all your data changes in the event of a crash. I also considered a compromise approach (periodic asynchronous writing of changes), but ruled that out as too complex for this exercise (I'd be tempted to do that or some other sort of autosave feature in a 'real' project though).
Whatever I decide, all this is going in my choices.txt file!