Transactions Flashcards

1
Q

What is a transaction?

A

A transaction is a unit of work that might include multiple activities that query and modify data and that can also change the data definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What statement designates the beginning of a transaction?

A

BEGIN TRANSACTION;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What statement designates the end of a transaction?

A

COMMIT TRANSACTION;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What statement allows you to undo a transaction?

A

ROLLBACK TRAN or ROLLBACK TRANSACTION;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which session option allows the user to change how SQL Server handles implicit transactions?

A

IMPLICIT_TRANSACTIONS (turned off by default)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List the four properties of a transaction.

A

atomicity, consistency, isolation, and durability (ACID)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define atomicity in the context of a transaction.

A

Atomicity: a transaction is an atomic unit of work; either all changes in the transaction take place or none do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which session option will cause the encounter of any error, regardless if it is not severe enough to automatically trigger a rollback, to abort and rollback the transaction?

A

XACT_ABORT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define consistency in the context of a transaction.

A

The term consistency refers to the state of the data that the relational database management system (RDBMS) gives you access to as concurrent transactions modify and query it.

Consistency also refers to the fact that the database must adhere to all integrity rules that have been defined within it by constraints (primary keys, unique constraints, etc.)

The transaction transitions the database from one consistent state to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define isolation in the context of a transaction.

A

Isolation ensures that transactions access only consistent data.

You control what consistency means to your transactions though a mechanism called isolation levels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

describe the two models which SQL Server provides to handle isolation.

A

A model which uses locking (Default for SQL Server)

A model which uses a combination of locking and row versioning (Default for Azure SQL Database)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False:

In the model based on locking, readers require shared locks.

A

True:

If the current state of the data is inconsistent, readers are blocked until the state of the data becomes consistent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False:

In the model based on a combination of locking and row versioning, readers require shared locks.

A

False:

In this model, readers don’t take shared locks and don’t need to wait.

If the state of the data is inconsistent, the reader gets an older consistent state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define durability in the context of a transaction.

A

Data changes are always written to the database’s transaction log on disk before they are written to the data portion of the database on disk.

After the commit instruction is recorded in the transaction log on disk, the transaction is considered durable even if the change hasn’t yet made it to the data portion on disk.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe lock in the context of a transaction.

A

Locks are control resources obtained by a transaction to guard data resources, preventing conflicting or incompatible access by other transactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two main lock modes?

A

exclusive and shared.

17
Q

True or False:

lock interaction between transactions is known as lock compatibility.

A

True.

18
Q

Describe how locking works in SQL Server Box products

A

Data that was modified by one transaction can neither be modified nor read by another transaction until the first transaction finishes.

Additionally, while data is being read by one transaction, it cannot be modified by another.

19
Q

_______ are the physical data blocks that contain table or index data.

A

Pages.

Note: rows reside within pages.

20
Q

lock escalation is triggered when a single statement acquires at least _______ locks against the same object, and then for every _______ new locks, if previous attempts at lock escalation were unsuccessful.

A

5000, 1250

21
Q

You can set a table option called ________ by using the ALTER TABLE statement to control the way lock escalation behaves.

A

LOCK_ESCALATION

22
Q

In the context of SQL Server, what does DMV stand for?

A

Dynamic Management View

23
Q

Which DMV should be queried to find information about the current state of session locks?

A

sys.dm_trans_locks

The sys.dm_tran_locks view gives you information about the IDs of the sessions involved in the blocking chain.

24
Q

How do you find the database name when only its id is provided?

A

DB_NAME(dbid) AS dbname

25
Q

What is a blocking chain?

A

A blocking chain is a chain of two or more sessions that are involved in the block situation.

26
Q

SQL Server supports four isolation levels that are based on the pure locking model, name them.

A

READ UNCOMMITTED, READ COMMITTED (default), REPEATABLE READ, SERIALIZABLE.

27
Q

SQL Server supports two isolation levels that are based on a combination of locking and row versioning, what are they?

A

SNAPSHOT and READ COMMITTED SNAPSHOT.

28
Q

What is the command to set the isolation level for the whole session?

A

SET TRANSACTION ISOLATION LEVEL

29
Q

What is the lowest available isolation level?

A

READ UNCOMMITTED

In this isolation level, a reader doesn’t ask for a shared lock. A reader that doesn’t ask for a shared lock can never be in conflict with a writer that is holding an exclusive lock. This means that the reader can read uncommitted changes (also known as dirty reads).

30
Q

What is the lowest available isolation level that prevents dirty reads?

A

READ COMMITTED

As the name indicates, this isolation level allows readers to read only committed changes. It prevents uncommitted reads by requiring a reader to obtain a shared lock. This means that if a writer is holding an exclusive lock, the reader’s shared lock request will be in conflict with the writer, and it has to wait.

31
Q

When the transactions isolation level is set to REPEATABLE READ, how long are locks held?

A

REPEATABLE READ holds shared locks until the end of the transaction; therefore, exclusive locks cannot be obtained until after the transaction is complete. Therefore, you’re guaranteed to get a repeatable read of the rows that you read the first time in the transaction.

32
Q

Describe how the isolation level SERIALIZABLE differs from REPEATABLE READ

A

For the most part, the SERIALIZABLE isolation level behaves similarly to REPEATABLE READ: namely, it requires a reader to obtain a shared lock to be able to read, and it keeps the lock until the end of the transaction.

But the SERIALIZABLE isolation level adds another facet—logically, this isolation level causes a reader to lock the whole range of keys that qualify for the query’s filter.

This means that the reader locks not only the existing rows that qualify for the query’s filter, but also future ones. Or, more accurately, it blocks attempts made by other transactions to add rows that qualify for the reader’s query filter.

33
Q

What is a deadlock?

A

A deadlock is a situation in which two or more sessions block each other.

An example of a two-session deadlock is when session A blocks session B and session B blocks session A.

An example of a deadlock involving more than two sessions is when session A blocks session B, session B blocks session C, and session C blocks session A.

In any of these cases, SQL Server detects the deadlock and intervenes by terminating one of the transactions. If SQL Server did not intervene, the sessions involved would remain deadlocked forever.