Module 8
Designing a Transaction and Concurrency Strategy
Module Overview
• Guidelines for Defining Transactions
• Defining Isolation Levels
• Guidelines for Designing a Resilient Transaction Strategy
Lesson 1: Guidelines for Defining Transactions
• Guidelines for Creating Transactions
• Considerations for Using Nested Transactions
• Demonstration: How to Create a Nested Transaction
ü
Use transaction savepointsü
Guidelines for Creating Transactions
Perform a transaction in:• Explicit Mode
• Autocommit Mode
• Implicit Mode
Manage transactions across multiple platformsü
Handle the errors in a transaction by using TRY…CATCHü
Considerations for Using Nested Transactions
Committing inner transactions is ignored by the SQL Server Database Engineü
Each call to COMMIT TRANSACTION or COMMIT WORK applies to the last executed BEGIN TRANSACTIONü
The Transaction_name should not refer to the inner transactions of a set of named nested transactionsü
The @@TRANCOUNT function records the current transaction nesting levelü
Demonstration: How To Create a Nested Transaction
In this demonstration, you will see how to:
Create a nested transaction
Lesson 2: Defining Isolation Levels
• Isolation Levels in the Database Engine
• Comparing Read Committed Isolation with Snapshot Isolation
• Selecting an Isolation Level for a Database
• Considerations for Offloading Concurrent Activities
Isolation Levels in the Database Engine
READ UNCOMMITTED: Can read data that has been modified by other transactions but not committed
READ COMMITTED: Cannot read data that has been modified but not committed by other transactions
REPEATABLE READ: No other transactions can modify data that has been read by the current transaction until the current transaction completes
SERIALIZABLE: Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until it is complete
SNAPSHOT: Data read by any statement in a transaction will be the transactionally consistent version of the data that existed at the start of the transaction
Comparing Read Committed Isolation with Snapshot Isolation
• Reader/writer blocking occurs to the point that concurrency benefits outweigh increased overhead of creating and managing row versions
• An application requires absolute accuracy for long-running aggregations or queries where data values must be consistent to the point in time that a query starts
When to Use Read Committed Isolation
• Optimistic concurrency control is desired
• Probability is low that a transaction would have to be rolled back because of an update conflict
• An application needs to generate reports based on long-running, multi-statement queries that must have point-in-time consistency
When to Use Snapshot Isolation
Use read committed isolation when: Use snapshot isolation when:
Selecting an Isolation Level for a Database
Use alternatives to restrictive isolation levels, such as:• Redesigning Transact-SQL logic and
tables• Redesigning how applications access data• Overriding the default locking strategy
Determine the minimum isolation level
Use row versioning isolation levels
Set the Connection Reset property to True in the connections string, for connection pools
Database Isolation Level
Considerations for Offloading Concurrent Activities
Offloading concurrent activities helps:• Avoid contention and blocking problems• Improve system performance
Scale out the systemü Create a data
warehouseü Reschedule activitiesü
Methods for Offloading Concurrent Activities
• Guidelines for Designing an Object Access Strategy
• Guidelines for Transaction Rollback
• Guidelines for Using Optimizer Hints
• Guidelines for Using Locking Hint Functionalities
• Guidelines for Monitoring Transactions
• Guidelines for Troubleshooting Orphaned Transactions
• Demonstration: How to Use Locking Hints
• Discussion: Reducing Deadlock Errors
Lesson 3: Guidelines for Designing a Resilient Transaction Strategy
Guidelines for Designing an Object Access Strategy
Design an object access order based on usage
Manage conflicts when accessing objects
Communicate the object access order and enforce compliance
Discourage ad hoc queries
2
1
3
1
2
31
2
3
2
1
3
Guidelines for Transaction Rollback
Minimize errors Pre-validate data Monitor for rollbacks
Define a rollback strategy Define an error-handling strategy
Guidelines for Using Optimizer Hints
The query optimizer enforces a join strategy between two tables
Join Hints
Overrides the default behavior of the query optimizer
Table Hints
Query hints enforce query optimizer to execute the select statement as a whole
Query Hints
Using locking hints, you can implement the following functionalities:
• Provide finer locking control• Reduce deadlocking• Lower the isolation level
Locking hint functionalities of SQL Server 2008 include:
• XLOCK at row level• READPAST• READCOMMITTEDLOCK
Guidelines for Using Locking Hint Functionalities
Guidelines for Monitoring Transactions
Use the following options to monitor transactions:
sys.dm_tran_database_transactionsü
DBCC OPENTRANü
Use the following strategies to reduce the impact of large transactions:
Split large transactions into smaller onesü
Schedule large transactions to run at off-peak hoursü
Guidelines for Troubleshooting Orphaned Transactions
Identify the orphaned processes using sp_whoü
Ignore the orphaned processes or kill themü
Check for any improper procedures or general faultsü
Check whether the Inter-Process Communication (IPC)session is still active ü
Demonstration: How To Use Locking Hints
In this demonstration, you will see how to:
Create a locking hint
Discussion: Reducing Deadlock Errors
• What are good practices to prevent deadlock errors?
• How would you use object access strategy to handle deadlock errors?
Lab 8: Designing a Transaction and Concurrency Strategy
• Exercise 1: Determining the Database Isolation Level
• Exercise 2: Determining the Order of Object Access
• Exercise 3: Designing Transactions
Logon Information
Estimated time: 60 minutes
Virtual machine
User name
Password
NYC-SQL1
Administrator
Pa$$w0rd
Lab Scenario
You are a lead database designer at QuantamCorp. You are working on the Human Resources Vacation and Sick Leave Enhancement (HR VASE) project that is designed to enhance the current HR system of your organization. This system is based on the QuantamCorp2008 sample database in SQL Server 2008.
The main goals of the HR VASE project are as follows:
• Provide managers with current and historical information about employee vacation and sick leave.
• Grant view rights to individual employees to view their vacation and sick leave balances.
• Provide permission to selected employees in the HR department to view and update the vacation and sick leave details of employees.
• Grant the HR manager with the view and update rights to all the data.
The management team has a requirement to generate performance metric for the sales staff. For this purpose, you are required to add a new function to the QuantamCorp HR VASE database that will import information from the CRM database. The management then will use statistical data to generate the performance metric report.
Lab Review
• Describe another way to offload the services usage registration process.
• If you remove the requirement for simultaneous phone number assignment, what solution would you propose?
Module Review and Takeaways
• Review Questions
• Real-world Issues and Scenarios
• Best Practices