V
V
vetsmen2018-03-04 13:03:42
MySQL
vetsmen, 2018-03-04 13:03:42

Why is the rest of the code not executed after the function call?

There is a module for working with transactions in mysql:

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 = promisify(connection.release).bind(connection);

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

When closing a connection:
let transaction = await connection.prepareTransaction();	

try {
  await transaction.beginTransaction();
  ...
  await transaction.commit();
  await transaction.release();
  // дальше код не выполняется никакой
}

No code is executed further, you can't even return. Why is that? And how to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Chernyshev, 2018-04-26
@IvanBlacky

Apparently the promise transaction.release()
never becomes fullfilled, so the await doesn't complete.
Try rewriting it as a Promise and see if anything gets executed in the then or catch block.

Example
let transaction = await connection.prepareTransaction();	

try {
  await transaction.beginTransaction();
  ...
  await transaction.commit();
  transaction.release()
    .then(res=>{
      console.log(res);
     })
    .catch(err=>{
      console.error(err);
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question