Last active
July 15, 2024 09:39
-
-
Save glenjamin/8924190 to your computer and use it in GitHub Desktop.
DB transaction from a connection pool in node-mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require('mysql'); | |
var pool = mysql.createPool('mysql://localhost'); | |
inTransaction(pool, function(db, next) { | |
db.query("DELETE * FROM stuff", function(err) { | |
if (err) return next(err); | |
db.query("INSERT INTO stuff VALUES (1,2,3)", function(err) { | |
return next(err); | |
}); | |
}); | |
}, function(err) { | |
console.log("All done, transaction ended and connection released"); | |
}); | |
/** | |
* Convenience wrapper for database connection in a transaction | |
*/ | |
function inTransaction(pool, body, callback) { | |
withConnection(pool, function(db, done) { | |
db.beginTransaction(function(err) { | |
if (err) return done(err); | |
body(db, finished) | |
}) | |
// Commit or rollback transaction, then proxy callback | |
function finished(err) { | |
var context = this; | |
var args = arguments; | |
if (err) { | |
if (err == 'rollback') { | |
args[0] = err = null; | |
} | |
db.rollback(function() { done.apply(context, args) }); | |
} else { | |
db.commit(function(err) { | |
args[0] = err; | |
done.apply(context, args) | |
}) | |
} | |
} | |
}, callback) | |
} | |
/** | |
* Convenience wrapper for database connection from pool | |
*/ | |
function withConnection(pool, body, callback) { | |
pool.getConnection(function(err, db) { | |
if (err) return callback(err); | |
body(db, finished); | |
function finished() { | |
db.release(); | |
callback.apply(this, arguments); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I bumped into this piece of code while trying to execute transactions using pool connections and it did a pretty good job in saving the day. However, for my application, I had to do some rewrites. The rewrites includes ES6 rest parameters and arrow functions. I have also made it possible that additional data passed to the
callbackFn
ofbody()
are not overridden by the error argument.Here is my modified version:
You can now do this: