Transaction is a set of consecutive operations with the database, which comprises a logical unit of work with data. The transaction can be executed both completely and successfully, while maintaining data integrity and being independent from the transactions which may be performed simultaneously; or can not be executed at all. In the latter case it should not cause any changes.
Since the 7th version, Drupal provides an opportunity to support transactions including those databases that do not support them. However if you try to execute two transactions at the same time it can complicate the process of their performance. In this case their behavior will depend on the type of database used.
The same problem arises with embedding in C/C++. If the code has already locked the table A and tries to block it again, you’ll get stumped. But if a Drupal developer writes a code that checks whether there is any blocking and makes the second attempt only in the case of its absence, then such misunderstandings can be avoided. However this can lead to premature blocking removal while you may still need it.
There is a similar problem in SQL. E.g. if your code is running in a transaction and at the same moment a new one starts, we can get unpredictable outcome when one transaction is executed and another begins.
Java solved the embedding problem with blocking in another way. Supporting embedded structures Java allows you to mark a function as ‘synchronized’, which makes the latter wait until the blocking is possible, or remove the lock when it is no longer necessary.
Although it is not possible to write "transaction" function in PHP, one can follow the Java embedded logic using objects with constructors and destructors. For this reason Drupal designed a wrapper class for creating and managing transactions in databases - class DatabaseTransaction.
It is used in the function "$txn = db_transaction();" as the first operation to make transaction from the current function, in which it is invoked.