Home  >  Q&A  >  body text

javascript - Concurrent access to Node is inconsistent with the expected result

app.get('/', function (req, res) {
    var now = +(new Date())
    connection.query('select count(*) from ACTIVITY group by name', function (err, result, fields) {
        var curr = +(new Date())
        var tmp = '耗时:' + (curr - now)
        console.log(tmp)
        res.send(tmp)
    })
})

The following is a supplement

app.get('/', function (req, res) {
    var now = +(new Date())
    pool.getConnection(function (err, conn) {
        console.log('--连接池连接成功!' + +(new Date()))
        conn.query('select count(*) from ACTIVITY group by name', function (err, result, fields) {
            var curr = +(new Date())
            var tmp = '耗时:' + (curr - now)
            console.log(tmp)
            res.send(tmp)
        })
    })
})
怪我咯怪我咯2647 days ago760

reply all(1)I'll reply

  • 某草草

    某草草2017-06-22 11:56:23

    The start of time is before query and the end is when query is completed, so each time is the time when query is running,

    Node is asynchronous, but you are using the same connection. Does the connection itself need to be queued? As far as I know, the SQL executed by most databases in the same connection is queued and executed one after another... Multiple connections may be parallel.

    reply
    0
  • Cancelreply