module 8 designing a transaction and concurrency strategy

24
Module 8 Designing a Transaction and Concurrency Strategy

Upload: jagger

Post on 22-Feb-2016

41 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Module 8 Designing a Transaction and Concurrency Strategy

Module 8

Designing a Transaction and Concurrency Strategy

Page 2: Module 8 Designing a Transaction and Concurrency Strategy

Module Overview

• Guidelines for Defining Transactions

• Defining Isolation Levels

• Guidelines for Designing a Resilient Transaction Strategy

Page 3: Module 8 Designing a Transaction and Concurrency Strategy

Lesson 1: Guidelines for Defining Transactions

• Guidelines for Creating Transactions

• Considerations for Using Nested Transactions

• Demonstration: How to Create a Nested Transaction

Page 4: Module 8 Designing a Transaction and Concurrency Strategy

ü

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ü

Page 5: Module 8 Designing a Transaction and Concurrency Strategy

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ü

Page 6: Module 8 Designing a Transaction and Concurrency Strategy

Demonstration: How To Create a Nested Transaction

In this demonstration, you will see how to:

Create a nested transaction

Page 7: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 8: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 9: Module 8 Designing a Transaction and Concurrency Strategy

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:

Page 10: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 11: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 12: Module 8 Designing a Transaction and Concurrency Strategy

• 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

Page 13: Module 8 Designing a Transaction and Concurrency 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

Page 14: Module 8 Designing a Transaction and Concurrency Strategy

Guidelines for Transaction Rollback

Minimize errors Pre-validate data Monitor for rollbacks

Define a rollback strategy Define an error-handling strategy

Page 15: Module 8 Designing a Transaction and Concurrency 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

Page 16: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 17: Module 8 Designing a Transaction and Concurrency Strategy

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ü

Page 18: Module 8 Designing a Transaction and Concurrency Strategy

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 ü

Page 19: Module 8 Designing a Transaction and Concurrency Strategy

Demonstration: How To Use Locking Hints

In this demonstration, you will see how to:

Create a locking hint

Page 20: Module 8 Designing a Transaction and Concurrency Strategy

Discussion: Reducing Deadlock Errors

• What are good practices to prevent deadlock errors?

• How would you use object access strategy to handle deadlock errors?

Page 21: Module 8 Designing a Transaction and Concurrency Strategy

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

Page 22: Module 8 Designing a Transaction and Concurrency Strategy

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.

Page 23: Module 8 Designing a Transaction and Concurrency Strategy

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?

Page 24: Module 8 Designing a Transaction and Concurrency Strategy

Module Review and Takeaways

• Review Questions

• Real-world Issues and Scenarios

• Best Practices