Linux Application Development (2nd Edition)

 

What is a Transaction?

A transaction is an atomic unit of work that must be completed in its entirety. The transaction succeeds if it is committed and it fails if it is aborted. Transactions have four essential attributes: atomicity, consistency, isolation, and durability (known as the ACID attributes).

Concurrency Models and Database Locking

The attributes of consistency and isolation are implemented by using the database locking mechanism, which keeps one transaction from affecting another. If one transaction needs access to data that another transaction is working with, the data is locked until the first transaction is committed or rolled back. Transactions that need to access locked data are forced to wait until the lock is released, which means that long-running transactions can affect performance and scalability. The use of locks to prevent access to the data is known as a "pessimistic" concurrency model.

In an "optimistic" concurrency model, locks are not used when the data is read. Instead, when updates are made, the data is checked to see if the data has changed since it was read. If the data has changed, an exception is thrown and the application applies business logic to recover.

Transaction Isolation Levels

Complete isolation can be great, but it comes at a high cost. Complete isolation means that any data that is read or written during a transaction must be locked. Yes, even data that is read is locked because a query for customer orders should yield the same result at the beginning of a transaction and at the end of the transaction.

Depending on your application, you might not need complete isolation. By tweaking the transaction isolation level, you can reduce the amount of locking and increase scalability and performance. The transaction isolation level affects whether you experience the following.

Table 10-1 lists the transaction isolation levels along with their effects. It also shows the concurrency model that the isolation level supports.

Table 10-1: Isolation Levels in SQL Server 2005

Level

Dirty Read

Nonrepeatable Read

Phantom Read

Concurrency Model

Read Uncommitted

Yes

Yes

Yes

None

Read Committed with Locks

No

Yes

Yes

Pessimistic

Read Committed with Snapshots

No

Yes

Yes

Optimistic

Repeatable Read

No

No

Yes

Pessimistic

Snapshot

No

No

No

Optimistic

Serializable

No

No

No

Pessimistic

Here are details on each concurrency level.

The version store retains row version records after the UPDATE or DELETE statement has committed, until all active transactions have committed. The version store essentially retains row version records until all of the following transaction types have committed or ended:

Single Transactions and Distributed Transactions

A transaction is a unit of work that must be done with a single durable resource (such as a database or a message queue). In the .NET Framework, a transaction typically represents all of the work that can be done on a single open connection.

A distributed transaction is a transaction that spans multiple durable resources. In the .NET Framework, if you need a transaction to include work on multiple connections, you must perform a distributed transaction. A distributed transaction uses a two-phase commit protocol and a dedicated transaction manager. In Windows, the dedicated transaction manager for managing distributed transactions is the Distributed Transaction Coordinator (DTC). (This is covered in more detail later in the chapter.)

 

Категории