Transactions in Databases

Reading time: 2 minutes

Transactions are one of the key mechanisms used in databases. They allow you to perform a series of operations, treating them as a single entity. All of them must succeed. If even one cannot be executed, the entire transaction is rolled back. Transactions therefore ensure that the integrity of the data in the database is maintained. 

What are actually transactions?

The best and simplest example of a transaction is a bank transaction, between two accounts. Suppose we want to transfer an amount X from account A to account B. We make a transfer order on account A, so the account balance decreases by X amount. So the next operation should be to increase the balance of account B by the amount X. What if there is a system failure between these operations? The money is gone – there is no money in either one account or the other. Their owners would certainly not be happy.

With help come database transactions that prevent such situations

Features of database transactions

Database transactions must meet the assumptions of ACID (the acronym is derived from the first letters of the following features):

A – Atomicity – states that all operations performed in a transaction are to be validated, and in the event of an error in even one of them, all are to be rolled back.

C – Consistency – means that regardless of whether a transaction is approved or withdrawn, the information stored in the database will remain consistent.

I – Isolation – says that different transactions executed at the same time cannot affect each other. There are different levels of isolation.

D – Durability – ensures that the effects of approved transactions are preserved, for example, in case of hardware failures.

A database transaction is started with the BEGIN TRANSACTION command, approved with the COMMIT TRANSACTION command, and rolled back with the ROLLBACK TRANSACTION command.

However, transactions can affect database performance. Their number and complexity can significantly slow down the system, so their implementation must be well thought out and planned.

Summary

Thus, transactions are a solution to many problems, especially in a complex database system. The ability to use them is basic even for a novice developer. Only the basics of transaction knowledge have been presented in this article, but it is a good place to start.

Facebook
Twitter
LinkedIn

Contact

Nearshore Partner means for you:

Trusted us

Contact

Ola Wojdyła

Contact

Nearshore Partner means for you:

Trusted us