问题
My midlet uses two record stores. Currently, I create/open both record stores when the app starts and I leave them both open for the entire lifetime of the app.
If I open/close the record store after each operation (e.g., reading or writing) the delays are really bad in the emulator.
Similarly, if I close the recordstores when the app exits, there is another very long delay.
So is it OK for me to never close the record stores in my code (thereby, presuming the device will do this itself when the app exits). If not, what is the best practice I can employ without causing a noticeable delay for the user and without risking any data loss?
There is nothing in the docs regarding this, and nothing I could find on google.
回答1:
As far as I remember, on some phones changes in DB are stored permanently only when DB is closed. While in most J2ME implementations changes are saved on each record change.
I would suggest keeping DB open for whole app session, if it significantly improves performance. It is worth handling DB close in destroyApp() of course.
You also can consider implementing 'auto save' feature - close and reopen DB if IO is inactive for some time.
Usually heavy DB access is performed in some actions only, not constantly. In this case you could wrap bunch of IO operations in a 'transaction' finishing it with DB close.
In other words, on most devices you can go with the first approach (keeping DB open) but on some devices (do not remember exactly, probably on Nokia S40 or S60) it can lead to data loss when the app will be terminated by VM (and you can't handle it since destroyApp is not guarantied to be called), without proper DB close. So in general case it would be right to wrap critical transactions with DB.close() statements
来源:https://stackoverflow.com/questions/29129946/j2me-rms-best-practice-for-opening-closing-record-store