What is Transaction?

A series of operations on a database treated as a single unit. COMMIT makes the results permanent, and ROLLBACK returns the data to its state before the transaction began. The DBMS manages transactions with locks and logs so that they satisfy atomicity, consistency, isolation and durability (the ACID properties).

Japanese name: トランザクション

Transaction appears in 13 practice questions across 1 topic, including Database Basics.

Field: Database Basics

COMMIT and ROLLBACK

In a transfer between accounts, the result is correct only when both the debit from account A and the credit to account B have been made. A transaction groups such a series of operations into one unit: executing COMMIT at the end makes the results permanent, and executing ROLLBACK, for example when an error occurs partway through, cancels every update in the transaction. Uncommitted updates are also cancelled if a failure occurs, so no half-finished state with only some updates applied remains.

Concurrency control

When many users access a database at the same time, several transactions read and write the same data. Without any control, problems occur: one update can overwrite and lose another (a lost update), or a transaction can read uncommitted data (a dirty read). The DBMS prevents interference between transactions with concurrency control using shared and exclusive locks, or with multiversion concurrency control (MVCC), which keeps earlier versions of data for reads. How much interference to prevent is chosen with the isolation level.

Recovery from failures

The DBMS records the before and after values of updates in a log and writes the log to storage before the database itself. After a failure, it uses the log to redo the updates of committed transactions (rollforward) and undo the updates of uncommitted ones (rollback), restoring a correct state. Taking checkpoints regularly shortens the range of log that must be traced during recovery.

Try it: questions about Transaction

In an account table, a transaction is started, and an UPDATE that subtracts 300 from account A's balance and an UPDATE that adds 300 to account B's balance are executed. Then ROLLBACK is executed instead of COMMIT. What happens to the account table?

  1. AOnly the addition to account B is cancelled; the subtraction from account A remains
  2. BBoth UPDATEs are cancelled, and the table returns to its state before the transaction started
  3. CBecause the UPDATEs have already been executed, neither is cancelled
Show answer and explanation

Answer: B. Both UPDATEs are cancelled, and the table returns to its state before the transaction started

A transaction is a mechanism that treats a series of operations on a database as a single unit. Updates made within a transaction become permanent when COMMIT is executed, and are all cancelled when ROLLBACK is executed, returning the data to its state before the transaction started. In this example, both the subtraction from account A and the addition to account B are cancelled, and the two balances are unchanged. If an error occurs partway through a transfer, rolling back ensures that no half-finished state with only one account updated remains. To cancel only part of the work, you can mark a point with SAVEPOINT and return to it with ROLLBACK TO SAVEPOINT.

Among the ACID properties a transaction should have, which is the property that a transfer debiting account A and crediting account B is either carried out completely or not at all, even if a failure occurs partway through?

  1. AConsistency
  2. BDurability
  3. CAtomicity
Show answer and explanation

Answer: C. Atomicity

The ACID properties are named after the initials of four properties a transaction should satisfy. Atomicity means the operations in a transaction are either all executed or none are; if a failure occurs, the updates made so far are cancelled. Consistency means the data keeps satisfying integrity constraints (balances never negative, foreign keys always referring to existing rows, and so on) before and after the transaction. Isolation means that transactions running concurrently do not interfere with each other and produce the same result as if they had been executed one at a time. Durability means that the results of a committed transaction are not lost even if a failure occurs afterwards. The all-or-nothing property in the question is atomicity.

FAQ

What happens if an error occurs partway through a transaction?

Either the application executes ROLLBACK or the DBMS performs failure recovery, and every update in that transaction is cancelled. Only some of the updates are never left behind.

What problems arise from making a transaction long?

It holds locks for a long time, so other transactions wait, less work can be processed concurrently, and deadlocks become more likely. As a rule, a transaction should be kept as short as is needed to maintain consistency.

Transaction in the explanations

Questions that mention Transaction (13)

  1. In an account table, a transaction is started, and an UPDATE that subtracts 300 from account A's balance and an UPDATE that adds 300 to account B's balance are executed. Then ROLLBACK is executed instead of COMMIT. What happens to the account table?
  2. Among the ACID properties a transaction should have, which is the property that a transfer debiting account A and crediting account B is either carried out completely or not at all, even if a failure occurs partway through?
  3. The stock count of a product in a stock table is 10. Transaction T1 reads the stock count and writes back the value minus 3, and T2 reads the stock count and writes back the value minus 5. If they run without locks in the order T1 reads, T2 reads, T1 writes, T2 writes, what is the final stock count?
  4. Transaction T1 ran SELECT COUNT(*) FROM orders WHERE amount >= 1000 and got 8. Immediately afterwards, T2 inserted an order row with an amount of 2000 and committed. When T1 ran the same SELECT statement again, it got 9. What is this phenomenon?
  5. Transaction T1 holds a shared lock on a row and is reading it. T2 requests a shared lock and T3 requests an exclusive lock on the same row. Which is correct for the period until T1 releases its lock?
  6. Transaction T1 took an exclusive lock on row A and then requested a lock on row B, while T2 took an exclusive lock on row B and then requested a lock on row A, so neither can proceed. What does a DBMS typically do when it detects this situation?
  7. SQL statements are divided into the data definition language (DDL), data manipulation language (DML) and data control language (DCL). Which one is classified as DDL?
  8. What is the result of executing DELETE FROM employee in SQL without a WHERE clause?
  9. In a database used mainly for reading and aggregation, normalized tables are sometimes deliberately merged into one so that the same data is held in duplicate (denormalization). Which correctly pairs an advantage and a disadvantage of this design?
  10. Among the problems that occur when transactions run concurrently, which correctly describes a dirty read?
  11. Which correctly describes two-phase locking (2PL), used for concurrency control of transactions?
  12. When updating data, many DBMSs record the changes in a log (journal) and write that log to nonvolatile storage before the database itself (WAL: write-ahead logging). What is the main reason for this approach?
  13. There are transactions T1 to T5, and one checkpoint was taken before a failure occurred. T1 started and committed before the checkpoint. T2 and T3 started before the checkpoint; T2 committed after the checkpoint but before the failure, and T3 had not committed at the time of the failure. T4 and T5 started after the checkpoint; T4 committed before the failure, and T5 had not committed. In recovery after the failure, which transactions must be rolled forward using the log?

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

SQL ACID properties Isolation level Deadlock Normalization Two-phase locking (2PL) Database index Database view E-R diagram Foreign key

Back to the glossary