zeta243 / osmdroid

Automatically exported from code.google.com/p/osmdroid
1 stars 0 forks source link

Need to close SQLite database (MBTiles) #327

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There is a call to SQLiteDatabase.openDatabase in MBTilesFileArchive, but no 
corresponding close.

This might not normally be a problem in an application that always has the 
Activity with the MapView active.  In my app, the Activity with the MapView 
gets created and destroyed frequently.  This causes an error from Android and 
Android won't let you re-open the MBTiles archive.

03-28 11:04:10.009: E/Database(16060): close() was never explicitly called on 
database '/mnt/sdcard/osmdroid/map.mbtiles' 
03-28 11:04:10.009: E/Database(16060): 
android.database.sqlite.DatabaseObjectNotClosedException: Application did not 
close the cursor or database object that was opened here
03-28 11:04:10.009: E/Database(16060):  at 
android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
03-28 11:04:10.009: E/Database(16060):  at 
android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
03-28 11:04:10.009: E/Database(16060):  at 
org.osmdroid.tileprovider.modules.MBTilesFileArchive.getDatabaseFileArchive(MBTi
lesFileArchive.java:35)

Original issue reported on code.google.com by mgwjenk...@gmail.com on 28 Mar 2012 at 12:01

GoogleCodeExporter commented 9 years ago
Suggested approach to fix:

Add void close(); method to IArchiveFile which means each class implementing 
interface, of course, needs to implement, including MBTilesFileArchive.  It's 
probably not a bad thing that all IArchiveFile classes do some file closing!  
In MBTilesFileArchive it's nothing more complicated than:
    public void close()
    {
        mDatabase.close();
    }
Then the close() needs to get called when the MapView is going away, which 
means overriding onDetach() and calling mTileProvider.detach() and adding 
mArchiveFiles.get(0).close(); to the loop in MapTileProvider.detach() that 
removes all the archive file references from the array.

Hope this helps - it seems to work for me!

Original comment by mgwjenk...@gmail.com on 28 Mar 2012 at 12:58

GoogleCodeExporter commented 9 years ago
This problem might only show up when modifying the opening of the SQLite 
database to READ+WRITE which is necessary on some real-world (not emulator) 
devices - see other issue - 318.

Original comment by mgwjenk...@gmail.com on 28 Mar 2012 at 1:18

GoogleCodeExporter commented 9 years ago
See also issue 318.

Original comment by neilboyd on 28 Mar 2012 at 3:30

GoogleCodeExporter commented 9 years ago
a workaround would be to expose the MBTilesFileArchive constructor so one can 
provide his own database, read-only or not.

Original comment by schizosfera@gmail.com on 25 May 2012 at 8:12

GoogleCodeExporter commented 9 years ago
I've created a patch with the changes mgwjenkins proposes. They work really 
well and I didn't observe any problems with it in my app. With this patch 
applied, I just call the MapView.onDetach() method in my fragment's 
onDestroyView() method.

Because "Issue attachment storage quota exceeded", I copied the patch to 
pastebin: http://pastebin.com/BdWWN5hX

Original comment by philipp....@gmail.com on 2 Mar 2014 at 12:59

GoogleCodeExporter commented 9 years ago
I've committed the patch in revision 1449.

Original comment by neilboyd on 3 Mar 2014 at 7:12

GoogleCodeExporter commented 9 years ago
It's been in for a while so I guess it must be okay

Original comment by neilboyd on 27 Aug 2014 at 5:55