What is a database transaction?

119,481

Solution 1

A transaction is a unit of work that you want to treat as "a whole." It has to either happen in full or not at all.

A classical example is transferring money from one bank account to another. To do that you have first to withdraw the amount from the source account, and then deposit it to the destination account. The operation has to succeed in full. If you stop halfway, the money will be lost, and that is Very Bad.

In modern databases transactions also do some other things - like ensure that you can't access data that another person has written halfway. But the basic idea is the same - transactions are there to ensure, that no matter what happens, the data you work with will be in a sensible state. They guarantee that there will NOT be a situation where money is withdrawn from one account, but not deposited to another.

Solution 2

A transaction is a way of representing a state change. Transactions ideally have four properties, commonly known as ACID:

  • Atomic (if the change is committed, it happens in one fell swoop; you can never see "half a change")
  • Consistent (the change can only happen if the new state of the system will be valid; any attempt to commit an invalid change will fail, leaving the system in its previous valid state)
  • Isolated (no-one else sees any part of the transaction until it's committed)
  • Durable (once the change has happened - if the system says the transaction has been committed, the client doesn't need to worry about "flushing" the system to make the change "stick")

See the Wikipedia ACID entry for more details.

Although this is typically applied to databases, it doesn't have to be. (In particular, see Software Transactional Memory.)

Solution 3

Here's a simple explanation. You need to transfer 100 bucks from account A to account B. You can either do:

accountA -= 100;
accountB += 100;

or

accountB += 100;
accountA -= 100;

If something goes wrong between the first and the second operation in the pair you have a problem - either 100 bucks have disappeared, or they have appeared out of nowhere.

A transaction is a mechanism that allows you to mark a group of operations and execute them in such a way that either they all execute (commit), or the system state will be as if they have not started to execute at all (rollback).

beginTransaction;
accountB += 100;
accountA -= 100;
commitTransaction;

will either transfer 100 bucks or leave both accounts in the initial state.

Solution 4

"A series of data manipulation statements that must either fully complete or fully fail, leaving the database in a consistent state"

Solution 5

A transaction is a sequence of one or more SQL operations that are treated as a unit.

Specifically, each transaction appears to run in isolation, and furthermore, if the system fails, each transaction is either executed in its entirety or not all.

The concept of transactions is motivated by two completely independent concerns. One has to do with concurrent access to the database by multiple clients, and the other has to do with having a system that is resilient to system failures.

Transaction supports what is known as the ACID properties:

  • A: Atomicity;
  • C: Consistency;
  • I: Isolation;
  • D: Durability.
Share:
119,481

Related videos on Youtube

Vlad Gudim
Author by

Vlad Gudim

Scala programmer? Please read this answer.

Updated on March 16, 2021

Comments

  • Vlad Gudim
    Vlad Gudim about 3 years

    Can someone provide a straightforward (but not simpler than possible) explanation of a transaction as applied to computing (even if copied from Wikipedia)?

  • Michał Chaniewski
    Michał Chaniewski almost 15 years
    Not only database. We can extend this concept to other components - like queuing service, or state of external systems. So, "A series of data manipulation statements that must either fully complete or fully fail, leaving the system in a consistent state"
  • Ganbayar Gansukh
    Ganbayar Gansukh over 8 years
    Thanks for the very simple example
  • Yan Myo Aung
    Yan Myo Aung over 5 years
    nice explaination!