diff --git a/README.md b/README.md index 990d75cf2..101760e12 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,20 @@ function onDeviceReady() { **NOTE:** The database file name should include the extension, if desired. +### Workaround for Android db locking issue + +An [issue was reported](https://github.com/brodysoft/Cordova-SQLitePlugin/issues/193), as observed by several people that on some newer versions of the Android, if the app is stopped or aborted without closing the db then: +- (sometimes) there is an unexpected db lock +- the data that was inserted before is lost. + +It is suspected that this issue is caused by [this Android sqlite commit](https://github.com/android/platform_external_sqlite/commit/d4f30d0d1544f8967ee5763c4a1680cb0553039f), which references and includes the sqlite commit at: http://www.sqlite.org/src/info/6c4c2b7dba + +The workaround is enabled by opening the database like: + +```js + var db = window.sqlitePlugin.openDatabase({name: "my.db", androidLockWorkaround: 1}); +``` + ### Pre-populated database For Android & iOS (*only*): put the database file in the `www` directory and open the database like: diff --git a/SQLitePlugin.coffee.md b/SQLitePlugin.coffee.md index 3908b54c3..38fb42486 100644 --- a/SQLitePlugin.coffee.md +++ b/SQLitePlugin.coffee.md @@ -450,6 +450,9 @@ if !!openargs.createFromLocation and openargs.createFromLocation == 1 openargs.createFromResource = "1" + if !!openargs.androidLockWorkaround and openargs.androidLockWorkaround == 1 + openargs.androidLockWorkaround = 1 + new SQLitePlugin openargs, okcb, errorcb deleteDb: (first, success, error) -> diff --git a/src/android/org/pgsqlite/SQLitePlugin.java b/src/android/org/pgsqlite/SQLitePlugin.java index 66a328160..b1d9d35a2 100755 --- a/src/android/org/pgsqlite/SQLitePlugin.java +++ b/src/android/org/pgsqlite/SQLitePlugin.java @@ -240,12 +240,12 @@ private SQLiteDatabase openDatabase(String dbname, boolean createFromAssets, Cal SQLiteDatabase mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null); - //if (cbc != null) // needed for Android locking/closing workaround + if (cbc != null) // needed for Android locking/closing workaround cbc.success(); return mydb; } catch (SQLiteException e) { - //if (cbc != null) // needed for Android locking/closing workaround + if (cbc != null) // needed for Android locking/closing workaround cbc.error("can't open database " + e); throw e; } @@ -821,7 +821,7 @@ private void bindPreHoneycomb(JSONObject row, String key, Cursor cursor, int i) private class DBRunner implements Runnable { final String dbname; private boolean createFromAssets; - //private boolean androidLockWorkaround; + private boolean androidLockWorkaround; final BlockingQueue q; final CallbackContext openCbc; @@ -830,7 +830,10 @@ private class DBRunner implements Runnable { DBRunner(final String dbname, JSONObject options, CallbackContext cbc) { this.dbname = dbname; this.createFromAssets = options.has("createFromResource"); - //this.androidLockWorkaround = options.has("androidLockWorkaround"); + this.androidLockWorkaround = options.has("androidLockWorkaround"); + if (this.androidLockWorkaround) + Log.v(SQLitePlugin.class.getSimpleName(), "Android db closing/locking workaround applied"); + this.q = new LinkedBlockingQueue(); this.openCbc = cbc; } @@ -853,12 +856,12 @@ public void run() { executeSqlBatch(dbname, dbq.queries, dbq.jsonparams, dbq.queryIDs, dbq.cbc); // XXX workaround for Android locking/closing issue: - //if (androidLockWorkaround && dbq.queries.length == 1 && dbq.queries[0] == "COMMIT") { - // Log.v(SQLitePlugin.class.getSimpleName(), "close and reopen db"); - // closeDatabaseNow(dbname); - // this.mydb = openDatabase(dbname, false, null); - // Log.v(SQLitePlugin.class.getSimpleName(), "close and reopen db finished"); - //} + if (androidLockWorkaround && dbq.queries.length == 1 && dbq.queries[0] == "COMMIT") { + // Log.v(SQLitePlugin.class.getSimpleName(), "close and reopen db"); + closeDatabaseNow(dbname); + this.mydb = openDatabase(dbname, false, null); + // Log.v(SQLitePlugin.class.getSimpleName(), "close and reopen db finished"); + } dbq = q.take(); } diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 730a1a750..b851f7ef2 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -475,6 +475,9 @@ if (!!openargs.createFromLocation && openargs.createFromLocation === 1) { openargs.createFromResource = "1"; } + if (!!openargs.androidLockWorkaround && openargs.androidLockWorkaround === 1) { + openargs.androidLockWorkaround = 1; + } return new SQLitePlugin(openargs, okcb, errorcb); }), deleteDb: function(first, success, error) {