Chapter 12: Manage Transactions, Error Handling & Dynamic SQL Flashcards

1
Q

What’s a transaction?

A

It’s an unit of work: it’s all done or none, that’s partial result it’s not acceptable, it’s all or nothing.

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

What’s ACID?

A

It’s the properties that defines an database transaction. ACID stands for:

A: Atomicity. Every transaction is an atomic unit of work, that is, all database changes in the transactions are succeed or none of them succeed.
C: Consistency. Every transaction, whether successful or not, need to leave the database in a consistent state. If a inconsistent occur when a transaction happens, then it’ll rollback to the previous state, where the consistency was.
I: Isolated. A transaction don’t need to interference with another database transactions.
D: Durability. This property ensures that once the transaction has completed execution, the updates and modifications to the database are stored in and written to disk and they persist even is system failure occurs. These updates now become permanent and are stored in a non-volatile memory. The effects of the transaction, thus, are never lost.

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

Why is it important for SQL Server to maintain the ACID quality of transactions?

A

To ensure that the integrity of database data will not be compromised.

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

How does SQL Server implement transaction durability?

A

By first writing all changes to the database transaction log before making changes to the database data.

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

What are the types of transactions?

A

System transactions and user transactions.

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

What are the typicals commands for handle transaction?

A

BEGIN TRANSACTION tells that all codes below belong to this transaction.
COMMIT TRANSACTION tells that the changes needs to be commited to the database.
ROLLBACK TRANSACTION tells that all the changes will be reversed.

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

What are the functions to know the transaction level and its states?

A

@@TRANCOUNT and XACT_STATE()

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

What are the modes to work with SQL Server database transactions?

A

Autocommit mode,
Implicit Transaction mode,
Explicit Transaction mode

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

What does do the @@TRANCOUNT?

A

Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection.

The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.

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

Review and practice

A

IMPLICIT TRANSACTION MODE AND EXPLICIT TRANSACTION MODE

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

What’s a nested transaction?

A

When a transaction contains another transaction inside them, and so on.

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

Exam tip about nested transaction and COMMIT

A

An inner COMMIT statement has no real effect on the transaction, only decrementing
@@TRANCOUNT by 1. Just the outermost COMMIT statement, the one executed when
@@TRANCOUNT = 1, actually commits the transaction.

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

Exam tip about nested transaction and ROLLBACK

A

Note that it doesn’t matter at what level you issue the ROLLBACK command. A transaction
can contain only one ROLLBACK command, and it will roll back the entire transaction and
reset the @@TRANCOUNT counter to 0.

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

What are the aditional transaction options?

A

Savepoints, Cross - database transactions and Distributed transactions.

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

How many ROLLBACKs must be executed in a nested transaction to roll it back?

A

Only one ROLLBACK. A ROLLBACK always rolls back the entire transaction, no matter how many levels the transaction has.

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

How many COMMITs must be executed in a nested transaction to ensure that the entire transaction is committed?

A

One COMMIT for each level of the nested transaction. Only the last COMMIT actually commits the entire transaction.

17
Q

What are the modes of locking?

A

Shared locks: used for sessions that read data.

Exclusive locks: used for changes to data.

18
Q

What’s deadlock?

A

Let’s say we have two transactions: T1 and T2. T1 updates a database object called O1. T2 updates another database object called O2. So far, everything it’s fine. Now, T1 wants to update the object O2. It get’s blocked until T2 release that object. But now, T2 wants to update O1, it’s also get blocked because T1 locked O1. So this cause a deadlock between two transactions (T1 and T2). What SQL Server does it’s kill one of them, and let the other finish his process.

19
Q

If two transactions never block each other, can a deadlock between them
result?

A

No. In order to deadlock, each transaction must already have locked a resource the other transaction wants, resulting in mutual blocking.

20
Q

Can a SELECT statement be involved in a deadlock?

A

Yes. If the SELECT statement locks some resource that keeps a second transaction from finishing, and the SELECT cannot finish because it is blocked by the same transaction, the deadlock cycle results.

21
Q

NOTE

A

Read the page marked as favorite for understand better read uncommited, and so on. Also, check the chapter 12.sql

22
Q

What’s the default isolation level in a session?

A

The READ COMMITED, which permits non repeatable reads and phantom reads.

23
Q

If your session is in the READ COMMITTED isolation level, is it possible for one of your queries to read uncommitted data? (REVIEW THIS QUESTION)

A

Yes, if the query uses the WITH (NOLOCK) or WITH (READUNCOMMITTED) table hint. The session value for the isolation level does not change, just the characteristics for reading that table.

24
Q

Is there a way to prevent readers from blocking writers and still ensure that readers only see committed data?

A

Yes, that is the purpose of the READ COMMITTED SNAPSHOT option within the READ COMMITTED isolation level. Readers see earlier versions of data changes for current transactions, not the currently uncommitted data.

25
Q

How SQL Server alerts that a error has come?

A

When the @@ERROR function returns an positive integer value ( > 0).

26
Q

What are the parts of an Error Message in SQL Server?

A

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword ‘FROM’.

Msg (Error Number): gives a description of the error, such like “Incorrect syntax near the keyword ‘T - SQL Keyword’ “. This error number can be found in the sys.messages view.

Level (Error Level): determines the severity of the error, and his value is from 0 to 25, whereas 0 to 10 are warning, 11 to 16 are user errors, from 17 to 19 are serious exceptions and from 20 to 25 are fatal connections and server level exceptions

State (Error State): it’s an internally error message SQL Server.

27
Q

What happen to a TSQL code which is not inside of a TRY/CATCH block?

A

The error message will be passed through to the client and cannot be intercepted in T SQL code.

28
Q

How We can raise our own error messages?

A

With two commands: RAISERROR or THROW command.

29
Q

Can THROW be used without parameters?

A

Yes, it can.

30
Q

Exam tip about THROW

A

The statement before the THROW statement must be terminated by a semicolon (;). This reinforces the best practice to terminate all T-SQL statements with a semicolon.

31
Q

How can you add custom error messages?

A

You can use the system stored procedure sp_addmessage to add your own custom error messages.

32
Q

NOTE FOR HANDLE ERROR:

A

Review the T SQL code wrote.

33
Q

NOTE FOR DYNAMIC SQL:

A

Review Dynamic SQL.

34
Q

What does do the XACT_ABORT property?

A

When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back.

When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing. Depending upon the severity of the error, the entire transaction may be rolled back even when SET XACT_ABORT is OFF. OFF is the default setting.