brodycj / cordova-sqlite-storage-dependencies

4 stars 3 forks source link

NullPointerException when database cannot be initialized #3

Closed georgechr closed 3 years ago

georgechr commented 4 years ago

Hello,

We're using cordova-sqlite-storage and noticed that if there's a problem opening the connection to the database when SQLiteGlueConnection is created then a NullPointerException is thrown. As seen below, this.db is not initialized to anything when the SQLException is thrown

public SQLiteGlueConnection(String var1, int var2) throws SQLException {
        if (var1 == null) {
            throw new SQLException("null argument", "failed", 21);
        } else {
            SQLGDatabaseHandle var3 = new SQLGDatabaseHandle(var1, var2);
            int var4 = var3.open();
            if (var4 != 0) {
                throw new SQLException("sqlite3_open_v2 failure: " + this.db.getLastErrorMessage(), "failure", var4);
            } else {
                this.db = var3;
            }
        }
    }
luissilvaos commented 3 years ago

Any update on this issue?

brodycj commented 3 years ago

As seen below, this.db is not initialized to anything when the SQLException is thrown

That sounds right on the SQLiteGlueConnection code. But I think not such a detailed error would be reported to the JavaScript when using the plugin. Here is a test case that shows a very simple "Could not open database" error message reported when using a slash character on Android, iOS, and Windows: https://github.com/storesafe/cordova-sqlite-storage/blob/5.1.0/spec/www/spec/db-open-close-delete-test.js#L175-L189

At this point, it would be really helpful if someone could give some more clues on how to reproduce this issue and what the expected output should be.

luissilvaos commented 3 years ago

As seen below, this.db is not initialized to anything when the SQLException is thrown

That sounds right on the SQLiteGlueConnection code. But I think not such a detailed error would be reported to the JavaScript when using the plugin. Here is a test case that shows a very simple "Could not open database" error message reported when using a slash character on Android, iOS, and Windows: https://github.com/storesafe/cordova-sqlite-storage/blob/5.1.0/spec/www/spec/db-open-close-delete-test.js#L175-L189

At this point, it would be really helpful if someone could give some more clues on how to reproduce this issue and what the expected output should be.

@brodybits It's very easy to replicate this issue. Just create a cordova app with the cordova-sqlite-storage plugin, change the targetSdkVersion to 30 and open a database on the onDeviceReady event. Then, run the app on an Android 11 device and you will get the following error:

2020-11-18 17:10:16.845 7615-7759/io.cordova.hellocordova E/SQLitePlugin: unexpected error, stopping db thread
    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String io.liteglue.SQLDatabaseHandle.getLastErrorMessage()' on a null object reference
        at io.liteglue.SQLiteGlueConnection.<init>(SQLiteGlueConnection.java:12)
        at io.liteglue.SQLiteConnector.newSQLiteConnection(SQLiteConnector.java:20)
        at io.sqlc.SQLiteConnectorDatabase.open(SQLiteConnectorDatabase.java:55)
        at io.sqlc.SQLitePlugin.openDatabase(SQLitePlugin.java:213)
        at io.sqlc.SQLitePlugin.access$000(SQLitePlugin.java:28)
        at io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:328)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

The SQLiteGlueConnection constructor is accessing the db field to retrieve the previous error message before initializing it.

jainank commented 3 years ago

There is an issue already reported, and I added my comment as well https://github.com/storesafe/cordova-sqlite-storage/issues/954

I am also facing the same issue on Android 11 (Api 30) however it works fine for (API 29). Here is my analysis:

following code returns negative int value SQLiteNative.sqlc_db_open(this.dbfilename, this.openflags) in file https://github.com/liteglue/Android-sqlite-connector/blob/master/src/io/liteglue/SQLGDatabaseHandle.java Line 14

Due to which https://github.com/liteglue/Android-sqlite-connector/blob/master/src/io/liteglue/SQLiteGlueConnection.java line 12 throws Error.

jainank commented 3 years ago

After I digging more, it has to do with Android 11 permissions. https://developer.android.com/about/versions/11/privacy/storage

georgechr commented 3 years ago

Is there a workaround for Android 11 ?

jainank commented 3 years ago

depends on how you are consuming it. Someone reported following:

If I used SQLlite plugin directly, it opens the database fine with the below example

var db = (window as any).sqlitePlugin.openDatabase({ name: 'my.db', location: 'default', androidDatabaseProvider: 'system' }); If however I remove the androidDatabaseProvider, same failure as ionic storage, it cannot open the database..

brodycj commented 3 years ago

I think we should continue the discussion in https://github.com/storesafe/cordova-sqlite-storage/issues/954 which is now a pinned issue.


P.S. Thanks for reporting!

brodycj commented 3 years ago

I published an update with the JAR libraries that I made with the updates from the OutSystems forks of android-sqlite-connector and android-sqlite-native-driver. The cordova-sqlite-storage plugin will be updated very soon. Thanks everyone for your help and patience!