liveBook · Manning

Concurrency

As explained in Java Persistence with Spring Data and Hibernate epub

Concurrency is the execution or management of multiple activities whose operations may overlap in time.

In a database application, concurrency means that multiple transactions may access or change data during overlapping periods. An auction-closing operation, for example, may update the winning bid while another transaction is working with the same data. Transactions need boundaries: start the work, commit it on success, or roll it back on failure. Keep database transactions short because open transactions consume resources and exclusive locks can prevent concurrent access. section 11.1.2 section 11.2.1

Two updates, one surviving value

Transaction A reads a price. Transaction B reads the same price before A finishes. A writes its new price; B then writes its own, overwriting A’s change. This is a lost update: without effective concurrency control, the last commit wins. section 11.2.1

Other anomalies depend on what one transaction can observe while another runs:

  • Dirty read: a transaction reads another transaction’s uncommitted change, which may later be rolled back. section 11.2.1
  • Unrepeatable read: reading the same data twice produces different values after another transaction commits a change. section 11.2.1
  • Phantom read: repeating a query returns a different set of rows because another transaction added or deleted data. section 11.2.1

Isolation controls which effects of concurrent transactions can become visible. Complete isolation is costly in a multiuser system, so isolation levels trade some protection for performance and scalability. Choose a level by examining the anomalies an operation must avoid, rather than assuming every transaction requires the strongest setting. section 11.2.1

Table 11.1 ANSI isolation levels and the problems that they address (view table figure)

Isolation level

Phantom read

Unrepeatable read

Dirty read

Lost update

READ_UNCOMMITTED

–

–

–

+

READ_COMMITTED

–

–

+

+

REPEATABLE_READ

–

+

+

+

SERIALIZABLE

+

+

+

+

Compare the ANSI isolation levels against the listed read and update anomalies when deciding what protection a transaction needs. section 11.2.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains four anomalies through transaction interleavings and compares isolation levels, without detailing lock algorithms.
Examples
Uses competing price updates, uncommitted changes, changing bids, and repeated queries to illustrate anomalies.
What it assumes
Assumes basic familiarity with database reads and writes while defining the concurrency anomalies through examples.
What you can do after
Evaluate/understand overlapping transactions by identifying anomalies and comparing the protection offered by isolation levels.
How it connects
Connects concurrency to transaction boundaries, rollback, exclusive locks, and transaction isolation.

Also explained in

Compare how 23 books explain concurrency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.