Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
Commit instead of rollback?

Simple example (PSEUDO CODE):

for (int i = 0; i < 100; i++) {
    START TRANSACTION;  
    SELECT id, name FROM employees WHERE id = i;

  
    IF (someFunction(id)) {
        ROLLBACK;
        CONTINUE; // GO TO NEXT EXECUTION OF FOR LOOP
    }

    UPDATE company SET good = good + 1;

    COMMIT;
}

Can I use in this example COMMIT (so I'm gonna have two COMMIT in my script) instead of ROLLBACK?

Does it make any difference to the database if I use COMMIT instead of ROLLBACK after select?

Is there any difference between MySQL and PostgreSQL here?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Select by itself does not require either rollback nor commit. Those are needed only after DML (insert, update, delete). Further it is typically considered better to have only 1 commit in a transaction. The idea being the entire transaction completely succeeds or completely as a unit. so your pseudo code becomes:

START TRANSACTION;   
for (int i = 0; i < 100; i++) {
    SELECT id, name FROM employees WHERE id = i;

    IF NOT (someFunction(id)) {
       UPDATE company SET good = good + 1;  
    }
}
COMMIT;
over 4 years ago · Santiago Trujillo Relatório

0

So I understand your question to be asking if ROLLBACK or COMMIT is better when, after only a select, you determine that no changes are going to be made in this transaction.

As far as mysql is concerned, there's no reason to do either rollback or commit; since no changes have been made, neither does anything, and doing neither causes no issues. But it is not clear what you hope to accomplish by having the select inside the transaction in the first place, or by having a separate transaction for each iteration of the loop. If you were to provide more information, you would get better advice.

over 4 years ago · Santiago Trujillo Relatório

0

Here is a variation of @Belayer's answer, in which I made the following changes:

  1. Perform a single SELECT, with the intention of reducing the number of queries
  2. Keep a running total of the number of times company.good should be incremented, before finally incrementing it with a single UPDATE.
new_good = 0;
SELECT id, name FROM employees WHERE id >= 0 AND i < 100
for each fetched row {
    IF NOT (someFunction(id)) {
       new_good++;
    }
}
START TRANSACTION /* Probably not needed */
UPDATE company SET good = good + new_good;  
COMMIT /* Probably not needed */;

This probably eliminates the need for a transaction entirely, since any failed call to SELECT or fetch will result in no change to new_good, so when it eventually gets to the UPDATE, new_good will still contain a valid value (even if 0).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda