Skip to content

Commit

Permalink
Workaround for Android db locking/closing issue (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Brody committed Mar 9, 2015
1 parent 60a4380 commit a622966
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions SQLitePlugin.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
23 changes: 13 additions & 10 deletions src/android/org/pgsqlite/SQLitePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<DBQuery> q;
final CallbackContext openCbc;

Expand All @@ -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<DBQuery>();
this.openCbc = cbc;
}
Expand All @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions www/SQLitePlugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a622966

Please sign in to comment.