Answer the question
In order to leave comments, you need to log in
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:
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;
}
});
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});
});
});
};
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question