0

When doing transactions using promises in the sql2 npm lib this way:

try {
      const db = await getDatabase(); // this return a connection pool
      const connection = await db.getConnection();
      await connection.beginTransaction();

      await connection.execute("...query1");
      await connection.execute("...query2");
      await connection.execute("...query3");

      await connection.commit();
      await connection.release();

      return {
        success: true,
      };
    } catch (error) {
      console.error(error);
      return {
        success: false,
      };
    }

Suppose one of the 3 queries throws an error. What will happen next? Will the lock from the transaction be released? will the connection to perform this transaction be released? If not, what should be the right way of doing transactions then?

0 Answers0