Question

I'm using a script that first sets the status of all my rows to 0, then individually sets the status of some of the rows to different values. This is done once per minute. I've already noticed multiple times that I can do a SELECT on my database and get completely incorrect results as many or all of my rows has the 0 status. This seems to be a gap of one second per minute where SELECTs will get incorrect data.

I've thought of using the sequelize QueryChainer, but that still isn't enough since a select query could easily fetch some incorrect data while the chainer is doing it's thing.

So I was wondering if sequelize has a method of ensuring that all of my queries are held back and sent at once, since I'm guessing that it doesn't have actual TRANSACTION support.

Was it helpful?

Solution

You are right, there is currently no transaction-support. Your problem actually sound like you are doing something like:

sequelize.query('UPDATE mytable SET expired=0').success(function() {
  Mytable.findAll(/* conditions */ ).success(function(entries) {
    entries.forEach(function(entry){ entries.updateAttributes({expired:1}) })
    Mytable.findAll().success(function(entries){
      // check status of entries
    })
  })
})

This code updates some entries but doesn't wait for it to be done. If you have something like this, you should use the QueryChainer, like so:

var chainer = new Sequelize.Utils.QueryChainer

entries.forEach(function(entry) {
  chainer.add(entry.updateAttributes({ expired: 1 }))
})

chainer.run().success(function() {
  // now go on here
})

If you don't have the above mistake and want to collect all operations first and execute them in bulk afterwards (and serially), do this:

var chainer = new Sequelize.Utils.QueryChainer

entries.forEach(function(entry) {
  chainer.add(entry, 'updateAttributes', [{ expired: 1 }])
})

chainer.runSerially().success(function() {
  // now go on here
})

Hope that helps

OTHER TIPS

As of commit cf8cd6eb769f2470b58c95e49114c05cdd1e3653 (late November 2013), transactions are now supported in Sequelize. This has recently been made available in npm (1.7.0 and 2.0.0 branches). The API is described in the pull request.

You need something like:

sequelize.transaction(function(t) {
   Table.update({status: 0}, {}, { transaction: t });
   t.commit().success(function () { ... });
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top