[Android] Strange database exceptions on device only

You must Login before you can answer or comment on any questions.

Hi guys,

I have the following module which I use to do some database queries:

function Database(service, callback, params) {
    var db = Ti.Database.open('stb_crm');
    switch (service) {
        case 'get_data':
            var SQL = "SQL query", 
                resultSet = getResultSet(db, SQL, params),
                results;
            // get data out of the result set into results
            resultSet.close();
            db.close();
            callback(results);
            break;
 
        default:
            db.close();
            break;
 
    }
}
 
module.exports = Database;
 
function getResultSet(db, query, params) {
    return db.execute(query, params);
}
Everything works fine on both devices and simulators. But on the devices I see some exceptions thrown by Titanium related to the database closing which don't affect the app:
10-03 14:12:54.960: E/SQLiteDatabase(8667): close() was never explicitly called on database '/data/data/nl.stb.stbcrmmobile/databases/stb_crm' 
10-03 14:12:54.960: E/SQLiteDatabase(8667): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1043)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1036)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:761)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at ti.modules.titanium.database.DatabaseModule.open(DatabaseModule.java:72)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at ti.modules.titanium.database.DatabaseModule.install(DatabaseModule.java:139)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:60)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:636)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:831)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:307)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at android.os.Looper.loop(Looper.java:137)
10-03 14:12:54.960: E/SQLiteDatabase(8667):     at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:109)
10-03 14:12:55.152: D/TiUIView(8667): (main) [733,1243] Nativeview is null
10-03 14:12:55.152: D/TiUIView(8667): (main) [1,1244] Nativeview is null
Any idea on what can I do to make this exceptions go away? Even though they don't seem to affect the app, I wouldn't want them there.

1 Answer

Judging by the Output lines:

10-03 14:12:54.960: E/SQLiteDatabase(8667): close() was never explicitly called on database '/data/data/nl.stb.stbcrmmobile/databases/stb_crm' 
10-03 14:12:54.960: E/SQLiteDatabase(8667): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
You just need to close the connection to your db after you have finished using it. This is also a best practice and the appropriate line should be along the lines of:

databaseName.close();

— answered 8 months ago by Steven McGowan
answer permalink
2 Comments
  • In the switch statement I'm doing that no matter what route does my code take.

    — commented 8 months ago by Lucian Pacurar

  • There could be something special with the phone not closing it since your Simulator is closing it. I should have mentioned that specifically.

    — commented 8 months ago by Steven McGowan

Your Answer

Think you can help? Login to answer this question!