Saturday, 22 April 2017

Nodejs Loopback transactions isolation READ_COMMITED and READ_UNCOMMITED not giving expected result

I am facing issue with transactions isolation level READ_UNCOMMITTED and READ_COMMITTED.

I have one table user having userid, name and schedule table dit, userid, enddate fileds

I am using transaction to insert data.

User.js

user.beginTransaction({isolationLevel: user.Transaction.READ_UNCOMMITTED}, function(err, tx_ua) {
  var userParams = {
          username: username,
          password: password,
          fname: fname,
          lname: lname
      }

      user.create(userParams, {transaction: tx_ua}, function(err, newUser) {
          if (err) {
            tx_ua.rollback(function(errr) {
              cb(err);
            });
          }

          Schedule.create({
              userId: newUser.id,
              endDate: endDate
          }, {transaction: tx_ua}, function(err, schedule) {
              if (err)
              {
                tx_ua.rollback(function(errr) {
                  cb(err);
                });
              }
              tx_ua.commit(function(err) {
                cb(null, 'xyz');
              });
          });
      });
}); 

Schedule.js

Schedule.observe('before save', function(ctx, next) {
    var context = LoopBackContext.getCurrentContext();
    var ua = context.get('currentUser');
    var filter = {};
    if (ctx.isNewInstance) {
        filter["where"]["id"] = ctx.instance.userId;
    } else {
        filter["where"]["id"] = ctx.currentInstance.userId;
        ctx.hookState.ChangedData = ctx.data;
    }
    app.models.User.findOne(filter, function(err, result) {
        if (err){
            next(err);
        }
        else if (result) {
            next()
        } else {
            var err = basehelper.customError("you are not allowed", 401, "NOT_ALLOWED");
            next(err);
        }
    });
});

When finding user in Schedule before save operational hook it is not returning result in both READ_UNCOMMITTED and READ_COMMITTED. Can anyone please guide what am I doing wrong?



via Sahil

No comments:

Post a Comment