Transactions، Isolation Level و In-Memory OLTP | Pro SQL Server 2019 Administration

Transactions، Isolation Level و In-Memory OLTP

توسط admin | گروه SQL Server | 1405/05/20

نظرات 0

Transactions، Isolation Level و In-Memory OLTP

Chapter 18 — Transactions, Isolation Levels and In-Memory OLTP

نویسنده: Peter A. Carter

زبان منبع: انگلیسی

محدوده: صفحات PDF 677 تا 693

تاریخ ترجمه: 2026-08-11

اعتبار ترجمه: ترجمه با کمک هوش مصنوعی

PAGE-677

کاهش Deadlock و Transactions

برای کاهش Deadlock، Objectها را در ترتیب ثابت Access کنید، Transaction را کوتاه نگه دارید، Index مناسب ایجاد کنید، User Interaction را داخل Transaction نگه ندارید و Error 1205 را با Retry محدود Handle کنید.

PAGE-678

ACID — Atomic

Atomic یعنی Transaction یا کامل Commit می‌شود یا هیچ اثر نهایی ندارد. Savepoint اجازه Rollback بخشی از Transaction را می‌دهد، اما Commit نهایی هنوز کل Transaction را Durable می‌کند.

SAVE TRANSACTION SavePoint1;
-- عملیات
ROLLBACK TRANSACTION SavePoint1;
PAGE-679

Database/داده نمونه برای نمایش ACID و Isolation ساخته و تراکنش‌های کنترل‌شده روی آن اجرا می‌شوند.

PAGE-680

Database/داده نمونه برای نمایش ACID و Isolation ساخته و تراکنش‌های کنترل‌شده روی آن اجرا می‌شوند.

PAGE-681

Consistent و Isolated

Consistency یعنی Transaction Database را از State معتبر به State معتبر ببرد و Constraint/Ruleها حفظ شوند. Isolation تعیین می‌کند Transactionهای همزمان تا چه حد تغییرات یکدیگر را ببینند.

Figure 18-3 — شکل/تصویر منبع، صفحه PDF 681
PAGE-682

Transactional Anomalies

Dirty Read خواندن داده Commit‌نشده است؛ Nonrepeatable Read یعنی یک Row در دو Read یک Transaction مقدار متفاوت داشته باشد. Isolation Levelهای بالاتر برخی یا همه این پدیده‌ها را جلوگیری می‌کنند.

Table 18-7 — بازنمایی متن فنی جدول منبع
--- PDF PAGE 682 ---
675
Transactional Anomalies
Transactional anomalies can cause queries to return unpredictable results. Three types 
of transactional anomalies are possible within SQL Server: dirty reads, nonrepeatable 
reads, and phantom reads. These are discussed in the following sections.
Dirty Reads
A dirty read occurs when a transaction reads data that never existed in the database. An 
example of how this anomaly can occur is outlined in Table 18-7.
Table 18-7.  A Dirty Read
Transaction1
Transaction2
Inserts row1 into Table1
Reads row1 from Table1
Rolls back
Table 18-8.  A Nonrepeatable Read
Transaction1
Transaction2
Reads row1 from Table1
Updates row1 in Table1
Commits
Reads row1 from Table1
In this example, because Transaction1 rolled back, Transaction2 read a row that 
never existed in the database. This anomaly can occur if shared locks are not acquired for 
reads, since there is no lock to conflict with the exclusive lock taken out by Transaction1.
Nonrepeatable Read
A nonrepeatable read occurs when a transaction reads the same row twice but receives 
different results each time. An example of how this anomaly can occur is outlined in 
Table 18-8.
Chapter 18  Locking and Blocking
Table 18-8 — بازنمایی متن فنی جدول منبع
--- PDF PAGE 682 ---
675
Transactional Anomalies
Transactional anomalies can cause queries to return unpredictable results. Three types 
of transactional anomalies are possible within SQL Server: dirty reads, nonrepeatable 
reads, and phantom reads. These are discussed in the following sections.
Dirty Reads
A dirty read occurs when a transaction reads data that never existed in the database. An 
example of how this anomaly can occur is outlined in Table 18-7.
Table 18-7.  A Dirty Read
Transaction1
Transaction2
Inserts row1 into Table1
Reads row1 from Table1
Rolls back
Table 18-8.  A Nonrepeatable Read
Transaction1
Transaction2
Reads row1 from Table1
Updates row1 in Table1
Commits
Reads row1 from Table1
In this example, because Transaction1 rolled back, Transaction2 read a row that 
never existed in the database. This anomaly can occur if shared locks are not acquired for 
reads, since there is no lock to conflict with the exclusive lock taken out by Transaction1.
Nonrepeatable Read
A nonrepeatable read occurs when a transaction reads the same row twice but receives 
different results each time. An example of how this anomaly can occur is outlined in 
Table 18-8.
Chapter 18  Locking and Blocking
PAGE-683

Phantom Read و Pessimistic Isolation

Phantom وقتی Query Range در اجرای دوم Row جدید/حذف‌شده ببیند رخ می‌دهد. READ COMMITTED، REPEATABLE READ و SERIALIZABLE از Locking Pessimistic با سخت‌گیری افزایشی استفاده می‌کنند.

Table 18-9 — بازنمایی متن فنی جدول منبع
--- PDF PAGE 683 ---
676
In this example, you can see that Transaction1 has read row1 from Table1 twice. 
The second time, however, it receives a different result, because Transaction2 has 
updated the row. This anomaly can occur if Transaction1 takes out shared locks but 
does not hold them for the duration of the transaction.
Phantom Read
A phantom read occurs when a transaction reads a range of rows twice but receives a 
different number of rows the second time it reads the range. An example of how this 
anomaly can occur is outlined in Table 18-9.
Table 18-9.  Phantom Reads
Transaction1
Transaction2
Reads all rows from Table1
Inserts ten rows into Table1
Commits
Reads all rows from Table1
In this example, you can see that Transaction1 has read all rows from Table1 twice. 
The second time, however, it reads an extra ten rows, because Transaction2 has inserted 
ten rows into the table. This anomaly can occur when Transaction1 does not acquire a 
key-range lock and hold it for the duration of the transaction.
Isolation Levels
SQL Server provides four pessimistic and two optimistic transaction isolation levels 
for transactions that involve disk-based tables. Pessimistic isolation levels use locks 
to protect against transactional anomalies, and optimistic isolation levels use row 
versioning.
Pessimistic Isolation Levels
Read Uncommitted is the least restrictive isolation level. It works by acquiring locks for 
write operations but not acquiring any locks for read operations. This means that under 
this isolation level, read operations do not block other readers or writers. The result is 
that all transactional anomalies described in the previous sections are possible.
Chapter 18  Locking and Blocking
PAGE-684

Optimistic Isolation

READ_COMMITTED_SNAPSHOT و SNAPSHOT از Row Versioning در TempDB استفاده می‌کنند تا Readها کمتر با Writeها Block شوند. هزینه آن Version Store و احتمال Update Conflict در SNAPSHOT است.

PAGE-685
ALTER DATABASE Chapter18 SET ALLOW_SNAPSHOT_ISOLATION ON;
ALTER DATABASE Chapter18 SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
PAGE-686

Durable و Delayed Durability

Durability یعنی پس از Commit، تغییرات در برابر Failure حفظ شوند. SQL Server به‌طور معمول Commit را تا Harden شدن Log منتظر می‌ماند. Delayed Durability می‌تواند Latency Commit را کاهش دهد اما Window کوچک Data Loss در Crash ایجاد می‌کند.

PAGE-687
Delayed Durability
سطحرفتار
DISABLEDCommitها کاملاً Durable
ALLOWEDTransaction می‌تواند DELAYED_DURABILITY=ON درخواست کند
FORCEDCommitهای مجاز به‌صورت Delayed
ALTER DATABASE Chapter18 SET DELAYED_DURABILITY = ALLOWED;
Table 18-10 — بازنمایی متن فنی جدول منبع
--- PDF PAGE 687 ---
680
SQL Server relaxed these rules, however, by introducing a feature called delayed 
durability. This feature works by delaying the flush of the log cache to disk until one of 
the following events occurs:
•	
The log cache becomes full and automatically flushes to disk.
•	
A fully durable transaction in the same database commits.
•	
The sp_flush_log system stored procedure is run against the 
database.
When delayed durability is used, the data is visible to other transactions as soon as 
the transaction commits; however, the data committed within the transaction could 
potentially be lost, if the instance goes down or is restarted, until the log records have 
been flushed. Support for delayed durability is configured at the database level, using 
one of the three options detailed in Table 18-10.
Table 18-10.  Support Levels for Delayed Durability
Support Level
Description
ALLOWED
Delayed durability is supported within the database and specified on a 
transaction level basis.
FORCED
All transactions within the database will use delayed durability.
DISABLED
The default setting. No transactions within the database are permitted to use 
delayed durability.
The command in Listing 18-5 shows how to allow delayed durability in the 
Chapter18 database.
Listing 18-5.  Allowing Delayed Durability
ALTER DATABASE Chapter18
SET DELAYED_DURABILITY  = ALLOWED ;
If a database is configured to allow delayed durability, then Full or Delayed durability 
is configured at the transaction level, in the COMMIT statement. The script in Listing 18-6 
demonstrates how to commit a transaction with delayed durability.
Chapter 18  Locking and Blocking
PAGE-688
COMMIT TRANSACTION WITH (DELAYED_DURABILITY = ON);

Data Loss Risk باید با Business Requirement سنجیده شود. Delayed Commit بیشتر برای Transactionهای کوچک و پرشمار که از دست رفتن محدود قابل قبول است مطرح می‌شود.

PAGE-689

Isolation Levels

READ COMMITTED پیش‌فرض رایج است. Memory-Optimized Transaction می‌تواند با MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT به Snapshot ارتقا یابد. RCSI Semantic Read Committed را با Versioning اجرا می‌کند.

PAGE-690

SNAPSHOT یک Snapshot منطقی در شروع Transaction می‌بیند؛ REPEATABLE READ Row خوانده‌شده را تا پایان Transaction Lock می‌کند؛ SERIALIZABLE علاوه بر Rowها Range را نیز محافظت می‌کند و Phantom را جلوگیری می‌کند.

PAGE-691

Cross-Container Transactions

Transaction می‌تواند Table Disk-Based و Memory-Optimized را با هم Access کند، اما Isolation Compatibility اهمیت دارد. Automatic Elevation می‌تواند دسترسی Memory Table را به SNAPSHOT ببرد.

Figure 18-4 — شکل/تصویر منبع، صفحه PDF 691
PAGE-692

Query Hintهایی مانند WITH (SNAPSHOT) یا Procedure Native با Isolation Level صریح برای Cross-container استفاده می‌شوند. Native Procedure محدودیت Syntax/Isolation خاص خود را دارد.

PAGE-693

Retry Logic

Optimistic Concurrency در Memory-Optimized ممکن است Conflictهایی مانند Update Conflict یا Validation Failure تولید کند. Application باید Errorهای مشخص را با Backoff/Retry محدود و Idempotent Handle کند، نه Loop بی‌نهایت.

امتیاز کاربران به این مقاله

☆☆☆☆☆

0 نفر امتیاز داده اند. میانگین: 0.0 از 5

 

0 نظر

نظر محترم شما در مورد مقاله های وب سایت برنامه نویسی و پایگاه داده

نظرات محترم شما در خدمات رسانی بهتر ما را یاری می نمایند. لطفا اگر مایل بودید یک نظر ما را مهمان فرمائید. آدرس ایمیل و وب سایت شما نمایش داده نخواهد شد.

0 / 500