Skip to content

Commit

Permalink
fix(db): lower deadlock wait limits
Browse files Browse the repository at this point in the history
This commit adjusts the transaction deadlock limits so that transactions
fail faster if a deadlock is encountered.  Now a transaction will fail
in approximately 3 seconds instead of ten.  This helps prevents transactions
from stacking - and frees up connections from the pool faster.
  • Loading branch information
Jonathan Niles authored and jniles committed Mar 21, 2017
1 parent a3aa269 commit 567ec22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/lib/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class DatabaseConnector {

// format the SQL statement using MySQL's escapes
const statement = mysql.format(sql.trim(), params);

connection.query(statement, (err, rows) => {
connection.release();
return (err) ? deferred.reject(err) : deferred.resolve(rows);
Expand Down
19 changes: 9 additions & 10 deletions server/lib/db/transaction.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@

const q = require('q');
const winston = require('winston');

/** @const the number of times a transaction is restarted in case of deadlock*/
const MAX_TRANSACTION_DEADLOCK_RESTARTS = 10;
const MAX_TRANSACTION_DEADLOCK_RESTARTS = 5;

/** @const the number of milliseconds delayed before restarting the transaction */
const TRANSACTION_DEADLOCK_RESTART_DELAY = 100;
const TRANSACTION_DEADLOCK_RESTART_DELAY = 50;

// Uses an already existing connection to query the database, returning a promise
function queryConnection(connection, sql, params) {
const deferred = q.defer();

const query = connection.query(sql, params, (error, result) => {
return (error) ?
deferred.reject(error) :
if (error) {
deferred.reject(error);
} else {
deferred.resolve(result);
}
});

winston.debug(`[Transaction] ${query.sql.trim()}`);
Expand Down Expand Up @@ -106,7 +107,7 @@ class Transaction {
if (error) { return deferred.reject(error); }

// with the acquired connection, get a transaction object
connection.beginTransaction(error => {
connection.beginTransaction((error) => {
if (error) { return deferred.reject(error); }

// map promises through to database queries
Expand All @@ -116,17 +117,15 @@ class Transaction {

// make sure that all queries are executed successfully.
return q.all(promises)
.then(results => {

.then((results) => {
// all queries completed - attempt to commit
connection.commit((error) => {
if (error) { throw error; }
connection.destroy();
deferred.resolve(results);
});
})
.catch(error => {

.catch((error) => {
// individual query did not work - rollback transaction
connection.rollback(() => {
connection.destroy();
Expand Down

0 comments on commit 567ec22

Please sign in to comment.