V
V
vetsmen2018-03-03 10:08:22
MySQL
vetsmen, 2018-03-03 10:08:22

Why after a while all mysql-sessions do not work?

Until recently everything was fine. Now I have transferred one module to transactions, here is the code:

The module itself
socket.on('sell', async (id) => {
      try {
        id = parseInt(id, 10);

        if(!socket || !socket.userid) {
          return;
        }

        if(!id) {
          return;
        }

        let transaction = await connection.prepareTransaction();
        
        try {
          await transaction.beginTransaction();

          let item = await transaction.query('SELECT * FROM Items WHERE id = ? FOR UPDATE', [id]);

          if(!item || !item[0]) {
            await transaction.rollback();
            transaction.release();

            return;
          }

          if(!item[0].userid) {
            await transaction.rollback();
            transaction.release();

            return;
          }

          await transaction.query('UPDATE Items SET ? WHERE id = ?', [{status: 0, userid: null, timestamp: 0}, id]);

          await transaction.commit();
          transaction.release();
        } catch (e) {
          await transaction.rollback();
          transaction.release();
        }
      } catch (error) {
        throw error;
      }
    });
mysql handler
const mysql = require('mysql');
const { promisify } = require('util');

const pool = mysql.createPool({
  host: config.host,
  user: config.user,
  password: config.password,
  database: config.database,
  socketPath: '/var/run/mysqld/mysqld.sock',
  connectionLimit: 100
});

exports.prepareTransaction = () => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
          if (err) throw err;

          const query = promisify(connection.query).bind(connection);
          const commit = promisify(connection.commit).bind(connection);
          const rollback = promisify(connection.rollback).bind(connection);
          const beginTransaction = promisify(connection.beginTransaction).bind(connection);
          const release = connection.release;

          resolve({beginTransaction, query, commit, rollback, release});
        });
    });
};

After a certain time (depending on the activity of users), mysql falls off, and no error arrives. The only version - all available connections are clogged, tk. connection.release() is not done, but in my case it seems to be everywhere. Thus, when connecting, the client waits for an available session, but it will never happen again.
After a reboot everything works fine again and after an hour or a few hours mysql crashes again. This was not the case before the update.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Skibin, 2018-03-03
@megafax

Monitor SHOW PROCESSLIST and htop for an hour, so you'll know for sure if your guess is correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question