Rollback does not work

Asked

Viewed 66 times

3

So guys I have a transaction to persist various data on different tables in my database, if by some chance a record goes wrong I need it from a rollback, after all I depend on the return id of one to make persistence of another, Moreover I cannot allow some tables to be persisted and others not. The problem is that my rollback is not working, I have some methods, they are with a Promise so if it gives error it gives a Retject and in my vision it would fall for the catch of the runQuery method, there is a rollback and this rollback would make me undo the operations, the problem is that the errors are not falling there, and in case the mistakes I am generating are several only for testing.

async run(data: any) {
try {
  const response = await this.runQuery(data);


  return 'response';
} catch (err) {
  throw err;
}}

async runQuery(data: any) {
try{
  connection.beginTransaction();  

  data[2].post.Ordem_servico = await this.insertGenericReturn(data[0]);

  await this.insertEpisOrder(data[6], data[2].post.Ordem_servico);

  data[5].post.Equipamento_FK = await this.insertGenericReturn(data[2]);

  data[3].post.Ordem_Servico = data[2].post.Ordem_servico;

  data[5].post.Locais_FK = await this.insertGenericReturn(data[3]);

  let listOperations : any = await this.insertOperationsOrder(data[4]);


  for (const element of listOperations) {
    data[5].post.Operacao_FK = element;
    await this.insertGenericReturn(data[5]);
  }

  connection.commit();

  return { result: 'Ordem de serviço criada!' }
} catch (err) {
  console.log('err runTransaction :>> ', err);

  await connection.rollback();
  throw err;
}}

private async insertGenericReturn(data: any) {
try {
  return new Promise((resolve, reject) => {
    connection.query(data.query, data.post,  async (err: any, result: any) =>{
      if (err) reject({ status: 401, msg: 'Não foi possível realizar a operação!', ...err })
      resolve(result.insertId);
    });
  });
} catch (err) {
  throw err;
}}

private async insertEpisOrder(data: any, order: any) {
try {
  return new Promise((resolve, reject) => {
    for (let index = 0; index < data.post.length; index++) {
      data.post[index].ordemServico_idOrdemServico = order;
    }
    for (const epi of data.post) {
      connection.query(data.query, epi, async (err: any, result: any) =>{
        if (err) reject({ status: 401, msg: 'Não foi possível realizar a operação!', ...err })
        resolve('EPIs cadastrados');
      });
    }
  });
} catch (err) {
  throw err;
}}

private async insertOperationsOrder(data: any) {
try {
  return new Promise((resolve, reject) => {
    let listOperations: any = [];
    let response: any;
    for (const item of data.post) {
      connection.query(data.query, item, async (err: any, result: any) =>{
        if (err) reject({ status: 401, msg: 'Não foi possível realizar a operação!', ...err })

        listOperations.push(result.insertId);

        resolve(listOperations);
      });
    }
  });
} catch (err) {
  throw err;
}}

Does anyone have any idea why I wouldn’t be going to my rollback? type it goes to those insertion methods but no return to the runQuery catch that has the rollback, if you can help thank you!

1 answer

0

Guys I solved here and I’ll share the resolution, maybe it will be useful to someone:

I added a treatment in the catch method is this

private getQueryError(err: any): { status: number, err: string } {
const error = Object.assign({}, err);

if (_.has(error, 'code')) return { status: 400, err: 'Não foi possível concluir a operação!' };
if (error.code === 'ER_DUP_ENTRY') return { status: 400, err: 'item já cadastrado!' };
return { status: 400, err: _.get(error, 'message', 'Não foi possível concluir a operação!') }}

And obviously added calls to this method in my catch in case:

const error = this.getQueryError(err);

Finally I hope I can help someone who is having this difficulty or who is working with transactions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.