storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 715 forks source link

Android - Can't reach SelfTest success callback #935

Open mattiagalati opened 4 years ago

mattiagalati commented 4 years ago

I have an old application built with cordova v6.5.0, and cordova-sqlite-storage v2.0.4. In the last two weeks, users says that the app hangs on init screen (where I initialize the sqlite database).

I ended up creating an empty app, with same versions, and only cordova-sqlite-storage plugin. Then, in my index.js i put this code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
  // Application Constructor
  initialize: function () {
    document.addEventListener(
      "deviceready",
      this.onDeviceReady.bind(this),
      false
    );
  },

  // deviceready Event Handler
  //
  // Bind any cordova events here. Common events are:
  // 'pause', 'resume', etc.
  onDeviceReady: function () {
    this.receivedEvent("deviceready");
    window.sqlitePlugin.echoTest(function () {
      console.log("-- ECHO test OK");
      window.sqlitePlugin.selfTest(function () {
        console.log("-- SELF test OK");
      }, function(e){
          console.error(e);
      });
    }, function(e){
        console.error(e);
    });

    window.cn = window.sqlitePlugin.openDatabase({
        name: "myappdb.db",
        location: 0,
        androidLockWorkaround: 1,
    },function(){
        console.log('Mydb open success');
        window.cn.sqlBatch([
            "CREATE TABLE IF NOT EXISTS [config] ([key] VARCHAR(255) NOT NULL UNIQUE, [value] TEXT, CONSTRAINT [] PRIMARY KEY ([key]));",
            "CREATE TABLE IF NOT EXISTS [log] ([timestamp] DATE DEFAULT (datetime('now','localtime')), [msg] VARCHAR(255));",
            "CREATE TABLE IF NOT EXISTS [documents] ([_id] INTEGER PRIMARY KEY, [acquired] DATE DEFAULT (datetime('now','localtime')), [queue_status] INT(1) NOT NULL, [attempts] INT(1) NOT NULL DEFAULT 0, [uri] TEXT NOT NULL, [data] TEXT);",
            "CREATE TABLE IF NOT EXISTS [locations] ([_id] INTEGER PRIMARY KEY, [timestamp] DATE DEFAULT (datetime('now','localtime')), [latitude] FLOAT NOT NULL, [longitude] FLOAT NOT NULL, [accuracy] FLOAT NULL, [altitude] FLOAT, [provider] TEXT NULL);",
            "CREATE TABLE IF NOT EXISTS [cache] ([_id] INTEGER PRIMARY KEY, [view_id] TEXT NOT NULL UNIQUE, [view_html] TEXT NULL);"
        ], function(){
            console.log('Schema init success')
        }, function(e){
            console.log('Schema init fail:', e);
        })
    },function(e){
        console.log('Mydb open fail:', e);
    });
  },

  // Update DOM on a Received Event
  receivedEvent: function (id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector(".listening");
    var receivedElement = parentElement.querySelector(".received");

    listeningElement.setAttribute("style", "display:none;");
    receivedElement.setAttribute("style", "display:block;");

    console.log("Received Event: " + id);
  },
};

app.initialize();

In the console log I see this messages:

Received Event: deviceready
SQLitePlugin.js:175 OPEN database: eggsential.db
SQLitePlugin.js:106 new transaction is waiting for open operation
index.js:36 -- ECHO test OK
SQLitePlugin.js:175 OPEN database: ___$$$___litehelpers___$$$___test___$$$___.db
SQLitePlugin.js:106 new transaction is waiting for open operation
SQLitePlugin.js:179 OPEN database: myappdb.db - OK
index.js:51 Mydb open success
SQLitePlugin.js:179 OPEN database: ___$$$___litehelpers___$$$___test___$$$___.db - OK

As you can see, the database seems to open correctly, but there is no further messages (success or fails) not in console nor in LogCat.

The problem has been reported on severa devices equipped with Android 8.1, 9 and 10. The same problem occurred with more recent versions of this plugin. We don't have an iOS version of this app so can't tell if the problem occurs in iOS as well.

benierm commented 4 years ago

I am facing the same problem. The sucesss callback for the first open is not called, so the js database never change states from INIT to OPEN.

When attempt to db.transaction(fn) the message "new transaction is waiting for open operation" is raised but the fn is never called.. So the app freezes (my app initialize database on start).

I debug both js and java code. After database is open, the sucesscallback is called from java side.

Updating sql plugin does not work.

brodycj commented 4 years ago

Minimal reproduction with latest version of both cordova-sqlite-storage and cordova-android platform is needed. Minimal reproduction should be a valid Cordova project with no plugins or platforms artifacts included.

https://stackoverflow.com/help/minimal-reproducible-example

brodycj commented 4 years ago

Closing this issue was a mistake, just reopened.

Another thing is that the androidLockWorkaround option is now deprecated. I would like to remove it someday.

benierm commented 4 years ago

I never used the option androidLockWorkaround before and does not work either. But I updated cordova version to 7.0.1 and android cordova plugin version to 6.2.3 and everthing works fine now. My previous cordova version was 6.0.0. I had nothing to do for upgrade too. I compared the two versions and I realized the cordova changed how they sends responses to js side.

I think this problem relates to some system webview update.

mattiagalati commented 4 years ago

My apologize @brodybits: the source code of index.js I've posted is exactly the only thing to edit after creating an empty project with cordova cli: I really don't know how to make my issue more minimal and reproducible.

Without any error to work on, we didn't found any other way than upgrade to Cordova 9.0.0, Cordova-Android 8.0.0 and plugin version 5.0.0. The problem is gone now, but at this time we have no idea of which of our applications is involved since the cause is unknown.

As mentioned by @benierm I also think that some Android Webview update caused the issue. Maybe if anyone could provide its experiences, we can discover the cause.

paragongithub commented 4 years ago

Hi, I can also confirm that we have had this problem, we rolled back the update applied in last 48 hours on Google Chrome & Android Webview and this got our application working again. We are also trying to work out an upgrade path to the current versions.