Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved async capabilities:

The auto-commit was committing when it shouldn't, something like: add to queue in callback
of a query executed after .commit was called. It was messy, but upon consideration it seems
that the auto-commit functionality is unnecessary anyway. Consider:
1) Auto-commit is only enabled when the user calls .commit
2) Auto-commit is only enacted when the user creates a new Queue
3) New Queues do not become active when they are created
4) New Queues will not become active until the active queue is completed
5) The active queue does not complete until you call .commit or .rollback

The only case where auto-commit makes sense is if a user calls .commit and then
tries to begin a new transaction on the old Queue object... but this is not supported
since the Queue object does not have a .startTransaction method.

This fix enabled some code that otherwise failed, consisting of parallel multiple inserts,
all tests pass.
  • Loading branch information...
commit c13711830934e58bd46670f0baa5da4137ac4893 1 parent edf9c6d
Kris Reeves authored

Showing 2 changed files with 21 additions and 42 deletions. Show diff stats Hide diff stats

  1. +17 39 index.js
  2. +4 3 package.json
56 index.js
@@ -15,19 +15,6 @@ module.exports = function(db, debug) {
15 15 //Create a new executable query Queue
16 16 db.createQueue = function() {
17 17 return new Queue(function() {return dbQuery.apply(db, arguments);}, function () {
18   - //If the current Queue is a transaction that has not yet been committed, commit it
19   - var ceq = options.currentlyExecutingQueue;
20   - if(ceq != null && ceq.commit != null)
21   - {
22   - //Also, warn the user that relying on this behavior is a bad idea
23   - if(ceq._autoCommit !== true)
24   - console.warn("WARNING: mysql-queues: Database transaction was " +
25   - "implicitly committed.\nIt is HIGHLY recommended that you " +
26   - "explicitly commit all transactions.\n" +
27   - "The last query to run was:", ceq.lastExecuted.sql);
28   - ceq.commit(ceq._autoCommitCB);
29   - return;
30   - }
31 18 options.currentlyExecutingQueue = null;
32 19 //Called when a Queue has completed its processing and main queue should be executed
33 20 while(options.mainQueue.length > 0)
@@ -67,7 +54,7 @@ function Queue(dbQuery, resumeMainQueue, options) {
67 54 /* Execute all queries on the Queue in order and prevent other queries from executing until
68 55 all queries have been completed.
69 56 */
70   - this.execute = function() {
  57 + this.execute = function(commit) {
71 58 if(this.paused === true) return this;
72 59 var that = this;
73 60 //If another Queue is currently running, we put this on the mainQueue
@@ -93,17 +80,10 @@ function Queue(dbQuery, resumeMainQueue, options) {
93 80 //Execute the original callback first (which may add more queries to this Queue)
94 81 if(item.cb != null)
95 82 item.cb.apply(this, arguments);
  83 +
96 84 //When the entire queue has completed...
97 85 if(++done == total)
98   - {
99   - if(that.paused === true) return;
100   - /* The query's callback may have queued more queries on this Queue.
101   - If so, execute this Queue again; otherwise, resumeMainQueue() */
102   - if(that.queue.length == 0)
103   - resumeMainQueue();
104   - else
105   - that.execute();
106   - }
  86 + that.execute();
107 87 });
108 88 } catch(e) {
109 89 if(options.debug)
@@ -117,8 +97,15 @@ function Queue(dbQuery, resumeMainQueue, options) {
117 97 //All queued queries are running, but we don't resume the main queue just yet
118 98 //console.log("Queue Complete:", options.currentlyExecutingQueue);
119 99 }
120   - else if(options.currentlyExecutingQueue == this)
121   - resumeMainQueue();
  100 + else if(options.currentlyExecutingQueue == this) {
  101 + if(commit) {
  102 + dbQuery("COMMIT", function() {
  103 + delete that;
  104 + resumeMainQueue();
  105 + });
  106 + return;
  107 + }
  108 + }
122 109 return this; //Chaining :)
123 110 };
124 111 this.pause = function(maxWaitTime) {
@@ -132,29 +119,20 @@ function Queue(dbQuery, resumeMainQueue, options) {
132 119 }
133 120 return this; //Chaining
134 121 }
135   - this.resume = function() {
  122 + this.resume = function(commit) {
136 123 if(this.pauseTimer)
137 124 clearTimeout(this.pauseTimer);
138 125 this.paused = false;
139   - this.execute();
  126 + this.execute(commit || false);
140 127 return this; //Chaining
141 128 }
142 129 }
143 130 Queue.isNowTransaction = function(q, dbQuery) {
144 131 q.query("START TRANSACTION");
145 132 q.commit = function(cb) {
146   - if(this.queue.length > 0)
147   - {
148   - this._autoCommit = true;
149   - this._autoCommitCB = cb;
150   - this.resume();
151   - }
152   - else
153   - {
154   - delete this.commit;
155   - delete this._autoCommit;
156   - this.query("COMMIT", cb).resume();
157   - }
  133 + delete this.commit;
  134 + delete this.rollback;
  135 + this.resume(true);
158 136 }
159 137 q.rollback = function(cb) {
160 138 this.queue = [];
7 package.json
@@ -6,10 +6,11 @@
6 6 "homepage": "https://github.com/bminer/node-mysql-queues",
7 7 "repository": {
8 8 "type": "git",
9   - "url": "git://github.com/bminer/node-mysql-queues.git"
  9 + "url": "git://github.com/myndzi/node-mysql-queues.git"
10 10 },
11 11 "contributors": [
12   - "Tom Atkinson <atkinson.tommy@neosoft.ba>"
  12 + "Tom Atkinson <atkinson.tommy@neosoft.ba>",
  13 + "Kris Reeves <krisreeves@searchfanatics.com>"
13 14 ],
14 15 "main": "index.js",
15 16 "engines": {
@@ -19,7 +20,7 @@
19 20 "mysql": ">=0.9.5"
20 21 },
21 22 "bugs": {
22   - "url": "https://github.com/bminer/node-mysql-queues/issues"
  23 + "url": "https://github.com/myndzi/node-mysql-queues/issues"
23 24 },
24 25 "devDependencies": {},
25 26 "keywords": [

0 comments on commit c137118

Please sign in to comment.
Something went wrong with that request. Please try again.