Skip to content

Commit f5d280d

Browse files
authored
Merge pull request #1 from OutSystems/sequential-opendb
Sequential opendb Fixes problems with concurrent database open in iOS
2 parents 00b2cc0 + 9d464a6 commit f5d280d

1 file changed

Lines changed: 45 additions & 27 deletions

File tree

www/SQLitePlugin.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@
127127
};
128128

129129
SQLitePlugin.prototype.startNextTransaction = function() {
130-
var self;
131-
self = this;
132-
nextTick((function(_this) {
133-
return function() {
130+
var _this = this;
131+
nextTick(function() {
134132
var txLock;
135133
if (!(_this.dbname in _this.openDBs) || _this.openDBs[_this.dbname].state !== DB_STATE_OPEN) {
136134
console.log('cannot start next transaction: database not open');
137135
return;
138136
}
139-
txLock = txLocks[self.dbname];
137+
txLock = txLocks[_this.dbname];
140138
if (!txLock) {
141139
console.log('cannot start next transaction: database connection is lost');
142140
return;
@@ -158,8 +156,7 @@
158156
}
159157
}
160158
}
161-
};
162-
})(this));
159+
});
163160
};
164161

165162
SQLitePlugin.prototype.abortAllPendingTransactions = function() {
@@ -178,19 +175,26 @@
178175

179176
SQLitePlugin.prototype.open = function(success, error) {
180177
var openerrorcb, opensuccesscb;
178+
var _this = this;
179+
181180
if (this.dbname in this.openDBs) {
182181
console.log('database already open: ' + this.dbname);
183-
nextTick((function(_this) {
184-
return function() {
182+
nextTick(function() {
185183
success(_this);
186-
};
187-
})(this));
184+
});
188185
} else {
189-
var maxConnections = 4;
186+
187+
var maxConnections = 4, okConnections = 0;
188+
var failed = false;
189+
var successOnce = function(sqlitePlugin) {
190+
okConnections++;
191+
if (!failed && okConnections == maxConnections) {
192+
success(sqlitePlugin);
193+
}
194+
}
190195

191196
console.log('OPEN database: ' + this.dbname);
192-
opensuccesscb = (function(_this) {
193-
return function(connectionName) {
197+
opensuccesscb = function(connectionName) {
194198
return function() {
195199
var txLock;
196200
console.log('OPEN database: ' + _this.dbname + ', connection: ' + connectionName + ' - OK');
@@ -204,7 +208,7 @@
204208

205209
var callSuccessAndStartTransaction = function () {
206210
if (!!success) {
207-
success(_this);
211+
successOnce(_this);
208212
}
209213
txLock = txLocks[_this.dbname];
210214
if (!!txLock && txLock.queue.length > 0) {
@@ -221,33 +225,47 @@
221225

222226
};
223227
};
224-
})(this);
225-
openerrorcb = (function(_this) {
226-
return function() {
228+
openerrorcb = function() {
227229
console.log('OPEN database: ' + _this.dbname + ' FAILED, aborting any pending transactions');
228-
if (!!error) {
230+
if (!!error && !failed) {
229231
error(newSQLError('Could not open database'));
230232
}
233+
failed = true;
231234
delete _this.openDBs[_this.dbname];
232235
_this.abortAllPendingTransactions();
233236
};
234-
})(this);
235237
this.openDBs[this.dbname] = {
236238
state: DB_STATE_INIT,
237239
connections: []
238240
};
239241

240-
var options = {};
241-
for (var key in this.openargs) {
242-
options[key] = this.openargs[key];
242+
var copyOptions = function() {
243+
var options = {};
244+
var optionNames = Object.keys(_this.openargs);
245+
for (var i = 0; i < optionNames.length; i++) {
246+
options[optionNames[i]] = _this.openargs[optionNames[i]];
247+
}
248+
return options;
243249
}
244250

245-
for (var i = 0; i < maxConnections; i++) {
246-
var connectionName = 'connection' + i;
251+
var openNext = function openNext(connNumber) {
252+
253+
connNumber++;
254+
255+
var options = copyOptions();
256+
var connectionName = 'connection' + connNumber;
257+
var onSuccess = opensuccesscb(connectionName);
247258
options['connectionName'] = connectionName;
248259

249-
cordova.exec(opensuccesscb(connectionName), openerrorcb, "SQLitePlugin", "open", [options]);
250-
}
260+
cordova.exec(function() {
261+
onSuccess();
262+
if (connNumber < maxConnections) {
263+
openNext(connNumber);
264+
}
265+
}, openerrorcb, "SQLitePlugin", "open", [options]);
266+
};
267+
268+
openNext(0);
251269
}
252270
};
253271

0 commit comments

Comments
 (0)