Mysql: How to run queries sequentially

Created on 5 Jul 2018  路  7Comments  路  Source: mysqljs/mysql

This is a very basic question and I am sorry to asking.
But basically I built a sync db app which listen to master db bin log and call my handlers. In my handlers I will do a query on slave db. As query function has callback. What is the best way of doing these queries right after each other.

needs more info question

All 7 comments

Without more information on what you're trying to do with example code, it's hard to give you any specific suggestions besides that you will need to implement a method to coordinate the activities. There are modules on npm like "async" that provide helpers to make async takes execute sequentially, for instance.

@dougwilson thanks for your answer . It's on event handler I used a 3rd party binlog reader and that emit an event as soon as binlog on master get changed and in my handler I will do a query but I need to wait till this query be done to process next one

rs.on('binlog', async function () {
  try{
      const client = this.conn;
      // should be wait to get the result and then handle next event
      return await client.query(query, values).then(res=>res);
    } catch (err) {
      console.log(err);
    }
});

Thanks for that example! I'm not really sure, is that from this module? This module doesn't support await or .then currently, and without understanding what the .query does and what that client object is, it's hard to give you an example that would work for you.

@dougwilson sorry you are write I mixed up the code with promise-mysql
here my example with your module

```
rs.on('binlog', async function () {
var con = mysql.createConnection({
host: "localhost",
user: "test",
password: "pass"
});

con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
//wait to this line be done and call back call before next event handle
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Result: " + result);
});
});
});
```

The simplest solution is to just create the connection outside of your event handler. Queries on the same connection are always run sequentially:

var con = mysql.createConnection({
  host: "localhost",
  user: "test",
  password: "pass"
});

rs.on('binlog', async function () {
 con.query(sql, function (err, result) {
   if (err) throw err;
   console.log("Result: " + result);
 });
});

in call back I have a redis to set the last position . what if one of those queries fails ? are the callbacks called sequentially ? in my real code the connection defined in class level property and build on construction. but I see it jumps to next handler without process last callback and that cause i set the pointer after one faster than failed one. so when I want to retry the queries I will jump to latest one not that actually has the error.

Yes, the callbacks are also called sequentially in my example. If you're having something different, I can take a look if you can provide all the code needed to demonstrate the issue that I can run and see it happening. If you are doing something more complex, you need to coordinate the sequentially of your event handlers with either your own code, or using helpers that npm modules like "async" provide.

But without you provide your full, complete code, I feel like I'm not going to be helpful. In isolation, the example I gave is the exact solution for the code you presented so far. If it's actually more complicated that you lead me to believe, I'm not surprised my example isn't working for you, but I cannot know that; I only know the information you provide.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rhapsody-Sky picture Rhapsody-Sky  路  3Comments

winzig picture winzig  路  4Comments

whatthehell232 picture whatthehell232  路  3Comments

skilbjo picture skilbjo  路  3Comments

DmitryEfimenko picture DmitryEfimenko  路  4Comments