Паруса Транзакция DeadLock - PullRequest
0 голосов
/ 25 января 2019

В настоящее время я занимаюсь разработкой приложения под Sails 1.1.0 .Далее вы можете найти, когда код, кажется, вызывает взаимоблокировку .

По сути, речь идет о пользователе, посещающем web_url, который создает для него модели Token и UnassignedGame.Во время этого процесса создания я иногда получал DeadLock в драйвере MySQL.

Можете ли вы сказать мне, если вы видите какие-либо ошибки или, в конечном итоге, лучшую версию или лучший способ ее обработки.

Спасибо

Token.js

module.exports = {
  attributes: {
    exp: {
      type: 'ref',
      columnType: 'datetime',
      required: true
    },
    value: {
      type: 'string',
      required: true
    },
    unassignedGame: {
      model: 'unassignedgame',
    }
  },
};

UnassignedGame.js

module.exports = {
  attributes: {
    score: {
      type: 'number',
      required: true
    },
    token: {
      model: 'token',
    }
  },
};

Контроллер

var flaverr = require('flaverr');

module.exports = {

  fn: async function (req, res) {
    let slug = this.req.param("slug");
    let token = this.req.param("token");
    var currentDateMoment = sails.moment.utc(new Date);
    currentDateMoment.add(2, 'h');
    let expirationDate = new Date(currentDateMoment.valueOf());
    var newToken = null;
    var newUnassignedGame = null;

    try {
      await sails.getDatastore().transaction(async (db) => {
        newToken = await Token.create({ exp: expirationDate, value: token }).usingConnection(db).fetch();
        if (!newToken) {
          throw flaverr('E_CANT_CREATE_TOKEN');
        } else {
          newUnassignedGame = await UnassignedGame.create({ score: 0, token: newToken.id }).usingConnection(db).fetch();
          if (!newUnassignedGame) {
            newToken = null;
            throw flaverr('E_CANT_CREATE_UNASSIGNED_GAME');
          }
        }
      }).intercept(
        [
          'E_CANT_CREATE_TOKEN',
          'E_CANT_CREATE_UNASSIGNED_GAME'
        ], () => 'E_FAILED_START'
      )
    } catch (err) {
      if (err.raw == "E_FAILED_START") {
        return this.res.redirect('/');
      } else {
        throw err;
      }
    }
    if (newToken != null && newUnassignedGame != null) {
      let tokenUpdated = await Token.update({ value: token }).set({ unassignedGame: newUnassignedGame.id }).fetch();
      if (!tokenUpdated) {
        return this.res.redirect('/');
      }
    }
    return this.res.view('game');
  }
}
...