Database randomly deletes itself every so often
I have a normal SQLite database in my app, managed through my own ContentProvider and implemented via SQLiteOpenHelper.
On the Droid only, every few weeks or so, the database just disappears. I have several users who wrote me about that, and I've seen it myself too now. I added some debug info (hard to repro when you only see it every few weeks), and in one instance, it just happened inbetween widget updates - first, the widget is displayed and shows the data just fine, the next time, the SQLiteOpenHelper's onCreate is called.
The app itself does some multi-threaded shenanigans, but all ContentProvider implementations have the synchronized keyword, all access to the database is via the content provider, and in the instance described above, there is no multi-threading going on (the app itself hadn't been running, just the widget).
And 开发者_JAVA技巧again, this is only on the Droid. I've never seen it on the G1. I never close the database, but it being a content provider, it sounds like I'm not supposed to do that in the first place (I remember hackbod saying that the idea is that the OS will close the app's process when necessary, which will automatically close the database).
Any ideas?
According to the documentation, SQLiteDatabase has locking enabled by default (SQLiteDatabase.setLockingEnabled
). In my app, I explicitly turn it on after opening, and added some other more paranoid synchronization to make sure multiple threads can't access the database at the same time, and that seems to have fixed it.
I'm still curious as to why this problem only ever occured on the Motorola Droid, and I had this hunch that this phone has locking disabled by default, but that sounds a bit weird - the Droid, after all, is one of the phones with almost no customization by the manufacturer at all.
In any case, explicitly enabling locking and proper synchronization did the trick for me.
精彩评论