What is a transaction

Transaction is a set of operations for working with a database (DB), combined into one atomic bundle.





(It is assumed that you know what a database is. But a little later there will be a link to the article "what is it")





Transactional databases (databases that work through transactions) comply with ACID requirements, which ensure data security. Including financial data =) Therefore, the developers choose them.





I will talk about what a transaction is. How to open it and how to close it. And why is it important to close the transaction. And then, when writing queries to the database, you will have a conscious understanding of what is happening there, under the hood, and why you need this obligatory commit after the update.





Content

  • What is a transaction





  • How to send a transaction





  • How to open a transaction





  • How to close a transaction





  • Total









What is a transaction

A transaction is an archive for queries to the database. It protects your data on an all-or-nothing basis.





Imagine that you decide to send a friend 10 files in a messenger. What are the options:





  1. Throw each file separately.





  2. Put them in the archive and send the archive.









It seems that there is not much difference. But what if something goes wrong? The connection will drop in the middle, the server will go into reboot, or simply give an error ...





In the first case, your friend will receive 9 files, but not one.





. . , . , . , « ».









, , ? ? ? , . ? , . , ! , , !









, :





— ?





— 10





— ? 9... , .









, . 100 2 ? « 1», « 2» , «hfdslafebx63542437457822nfhgeopjgrev0000444666589.xml» ... ! , .









! — . «, ». , , .





, . "" :









delete from 1 where = 1





insert into 2 values ('')









« » . , 1 , 2 ... ...









. 1!





-, — , . — , - . , « », , .





 





, . ( . connection, ). —  , .





, . :





  1. .





  2. .





  3. .









, . , .





, , . . . — , .









, . —  .





. (, ), , -.





 





. Oracle , . MySql «start transaction».





 





2 :





  1. COMMIT — ;





  2. ROLLBACK —  ;









, «», . , , .





, :





insert into clients (name, surname) values ('', '');
--        «»   «»
      
      







, ! , select , , — ! .









! , . sql developer ( , ) select — .









, , :





insert into clients (name, surname) values ('', '');
commit;
      
      







. . . sql developer, , .





, : « » , ! ? .









. , . , . :





= «»





= «»





...









. select count —  . 100 ! , . .





, - ! «» «», - . ... . rollback.









—  ROLLBACK? . , . . .





. :





, «»;





495 499;





....









, . select , . «, , ? , ». .









- . . 3 , . 10 — , . , ...









. !









— , .





, (, insert , ...).









, . . ( connection) — , . — , , .





, . . :





  1. COMMIT — ;





  2. ROLLBACK —  ;









, -, . . - . rollback, . ? - . , , .





() . — , (update, delete…) , commit /rollback — .









PS - for more useful articles, look at my blog under the tag "useful" . And useful videos are on my youtube channel








All Articles