Skip to content

Commit

Permalink
Fix database close conditions and unit test.
Browse files Browse the repository at this point in the history
The test was closing the database in the middle of an execute transaction. This would cause the transaction to fail because at the exit of the transaction handler a commit would be issued on a closed database. This fix moves the close to the success handler of the transaction so that the final close occurs after the transaction ends.

Additionally, the db variable is now obtained from the open callback instead of the function result, which is a more consistent pattern for async.

Finally, the close method was updated to fail if an attempt is made to call it while a transaction is active and a test was added to confirm this behavior.
  • Loading branch information
aarononeal committed Jan 2, 2015
1 parent 88a5bed commit 8c3bdcc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions SQLitePlugin.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ License for common Javascript: MIT or Apache
#console.log "SQLitePlugin.prototype.close"
if @dbname of @openDBs
if txLocks[@dbname] && txLocks[@dbname].inProgress
error(new Error('database cannot be closed while a transaction is in progress'))
return
delete @openDBs[@dbname]
cordova.exec success, error, "SQLitePlugin", "close", [ { path: @dbname } ]
Expand Down
38 changes: 34 additions & 4 deletions test-www/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,33 @@
start(1);
});
});

if (!isWebSql) test (suiteName + ' database.close fails in transaction', function () {
stop(1);

var dbName = "Database-Close-fail";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);

db.readTransaction(function(tx) {
tx.executeSql('SELECT 1', [], function(tx, results) {
// close database - need to run tests directly in callbacks as nothing is guarenteed to be queued after a close
db.close(function () {
ok(false, 'expect close to fail during transaction');
start(1);
}, function (error) {
ok(true, 'expect close to fail during transaction');
start(1);
});
start(1);
}, function(error) {
ok(false, error);
start(2);
});
}, function(error) {
ok(false, error);
start(2);
});
});

if (!isWebSql) test(suiteName + ' open same database twice works', function () {

Expand Down Expand Up @@ -1275,15 +1302,14 @@
stop(1);

var dbName = "Database-Close-and-Reopen";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function (db) {
db.close(function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function (db) {
db.close(function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function (db) {
db.readTransaction(function (tx) {
tx.executeSql('SELECT 1', [], function (tx, results) {
ok(true, 'database re-opened succesfully');
db.close();
start(1);
}, function (error) {
ok(false, error.message);
Expand All @@ -1292,6 +1318,10 @@
}, function (error) {
ok(false, error.message);
start(1);
}, function(tx) {
// close on transaction success not while executing
// or commit will fail
db.close();
});
}, function (error) {
ok(false, error.message);
Expand Down

0 comments on commit 8c3bdcc

Please sign in to comment.