Doubt rollback in PHP / MySQL

1

I am starting to use TRANSACTIONS in PHP / MySQL but I am observing the following situation:

In my tests I have noticed that rollback is not necessary although I have read that they are always put to return the data to its original state if an error occurs, but my doubt arises from the following example:

1.- deshabilito autocommit.
2.- realizo las querys necesarias.
3.- valido si se realizaron todas la querys.
4.- SI SE realizaron todas lanzar commit (guardar los datos de forma permanente).
5.- SI NO se realizaron todas lanzar rollback (retroceso de datos).

In this situation my question is this:

I understand that when you disable autocommit , nothing will be registered in the database, until you execute a commit . That said I do not see a rollback necessary, since they were never registered in the database.

    
asked by Enrique Navarro 20.06.2018 в 08:02
source

1 answer

0

I understand that when disabling autocommit, nothing will be registered in the database, until executing a commit, saying that I do not see a rollback necessary, since they were never registered in the database.

It's necessary to do a ROLLBACK whenever you're going to keep making queries since a new transaction will make COMMIT of the previous one :

  

Statements That Cause an Implicit Commit:

     
  • [...]

  •   
  • Transaction-control and locking statements. BEGIN , LOCK TABLES ,    SET autocommit = 1 (if the value is not already 1), START TRANSACTION , UNLOCK TABLES .

  •   
  • [...]

  •   

In Spanish:

  

Queries that cause an implicit COMMIT:

     
  • [...]

  •   
  • Transaction control and blocking queries. BEGIN , LOCK TABLES , SET autocommit = 1 (if the value was not already 1), START TRANSACTION , UNLOCK TABLES .

  •   
  • [...]

  •   

If you are completely sure that you will not make any further queries you may not do the ROLLBACK for MySQL to make an implicit one at the end of the connection, but I prefer (and I recommend) doing it explicitly.

    
answered by 20.06.2018 в 10:00