Академический Документы
Профессиональный Документы
Культура Документы
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, MS-DOS, Windows, Windows NT, <plus other appropriate product names or titles.
The publications specialist replaces this example list with the list of trademarks provided by the
copy editor. Microsoft, MS-DOS, Windows, and Windows NT are listed first, followed by all
other Microsoft trademarks listed in alphabetical order.> are either registered trademarks or
trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
<The publications specialist inserts mention of specific, contractually obligated to, third-party
trademarks, provided by the copy editor>
The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.
Beta
Contents
Introduction 1
Clinic Materials 2
Microsoft Learning Product Types 5
How to Get the Most Out of a Clinic 6
Microsoft Learning 7
Microsoft Certification Program 9
Facilities 12
About This Clinic 13
Prerequisites 15
Clinic Outline 16
Introduction to the Workshop Business
Scenario 17
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, Active Directory, ActiveX, BizTalk, Excel, IntelliSense, Microsoft Press, MSDN, MS-
DOS, Outlook, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++, Visual C#, Visual
Studio, Windows, Windows NT, and Windows Server are either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries.
Introduction
The instructor will ask you to introduce yourself by providing the information on the slide to the
other students in the clinic.
Briefly describe your job role in your organization and your prior experience developing databases
in an enterprise environment, including any notable challenges you might have encountered. In
particular, describe your experiences developing database solutions using previous versions of
Microsoft® SQL Server™ in conjunction with the Microsoft .NET Framework.
Let your instructor know what you hope to learn in this clinic so that he or she can best meet your
expectations. Additionally, your instructor can recommend other Microsoft learning products that
can help you to acquire the knowledge and skills you need to meet your professional goals.
Clinic Materials
Note To open the Web page, insert the Student Materials compact disc into the CD-ROM
drive, and then in the root directory of the compact disc, double-click StartCD.exe.
Clinic evaluation. Near the end of the clinic, you will have the opportunity to complete an online
evaluation to provide feedback on the clinic, training facility, and instructor.
Document Conventions
The following conventions are used in clinic materials to distinguish elements of the text.
Convention Use
Represents resources available by launching the Resource Toolkit shortcut on the desktop.
Bold Represents commands, command options, and syntax that must be typed exactly as
shown. It also indicates commands on menus and buttons, dialog box titles and options,
and icon and menu names.
Italic In syntax statements or descriptive text, indicates argument names or placeholders for
variable information. Italic is also used for introducing new terms, for book titles, and for
emphasis in the text.
Title Capitals Indicate domain names, user names, computer names, directory names, and folder and file
names, except when specifically referring to case-sensitive names. Unless otherwise
indicated, you can use lowercase letters when you type a directory name or file name in a
dialog box or at a command prompt.
ALL CAPITALS Indicate the names of keys, key sequences, and key combinations —for example,
ALT+SPACEBAR.
monospace Represents code samples or examples of screen text.
[] In syntax statements, enclose optional items. For example, [filename] in command syntax
indicates that you can choose to type a file name with the command. Type only the
information within the brackets, not the brackets themselves.
{} In syntax statements, enclose required items. Type only the information within the braces,
not the braces themselves.
| In syntax statements, separates an either/or choice.
Ç Indicates a procedure with sequential steps.
... In syntax statements, specifies that the preceding item may be repeated.
. Represents an omitted portion of a code sample.
.
.
Microsoft Learning offers four types of instructor-led products. Each is specific to a particular
audience type and level of experience. The different product types also tend to suit different
learning styles. These types are as follows:
Courses are for information technology (IT) professionals and developers who are new to a
particular product or technology and for experienced individuals who prefer to learn in a
traditional classroom format. Courses provide a relevant and guided learning experience that
combines lecture and practice to deliver thorough coverage of a Microsoft product or technology.
Courses are designed to address the needs of learners engaged in planning, design,
implementation, management, and support phases of the technology adoption lifecycle. They
provide detailed information by focusing on concepts and principles, reference content, and in-
depth hands-on lab activities to ensure knowledge transfer. Typically, the content of a course is
broad, addressing a wide range of tasks necessary for the job role.
Workshops are for knowledgeable IT professionals and developers who learn best by doing and
exploring. Workshops provide a hands-on learning experience in which participants use Microsoft
products in a safe and collaborative environment based on real-world scenarios. Workshops are
the learning products where students learn by doing through scenario, and through troubleshooting
hands-on labs, targeted reviews, information resources, and best practices, with instructor
facilitation.
Clinics are for IT professionals, developers and technical decision makers. Clinics offer a detailed
“how to” presentation that describes the features and functionality of an existing or new Microsoft
product or technology, and that showcases product demonstrations and solutions. Clinics focus on
how specific features will solve business problems.
Hands-On Labs provide IT professionals and developers with hands-on experience with an
existing or new Microsoft product or technology. Hands-on labs provide a realistic and safe
environment to encourage knowledge transfer by learning through doing. The labs provided are
completely prescriptive so that no lab answer keys are required. There is very little lecture or text
content provided in hands-on labs, aside from lab introductions, context setting, and lab reviews.
Clinics are intended to provide knowledge, not skills, transfer. The primary purpose of a clinic is to
give customers a first look at the benefits and features of the latest Microsoft technologies so that
they can make informed decisions and plan ahead
The clinic is a fast-paced learning format that focuses on instructor-led demonstrations rather than
lecture. In a clinic, lecture time is kept to a minimum so that students have the opportunity to focus
on the demonstrations. The clinic format enables students to reinforce their learning by seeing how
tasks are performed and how problems are solved.
Microsoft Learning
Microsoft Learning develops Official Microsoft Learning Product (OMLP) courseware for
computer professionals who design, develop, support, implement, or manage solutions by using
Microsoft products and technologies. These learning products provide comprehensive, skills-based
training in instructor-led and online formats.
Assessments:
• One job-role based assessment: Introduction to Microsoft SQL Server
2005 for Database Developers
Each learning product relates in some way to other learning products. A related product may be a
prerequisite; a follow-up course, clinic, or workshop in a recommended series, or a learning product
that offers additional training.
It is recommended that you take the following learning products in this order:
Clinic 2783A, Designing the Data Tier for Microsoft SQL Server 2005
Workshop 2784A, Tuning and Optimizing Queries Using Microsoft SQL Server 2005
Other related learning products may become available in the future, so for up-to-date information
about recommended learning products, visit the Microsoft Learning Web site.
Microsoft Learning offers a variety of certification credentials for developers and IT professionals.
The Microsoft certification program is the leading certification program for validating your
experience and skills, keeping you competitive in today’s changing business environment.
MCP Certifications
The MCP program includes the following certifications.
MCITP: Database Developer
The Microsoft Certified Desktop Support Technician (MCDST) certification is designed for
professionals who successfully support and educate end users and troubleshoot operating system
and application issues on desktop computers running the Windows® operating system.
MCSA on Microsoft Windows Server™ 2003
The Microsoft Certified Systems Administrator (MCSA) certification is designed for professionals
who implement, manage, and troubleshoot existing network and system environments based on
the Windows Server 2003 platform. Implementation responsibilities include installing and
configuring parts of systems. Management responsibilities include administering and supporting
MCT USE ONLY. STUDENT USE PROHIBITED
systems.
10 Session 0: Introduction
The Microsoft Certified Systems Engineer (MCSE) credential is the premier certification for
professionals who analyze business requirements and design and implement infrastructure for
business solutions based on the Windows Server 2003 platform. Implementation responsibilities
include installing, configuring, and troubleshooting network systems.
MCAD
The Microsoft Certified Application Developer (MCAD) for Microsoft .NET credential is
appropriate for professionals who use Microsoft technologies to develop and maintain department-
level applications, components, Web or desktop clients, or back-end data services, or who work in
teams developing enterprise applications. The credential covers job tasks ranging from developing
to deploying and maintaining these solutions.
MCSD
The Microsoft Certified Solution Developer (MCSD) credential is the premier certification for
professionals who design and develop leading-edge business solutions with Microsoft
development tools, technologies, platforms, and the Microsoft Windows DNA architecture. The
types of applications MCSDs can develop include desktop applications and multi-user, Web-
based, N-tier, and transaction-based applications. The credential covers job tasks ranging from
analyzing business requirements to maintaining solutions.
MCDBA on Microsoft SQL Server 2000
The Microsoft Certified Database Administrator (MCDBA) credential is the premier certification
for professionals who implement and administer SQL Server databases. The certification is
appropriate for individuals who derive physical database designs, develop logical data models,
create physical databases, use Transact-SQL to create data services, manage and maintain
databases, configure and manage security, monitor and optimize databases, and install and
configure SQL Server.
MCP
The Microsoft Certified Professional (MCP) credential is for individuals who have the skills to
successfully implement a Microsoft product or technology as part of a business solution in an
organization. Hands-on experience with the product is necessary to successfully achieve
certification.
MCT
Microsoft Certified Trainers (MCTs) demonstrate the instructional and technical skills that qualify
them to deliver Official Microsoft Learning Products through a Microsoft Certified Partner for
Learning Solutions.
Certification Requirements
Requirements differ for each certification category and are specific to the products and job
functions addressed by the certification. To become a Microsoft Certified Professional, you must
pass rigorous certification exams that provide a valid and reliable measure of technical proficiency
and expertise.
Facilities
Inform the students of class logistics, including class start and end times, break
times, and building hours. Also inform the students of any classroom policies
you might have, such as limitations on cellular telephone usage.
Point out the locations of parking, restrooms, dining facilities, telephones, and
areas where smoking is permitted.
If your facility participates in a recycling program, be sure to encourage
students to recycle accordingly.
This section provides you with a brief description of the clinic, objectives, and target audience.
Description
The purpose of this one-day clinic is to teach database developers working in enterprise
environments how to understand and determine how application developers will access and
consume their data—one of the major reasons for the failure of database solutions today.
Clinic Objectives
After completing this clinic, you will be able to explain how to:
Choose data access technologies and an object model to support an organization’s business needs.
Design an exception handling strategy.
Choose a cursor strategy.
Design query strategies using multiple active results sets (MARS).
Design caching strategies for database applications.
Design a scalable data tier for database applications.
Audience
The target audience for this clinic is experienced professional database developers who are already
proficient in database technologies and at applying them at a professional job role level. These
database developers have experience using previous version of Microsoft SQL Server or other
database technologies, but have not worked with SQL Server 2005.
Most audience members will work for enterprise-level organizations consisting of at least 500
personal computers (PCs) and 100 servers. Typically, their job roles require them to address and
create solutions for all types of enterprise issues, including:
Writing Transact-SQL queries.
Designing and implementing programming objects.
Troubleshooting programming objects.
Doing database performance tuning and optimization.
Designing databases, at both the conceptual and logical levels.
Implementing databases at the physical level.
In some cases, designing and troubleshooting the data access layer of the application.
Gathering business requirements.
Prerequisites
Clinic Prerequisites
This clinic requires that you meet the following prerequisites:
Experience reading user requirements and business-need documents. For example, development
project vision/mission statements or business analysis reports.
Basic knowledge of the Microsoft .NET framework, .NET concepts, Microsoft ADO.NET, and
service-oriented architecture (SOA).
Familiarity with the tasks that application developers typically perform.
Understanding of Transact-SQL syntax and programming logic.
Experience with professional-level database design.
• Specifically, the ability to design a normalized database and know the tradeoffs involved in
denormalization and designing for performance and business requirements.
Basic monitoring and troubleshooting skills.
• Specifically, how to use SQL Server Profiler and dynamic management views.
Basic knowledge of the operating system and platform. That is, how the operating system
integrates with the database, what the platform or operating system can do, and how interaction
between the operating system and the database works.
Basic knowledge of application architecture. That is, how applications can be designed based on
SOA, what applications can do, how interaction between the application and the database works,
and how the interaction between the database and the platform or operating system works.
Clinic Outline
Session 1, “Choosing Data Access Technologies and an Object Model,” explains how to choose
data access technologies and an object model to support an organization’s business needs. This
session focuses on methods of accessing data, building a data access layer, designing a data access
layer with SQLCLR, and using data object models for administering SQL Server 2005.
Session 2, “Designing an Exception Handling Strategy,” covers the various types of exceptions that
can occur in a database system, how to capture them, and how to manage them appropriately. In
addition, the session explains how to design strategies for detecting exceptions at the appropriate
layer, and how to log and communicate exceptions according to your business requirements.
Session 3, “Choosing a Cursor Strategy,” explains when cursors are appropriate, and how to use
them to optimize the use of system resources. The main purpose of this session is to discover the
adequate application scope for cursors. The session explains the scenarios in which cursors are
appropriate, considerations for selecting server-side and client-side cursors, and how to use cursors
to optimize the use of system resources.
Session 4, “Designing Query Strategies Using Multiple Active Result Sets,” explains how Multiple
Active Result Sets (MARS) can improve application response time and user satisfaction. The
session describes scenarios in which it might be beneficial to use MARS to combine write and read
operations. The session also covers the locking implications of using MARS and how these locks
affect other transactions.
Session 5, “Designing Caching Strategies for Database Applications,” focuses on how to optimize
system resources by caching data and objects in the appropriate layers. This session explains how
correctly optimizing applications by implementing caching will result in reduced resource
utilization and consequently better system performance. The session also describes how resources
such as memory, physical I/O, and network bandwidth can be optimized by using caching
methodologies.
Session 6, “Designing a Scalable Data Tier for Database Applications,” describes how to assess
scalability needs and design the best architecture to scale the system to meet the needs of your
users. The session explains how to identify when to scale database applications and what layer to
scale, as well as how to select the appropriate technology to avoid concurrency problems and
This topic introduces a fictitious business scenario, your role in that scenario, and one potential
solution to the business problem presented in the scenario. Your instructor will demonstrate the
solution, which you will be able to create upon completion of this workshop.
Demonstration
Your instructor will demonstrate the solution to the business problem in this workshop. The
solution that you ultimately create should be similar to the solution that you see in the
demonstration.
Contents
Session Overview 1
Section 1: Introduction to Data Access
Technologies 2
Section 2: Choosing Technologies for
Accessing Data 8
Section 3: Building a Data Access Layer 32
Section 4: Designing Data Access from
SQLCLR Objects 44
Section 5: Available Data Object Models for
Administering SQL Server 56
Next Steps 68
Discussion: Session Summary 69
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, <The publications specialist places the list of trademarks provided by the copy editor
here. Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order.>
are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or
other countries.
<The publications specialist inserts mention of specific, contractually obligated to, third-party
trademarks, provided by the copy editor>
Session Overview
Database applications can be used to access data stored in a database or to provide management
access to database systems. With knowledge of various data access technologies, you can select the
appropriate technologies and best features to develop efficient and manageable database
applications.
Developers often continue using the same data access technologies that they have used to develop
database applications in the past. This is because they are unaware of technologies that can better
serve their development needs. Using inappropriate data access technologies results in development
of inefficient database applications.
This session focuses on data access technologies and explains methods of accessing data, building a
data access layer, designing a data access layer with SQLCLR, and using data object models for
administering Microsoft® SQL Server™ 2005.
Session Objectives
Describe a typical database system and the role that data access technologies play in that system.
Select appropriate technologies for accessing data stored in SQL Server 2005.
Explain how to build a data access layer.
Explain how to design SQL Server objects that use the in-process data provider.
Describe the data object models for administering SQL Server 2005 components and objects.
Section Overview
There are two types of database applications: applications that obtain data and store it in the
database using data access components, and the administrative tools, based on well-defined object
models which provide the functionality required to administer a database system. Your approach
toward each type of database application will affect how these applications are used, their
performance, and their maintenance.
This section introduces you to available data access technologies, discusses their appropriate scope
of utilization and lists the sources from where you can find information on these technologies.
In this section, you will learn about the two types of database applications. You will also learn
about the various components of the data access system and the architecture of the data access
components and libraries. Additionally, you will learn about the sources where you can find
information and the available documentation describing the functionality of data access
technologies.
Section Objectives
Explain the various types of database applications.
Explain how the various components of a data access system interact with each other.
Explain where you can find information and documentation about data access technology.
Introduction
Database applications serve many purposes, such as managing data and providing administrative
control to the database system. In many cases, database applications are clearly oriented towards
either data management or system administration. However, it is common for database applications
to provide a mixture of these two completely different types of tasks.
Discussion Questions
1. What is a database application?
2. Is SQL Server Management Studio a database application?
3. Is Windows Explorer a database application?
4. Is a database application confined to a database server?
5. Should database applications be developed exclusively by client-side developers?
6. Should database applications be server-independent?
Introduction
To interact with a data source, every application requires a data access system. All applications,
from the simplest to a complex distributed enterprise application, require the same type of data
access components.
A simple database application can be a query running directly inside a database server. Such an
application will typically be a client application. An example of a simple database application is an
SQLcmd running on a desktop computer, connected to the database server through a specific
network library, such as SQL Native Client (SQLNCLI).
A complex distributed application can be a standard business application, such as a customer
relationship management (CRM) application or an accounting application. These applications can
be developed using data access components. You can create a client application that uses a data
access interface, such as ADO.NET, with a data source provider such as SQLNCLI to execute
queries against a remote database server.
In this topic, you will learn about the various components of the data access system.
Additional Information T-SQL Endpoints, which replace the legacy open data services
(ODS) interface, are TCP endpoints that SQL Server creates to listen to requests from client
applications. Rather than using the standard endpoint created by default, you can create
multiple T-SQL Endpoints to which applications can connect, and these endpoints can be
assigned specific permissions.
For more information on T-SQL Endpoints, refer to the following resources:
• “Hardware and Software Requirements for Installing SQL Server 2005” from
Books Online
• “Net-Libraries and Network Protocols” from Books Online, the online product
documentation for SQL Server 2005.
Introduction
During this demonstration, your instructor will point out important sources of information about
SQL Server 2005, including Books Online, the online product documentation for SQL Server 2005.
These sources of information are dynamically updated to provide up-to-date support for SQL
Server 2005.
Demonstration Overview
In this demonstration, your instructor will illustrate how to navigate to various sources of
information about data access technologies.
It is important to understand that Books Online (BOL) is not the only source of information about
SQL Server 2005. In fact, this demonstration will show other important, dynamically updated
sources of information. These sources provide up-to-date support for SQL Server 2005 and provide
more information than BOL can offer.
1. To access official SQL Server documentation, click Start, point to All Programs, point to
Microsoft SQL Server 2005 CTP, point to Documentation and Tutorials, and click SQL Server
Books Online.
The SQL Server Books Online window appears.
2. Click Start, point to All Programs, point to Microsoft SQL Server 2005 CTP, point to
Documentation and Tutorials, point to Tutorials, and then click SQL Server Tutorials.
3. In Windows Explorer, navigate to C:\Program Files\Microsoft SQL Server\90\Samples.
4. Click Start, point to Programs, point to Microsoft Visual Studio 2005 Beta 2, and then click
Microsoft Visual Studio 2005 Documentation.
5. Start Microsoft Internet Explorer.
6. Browse to http://msdn.microsoft.com.
7. Browse to http://msdn.microsoft.com/sql.
8. Browse to http://msdn.microsoft.com/practices.
9. Browse to http://technet.microsoft.com.
10. Browse to http://www.microsoft.com/sql/default.mspx.
11. Browse to http://blogs.msdn.com/.
12. Browse to http://msdn.microsoft.com/newsgroups/.
Section Overview
Database applications can be designed to access data by using various available data access
technologies. To make an informed decision about selecting the technology that best matches your
design, development, cost, and performance expectations, you should be aware of these
technologies. This knowledge also enables you to design and develop robust and efficient database
applications.
In this section, you will learn about various data access technologies, considerations for using
legacy and new data access technologies, and how to improve database application maintenance.
You will also learn how to use Hypertext Transfer Protocol (HTTP) Endpoints and SOAP to access
data, considerations for connecting SQL Server to other data stores, and how to connect SQL
Server to other data stores.
Section Objectives
Explain the various data access technologies.
Describe scenarios in which using earlier technologies to access data is appropriate.
Describe scenarios in which using SQL Native Client (SNAC) to access data is appropriate.
Explain how to migrate an existing C++ component that uses Open Database Connectivity
(ODBC) to a component that uses SNAC.
Apply the guidelines for accessing data by using ADO.NET.
Explain how to improve the maintenance of database applications by managing connection strings
appropriately.
Apply the guidelines for accessing data by using HTTP Endpoints and SOAP.
Evaluate the considerations for connecting SQL Server to other data stores.
Explain how to connect SQL Server to other data stores and the various ways to connect.
Introduction
Data access technologies enable database applications to connect to and obtain data from databases.
Database developers can use many different data access technologies to enable this functionality.
Understanding the architecture of data access technologies and how their components communicate
with each other will help you both select the data access technology that meets your application
development requirements and develop efficient client-side database applications.
This topic explains the architecture of data access technologies. It also discusses how server and
client components of data access technologies communicate. In addition, this topic describes the
evolution of these technologies from proprietary protocols to open client libraries.
SQL Native Client (SQLNCLI), a new technology implemented by SQL Server 2005, is a library
providing full access to SQL Server databases, including the new functionalities of SQL Server
2005, through the same programming interfaces as ODBC and OLE DB. Because SQLNCLI is
MCT USE ONLY. STUDENT USE PROHIBITED
x Session 1: Choosing Data Access Technologies and an Object Model
specific to SQL Server, there is no need to use an OLE DB provider or an ODBC driver to add
another layer. The communication is from the SQLNCLI directly to the SQL Server database.
The network client library connects to a database server through a server network library, which is
accessed internally from a T-SQL Endpoint.
A client always communicates with a database through the data access providers, and the database
application does not need to be involved in the implementation details of this communication.
Introduction
Depending on their level of complexity and their feature set, it might be appropriate to use legacy
data access technologies for specific applications. To select the best legacy data access technology
and make proper design decisions, you must be aware of the capabilities, limitations, and
performance levels of each technology.
DBLibrary
Legacy SQL Server database applications use DBLibrary to connect to SQL Server. DBLibrary, a
client interface that Microsoft inherited from Sybase, interacts with SQL Server databases.
DBLibrary exposes a set of commands and functions that can be called by C code to perform
operations on and extract results from a database.
Following are considerations for using DBLibrary to access data:
Deprecated feature: DBLibrary has been designed to work exclusively with SQL Server 6.5 and
its previous versions. It is considered a deprecated feature and will be removed in future releases.
Therefore, it should not be used for new projects.
No support for SQL Server versions 7.0 and above: DBLibrary does not expose new
functionalities provided by SQL Server versions 7.0 and above.
ODBC
Following are considerations for using ODBC to access data:
Well-established industry standard: Many database applications use ODBC to connect to relational
database systems. In fact, many high performance database components that require extreme
connectivity performance to SQL Server 2000 are still developed in the C language using ODBC
natively.
Availability of drivers: Developers implement the functions in the ODBC API through DBMS-
specific drivers. Applications call the functions in these drivers to access the data stored in any
database management system (DBMS). A driver manager manages communication between
applications and drivers. Microsoft provides driver managers for computers running Microsoft®
Windows® 95 and later versions, as well as driver managers for ODBC drivers. However, most
available ODBC applications and drivers are developed by other vendors, such as IBM and
Oracle.
ODBC Control Panel Data Source Names (DSNs) and DSN-less connections: The driver manager
exposes itself through an extension for the ODBC Control Panel to define different connection
definition names or DSNs to a user or an administrator. However, applications using ODBC can
create DSN-less connections as necessary.
Additional Information The connection definitions can be stored in the following locations:
The registry:
• System DSNs. Stored in the HKLM portion of the registry and are available to all
users of the computer
• User DSNs. Stored in the private per-user HKU portion of the registry, and are
available only to the user who created them
In the file system:
• File DSN. Files that contain the ODBC connection information and can be copied
from one computer to another or stored in a central location available to all
computers
OLE DB
Following are considerations for using OLE DB to access data:
Additional functionality from ODBC: Many database applications developed with Microsoft
Visual Basic® 6.0 or later versions use ADO and OLE DB to connect to SQL Server and other
database systems. OLE DB has been designed to access data stores that are beyond the typical
relational store. This makes it more suitable for database applications accessing other data sources
such as e-mail stores.
Data providers: OLE DB is a set of COM-based interfaces that expose data from a variety of
sources called OLE DB data providers. These data providers include Microsoft DBMSs and other
DBMSs such as Oracle OLE DB provider and Interbase OLE DB provider. OLE DB interfaces
provide applications with uniform access to data stored in diverse information sources or data
stores, giving developers better and standard programming interfaces. Because OLE DB is based
on a Component Object Model (COM) implementation, it exposes all its functionality as a
collection of objects, thereby exposing properties and simple methods for each required action
instead of the C-based, call-level interface exposed by ODBC drivers.
Universal Data Link (UDL): To simplify the creation of connection strings used by OLE DB, you
can create file data sources called UDLs directly in Windows Explorer. However, as with ODBC,
you can create connections as necessary without using these UDL files. Most applications using
OLE DB do so using ADO, because the ADO exposes a simpler object model than OLE DB does.
SQLXML
Following are considerations for using SQLXML to access data:
URL-based access to SQL Server: By using SQLXML, earlier known as SQL Server Web
Release, you can obtain URL access to SQL Server through a virtual server based on a specific
Internet Server Application Programming Interface (ISAPI) extension. This extension handles
communication with SQL Server.
SQLXML templates: There is a risk of structured query language (SQL) injection during URL-
based access to SQL Server. To avoid this security threat, SQLXML enables creation of
SQLXML templates containing the T-SQL syntax for queries, which are often parameterized and
restrict the type of queries that users can send through the URL.
Deprecated feature: In SQL Server 2005, SQLXML is replaced by HTTP Endpoints. SQL Server
also provides new XML support.
Introduction
Some applications require database operations to be executed with minimum overhead. These
applications can achieve this by using ADO.NET and OLE DB, but these data access APIs
introduce extra programming layers that can be optimized only to a certain point. Such applications
are developed in the C language using ODBC drivers or OLE DB providers natively. However, the
current ODBC drivers cannot access the new functionality of SQL Server 2005.
To use the new functionality provided by SQL Server 2005 and provide fast access to data, you use
SNAC using the familiar ODBC or OLE DB programming interfaces.
Note SQL Server 2005 uses SNAC to expose its new functionalities and administrative tools.
Using SQLXML
SQLXML 4.0, included with SQL Server 2005 and Microsoft Visual Studio® 2005, enables you to
use the new capabilities of SNAC. Using SQLXML, you can implement XML formatting in the
middle tier instead of overloading the computer running SQL Server to perform the task.
When creating a connection to a data source through SNAC, use SNAC as the provider name.
ODBC
To include the SNAC header file in an ODBC application, use the following line of programming
code.
include “SNAC.h”;
When creating a connection to a data source through SNAC, use SQL Native Client as the driver
name string.
Introduction
You can retrieve information from a database through a standard C++ application that uses ODBC
natively. To retrieve information, implement an ODBC connection string component in the
application.
Using a SNAC connection string component in a C++ application is an alternative and efficient
method of retrieving information from a database. To migrate from using an ODBC component to a
SNAC component, you only need to modify some lines of C++ code.
Demonstration Overview
In this demonstration, your instructor will illustrate how to migrate an existing C++ component that
uses ODBC to a component that uses SNAC.
Introduction
ADO.NET introduced a new way of working with data by splitting connected and disconnected
operations. This is done by defining two distinct models: the connected model, which is provider-
dependent, and the disconnected model, which is not linked to any particular data provider.
To design .NET Framework data applications, you must know about these models and the features
that they provide.
Using the disconnected model, you request the database server to retrieve a set of information. SQL
Server sends a stream of data to ADO.NET, which fills a DataSet or a DataTable, and then closes
the connection, freeing the server resources. This reduces the network traffic and resource
utilization on the server side but needs more client resources, mostly memory resources, to keep a
copy of the data inside the application. You must also keep track of the data version when you
update it.
ADOMD.NET
ADOMD.NET is a standard .NET data provider designed to communicate with multidimensional
data sources, such as SQL Server 2005 Analysis Services.
ADOMD.NET uses XML for Analysis version 1.1 to communicate with multidimensional data
sources and can also use Transmission Control Protocol/Internet Protocol (TCP/IP) HTTP streams
to transmit and receive XML-compliant SOAP requests and responses for analysis specification.
ADOMD.NET exposes objects similar to ADO.NET, such as AdoMdConnection,
AdoMdCommand, AdoMdDataReader, and AdoMdDataAdapter.
Introduction
Creating and managing connection strings is an important issue for any database application. This
simple task, when not performed appropriately, can create difficulty in applying changes to the
applications when the target database needs to be changed.
Demonstration Overview
In this demonstration, your instructor will illustrate different ways of managing the database
connection strings and how to improve the availability and security of a connection string.
8. On the Create a New Data Source to SQL Server screen, accept the default setting Windows
NT Authentication, and then click Next.
9. Select the Change the default database to check box, choose AdventureWorks from the
database list, and then click Next.
10. Click Finish, and then click the Test Data Source button. Notice the message Tests Completed
Successfully! Click OK.
11. Click OK to close the window.
12. Open Windows Explorer, and then browse to C:\PROGRAM_FILES\Common
Files\ODBC\Data Sources.
13. Open the file created with the specified name for the DSN connection. Right-click the file, click
Open With, and then click Select the program from a list. In the Open With dialog box, select
Notepad, and then click OK to show the content of the file.
14. Close Windows Explorer.
15. Close the Microsoft Visual Studio 2005 Beta 2 development environment.
Introduction
The Internet has become an important medium for accessing information. Many important features
are available to help you create database applications based on Internet protocols and standards.
This topic discusses the possibility of using HTTP Endpoints and SOAP to access data through the
Internet, and the advantages and disadvantages of using each.
Introduction
Some database applications require SQL Server to connect to other data stores by using stored
procedures, triggers, or user defined functions and to execute distributed queries combining data
from remote data stores.
This topic discusses the primary issues to be considered when connecting to remote data stores and
obtaining information about remote stores.
Note You can use computers running SQL Server and SQL Server databases that have an OLE
DB provider or an ODBC driver as linked servers. However, the functionality of an application
is limited to the functionality implemented by the OLE DB provider or the ODBC driver.
Note You can get more information about linked servers in SQL Server by using system
catalog views.
Inside a SQLCLR procedure, you can define how to connect to an external database, get the
information according to the data source standards, transform it in a compatible format, and process
it before using it with other information inside the original database. This gives you full control of
the external database behavior and enables you to cover any special needs that might be difficult to
meet using standard T-SQL techniques.
Introduction
SQL Server can connect directly to other data stores such as text files, other SQL Server databases,
and databases managed by other relational data base management systems (RDBMSs). You can
automate this process by using a linked server that uses T-SQL. However T-SQL offers additional
functionalities to connect to remote data stores.
Demonstration Overview
In this demonstration, your instructor will illustrate the various ways of connecting SQL Server to
other data stores.
Key points
Define SQL Servers as Linked Servers.
Define other data sources as linked servers.
Define linked servers using T-SQL.
Access information from linked servers.
Section Overview
The data access layer is an abstraction layer between the business logic processing code and the
physical data sources. The data access layer performs important tasks such as maintaining
transactional integrity, enforcing security, enabling communication, and transforming data. Every
system that stores and accesses data benefits from a well-designed data access layer.
A database application without a well-designed data access layer will be more difficult to develop
and maintain. The application will also suffer from performance problems because not all data
access processes will have the benefit of a centralized and common approach to performing data
access tasks.
In this section, you will learn about the benefits of building a data access layer. You will also learn
about some recommended techniques for pooling data access objects and about the guidelines for
passing data through application tiers.
Section Objectives
Explain the benefits of building a data access layer.
Explain the available application blocks relevant to data access technologies.
Explain the different techniques for pooling data access objects to improve application
performance.
Explain how to monitor ADO.NET connection pooling and how to create custom counters that
monitor the way other objects are pooled.
Apply the guidelines for passing data access objects through application tiers.
Introduction
Building a data access layer to group data access functions and methods facilitates their
maintenance. Building a data access layer also enhances the performance of the code in the data
access layer. Code performance affects the entire application.
Discussion Questions
1. Where do you place T-SQL code?
2. Do you use stored procedures or dynamic T-SQL code?
3. Do you design applications to deal with different SQL dialects?
4. Do you design applications to deal with different providers?
5. Does your application code access data interfaces or data objects directly?
6. What do you do to improve maintenance of database applications?
7. What do you do to improve data access layer scalability?
Introduction
The Patterns and Practices team at Microsoft provides scenario-specific recommendations for how
to design, develop, deploy, and operate applications that are architecturally sound for the Microsoft
.NET Framework. Some of these recommendations apply to the construction of data access layers,
specifically the Data Access Application Building Block and the Enterprise Library.
Demonstration Overview
In this demonstration, your instructor will illustrate how to navigate to application blocks created
and published on the Patterns and Practices team Web site.
Note To download application blocks, visit the Patterns and Practices team Web site at
http://msdn.microsoft.com/practices/.
Introduction
When an object, such as a command and data set, is created it passes through the following stages:
1. Memory allocation
2. Code initialization
3. Variable creation
4. Allocation of resources, such as connections, on the server side
5. Garbage collection
6. Memory de-allocation
Object pooling is a technique in which you keep a pool of available objects in the memory for
recycling and reuse instead of creating and destroying them repeatedly. An existing object instance
that is recycled and reused by multiple processes improves the performance of an application.
Object pooling is useful when creating an object is costly or when you need to pool access to scant
resources.
Introduction
There are several tools that you can use to monitor and trace ADO.NET connection pooling and
custom objects. Pooling techniques must find a balance between the number of available pools and
the maximum and minimum number of object instances per pool that must be loaded when a pool is
created.
Demonstration Overview
In this demonstration, your instructor will illustrate how to monitor ADO.NET connection pooling
and create custom counters that monitor how other objects are pooled.
6. Demonstrate that the code opens and closes the ADO.NET SqlConnection object several times
on the LoadVendors, LoadProducts, LoadStoreContacts, and LoadSalesTerritories routines.
Introduction
In a multi-tier application, data is passed from one tier to another for processing, displaying, or
combining with other data. There are many techniques for performing these actions, and each
technique offers you a different degree of flexibility and performance. The following considerations
help you evaluate the different techniques.
Best Practices Design an application to use a data-centric format such as XML documents or
DataSets.
Specific Issues
Several types of objects are not serializable because they have a dependency on, or an affinity with,
the computer on which they are running. Such types of objects are:
Connections
Transactions
DataSets
DataReaders
Section Overview
SQLCLR integrates the SQL Server 2005 engine and the common language runtime (CLR). It
provides the ability to extend the database and create new database elements that were not possible
in previous versions of SQL Server.
Coding for SQLCLR is similar to writing client-side code with ADO.NET, but because the code is
executed in-process inside the database server, there are some differences in the behavior of the
code.
In this section, you will learn about using ADO.NET as an in-process provider with SQLCLR.
Section Objectives
Explain the differences in the behavior of ADO.NET when accessing data from SQLCLR objects
and standard data access components.
Describe the various ADO.NET objects specific to the in-process data provider.
Explain how to use the in-process data provider to access data from SQLCLR objects.
Explain best practices for designing data access from SQLCLR objects.
Introduction
When you are writing code for SQLCLR, the coding and object models are the same as those for
regular ADO.NET code. But when running inside the server, the code has access to more
contextual information, which can be consumed through new available classes.
Available Namespaces
A new namespace and many new classes are available for using ADO.NET as an in-process
provider:
Microsoft.SqlServer.Server
• SqlContext
• SqlPipe
• SqlDataRecord
• SqlTriggerContext
Transaction Management
When using ADO.NET as an in-process provider, there are several options for transactions
management. ADO.NET can:
Use T-SQL statements, such as BEGIN TRAN, COMMIT TRAN, and ROLLBACK TRAN, for
local transactions.
Use SqlTransaction, and SqlTransaction.BeginTransaction objects through the SqlConnection
object.
Create distributed transactions with System.Transactions.Transaction class or with
SystemTransactions.TransactionScope class.
Important Executing heavy data-access routines from inside SQLCLR objects is not
recommended. T-SQL is more appropriate for this type of task.
Note For more information about SQLCLR, refer to the article “Managed Data Access Inside
SQL Server with ADO.NET and SQLCLR” on the MSDN Web site at
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/mandataaccess.asp. You can also refer to Course 2782: Designing Microsoft
SQL Server 2005 Databases to learn more about SQLCLR objects.
Introduction
When using ADO.NET as an in-process provider, several classes provide contextual information
and the ability to interact with the calling client application.
This object:
Is available from the SqlContext
Introduction
There are various techniques of accessing data by using out-of-process data providers and stored
procedures in T-SQL code. Data can also be accessed from a SQLCLR stored procedure utilizing
the in-process data provider. This demonstration will allow developers to view data retrieved by
each methodology.
Demonstration Overview
In this demonstration, your instructor will explain how to access data from SQLCLR objects by
using the in-process data provider.
4. In the same file, see the code in line 37. This code executes ADO.NET as an out-of-process data
provider to access a T-SQL stored procedure.
5. In Solution Explorer window, navigate to the project TSQL_SP, click the Create Scripts folder,
and then open the Employee_Birthday_TSQL.sql file.
6. In the TSQL_SP project, in the Create Scripts folder, right-click
Employee_Birthday_TSQL.sql.
7. From the context menu, select Run On.
8. If prompted to select a database, reference to the AdventureWorks database on the
MIA_SQL\SQLINST1 server, choose from the list of servers, or create a new reference.
The code executes the same T-SQL statements as it did in line 15 of the DataLayer.cs file of the
ClientApp project, but it is written as a stored procedure and is stored inside the database.
9. In the Data Layer.cs file, go to the code in line 53. This code executes ADO.NET as an out-of-
process data provider to access a SQLCLR stored procedure.
10. In Solution Explorer, in the InProc_SP project, open the Employee_Birthday_SQLCLR.cs
file.
11. The code executes the same T-SQL statements as it did in line 15 of the DataLayer.cs file of the
ClientApp project, but it is written as a stored procedure and is stored inside the database.
Task 5: Identifying the Limitations of TVFs That Access Data with an In-
Process Provider
Task Overview
This task illustrates the limitations of Table-Valued Functions (TVFs) that access data with an in-
process provider.
To illustrate the limitations of TVFs that access data with an in-process provider, perform the
following steps.
1. Switch to the Microsoft Visual Studio 2005 Beta 2 development environment
2. In the InProc_SP project, open the ImpossibleTVF.cs file.
3. Show that the code declares SQLCLR TVF, which returns a resultset read from the database
with the in-process data provider.
4. To run the code in the Test.sql file in the Test Scripts folder, in Solution Explorer window, right-
click the InProc_SP project, select Set as startup project, and press F5.
5. On the View menu, click Other Windows, and then click Output.
View the error message that is displayed. It should read: “System.InvalidOperationException: Data
access is not allowed in this context. Either the context is a function or method not marked with
DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow
method of a Table Valued Function, or is a UDT validation method.”
Introduction
SQLCLR provides new ways of programming tasks that could be programmed with different
technologies, but it is not a substitute for T-SQL or any other API that extends the capabilities of
SQL Server 2005.
When and how you use SQLCLR to implement data access to SQL Server 2005 should depend on
the recommended practices for each of the scenarios in which these technologies were designed to
be used.
Use T-SQL for data access–intensive T-SQL was specifically designed for
operations. direct data access and manipulation in the
database. This is a programming language,
appropriate for handling large sets of data.
Libraries.
Split a long procedure into several smaller Modular programming techniques can be
procedures by using the most appropriate applied when programming for SQL
language for each subprocedure. Server 2005, and therefore, you can
choose the best programming language for
the job.
Do not perform data access operations SQLCLR TVFs return their results in a
from inside a TVF. streaming manner. The process of
streaming of results happens as follows:
1. TVFs load all the results in memory.
2. The TVFs then stream the results back
to the client.
This process leads to a significant increase
of memory usage, depending on the size
of the result set.
Minimize data access operations inside Accessing data from inside triggers should
triggers. be minimized because a trigger executes
on the same transaction as does the client
code that modified the table that fired the
trigger execution.
Triggers increase lock contention if they
do not execute quickly enough. Accessing
data from inside a trigger might increase
execution time; therefore locks will be
held longer.
Consider offloading some code to client- The database server should execute only
side processing. the data related code. Business logic
processing and workflow management
should be moved to other physical layers.
Consider offloading some processing to You can use SQL Service Broker to
external components. communicate asynchronously with remote
servers and to promote distributed
execution and processing.
Section Overview
Creating user applications requires programmatic administration of SQL Server 2005, which
involves administering its various services. You can administer SQL Server programmatically by
using data object models only.
This section analyzes the changes in the management API of SQL Server 2005. This section also
discusses techniques for administering various SQL Server services programmatically.
Section Objective
In this section, you will learn about the data object models for administering SQL Server 2005
components and objects.
Introduction
In SQL Server 2000, SQL Server Enterprise Manager used the SQL-DMO API to administer and
communicate with SQL Server. Administering SQL Server 2000 involves administering server
objects; creating, updating, and deleting databases and their artifacts; executing administrative
tasks; and managing SQL Server 2000 services.
Using SQL-DMO, you can create applications that administer SQL Server 2000 in the same way as
SQL Server Enterprise Manager does.
SQL-DMO is also available with SQL Server 2005.
Demonstration Overview
In this demonstration, your instructor will illustrate how SQL Server Enterprise Manager uses SQL-
DMO to administer SQL Server and how database application can use SQL-DMO directly.
The recorded trace shows the events that were produced in SQL Server Profiler while you
navigated the object tree in SQL Server Enterprise Manager.
Introduction
SQL Management Objects (SMO), the successor of DMO, is a managed API in SQL Server 2005.
SMO exposes an object model to help you develop applications which execute administrative tasks
on a SQL Server instance. You can also use SMO to administer a server instance of SQL Server
2000.
Demonstration Overview
In this demonstration, your instructor will illustrate how to administer SQL Server 2005
programmatically by using SQL Management Objects (SMOs).
Pay special attention to the DatabaseCollection branch. Notice the child elements such as Database,
Filegroup, and LogFile.
Introduction
Replication Management Objects (RMO) is a managed API that automates administrative tasks
related to SQL Server Replication Services.
RMO and SMO specialize in specific administrative tasks, but work together for the programmatic
administration of a SQL Server instance. However, SQL-DMO is a single API that handles all the
administrative tasks.
Demonstration Overview
In this demonstration, your instructor will illustrate how to administer SQL Server 2005 replication
programmatically by using Replication Management Objects (RMO).
Pay special attention to the “” branch. Observe all the child elements such as “”.
Discussion Question
1. How does RMO relate to DMO and SMO?
Your instructor will explain the new relationship and similarities between RMO, DMO, and SMO.
Introduction
Analysis Management Objects (AMO) and Analysis Services Scripting Language (ASSL) are two
different tools for communicating with Analysis Services on a SQL Server 2005 instance. AMO is
a managed API used in managed applications to administer Analysis Services. ASSL is an XML
dialect that is used to execute Data Definition Language (DDL) statements to create, modify, or
delete objects on an instance of Analysis Services. ASSL is also used as a command language in
which to send action commands.
Demonstration Overview
In this demonstration, your instructor will illustrate how to administer SQL Server 2005 Analysis
Services programmatically by using AMOs and ASSL.
6. Pay special attention to the “” branch. Observe all the child elements such as “”.
7. On the toolbar, click Search, and then search for the phrase “Analysis Services Scripting
Language XML Element Hierarchy (ASSL).”
8. Select the topic titled Analysis Services Scripting Language XML Element Hierarchy
(ASSL).
9. Review the list of XML elements declared by ASSL.
Pay special attention to the root object “Server”; then see all the child objects, such as Databases,
Dimensions, and Cubes.
Discussion Question
How does AMO relate to DMO and SMO?
Your instructor will explain how AMO relates to DMO and SMO.
Next Steps
Introduction
The information in this section supplements the information provided in Session 1.
“Data Points: ADO.NET and System.Transactions” -- MSDN Magazine, February 2005
• http://msdn.microsoft.com/msdnmag/issues/05/02/DataPoints/default.aspx
Managed Data Access Inside SQL Server with ADO.NET and SQLCLR
• http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/mandataaccess.asp
MSDN TV: Introducing System.Transactions in .NET Framework 2.0
• http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20050203NETMC/manifest.
xml
Using CLR Integration in SQL Server 2005
• http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/sqlclrguidance.asp
Discussion Questions
1. What was most valuable to you in this session?
2. Have you changed your mind about anything based on this session?
3. Are you planning to do anything differently on the job based on what you learned in this session?
If so, what?
Contents
Session Overview 1
Section 1: Exception Types and Their
Purposes 3
Section 2: Detecting Exceptions 17
Section 3: Managing Exceptions 33
Next Steps 47
Discussion: Session Summary 48
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, <The publications specialist places the list of trademarks provided by the copy editor
here. Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order.>
are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or
other countries.
<The publications specialist inserts mention of specific, contractually obligated to, third-party
trademarks, provided by the copy editor>
Session Overview
Exceptions are unexpected behaviors caused by user interactions or database systems. Exceptions
occur because the code of a database application cannot predict all the potential actions of users or
database systems.
A well-designed exception handling strategy shields an application from unexpected events and
enhances the user experience. A well-designed exception handling strategy functions in the
following sequence:
1. If an application is unable to detect an exception, the exception is sent directly to the caller,
which can be a user of the application, another application, or a component using the functionality
of the database application.
2. If an exception is detected, the application tries to recover from it automatically. If the
application is unable to recover from the exception, it:
a. Gathers information about the exception and adds any contextual information.
b. Logs error information synchronously or asynchronously to a data store.
c. Sends notifications synchronSously or asynchronously to the system.
d. Performs a cleanup.
e. Displays information to the user.
f. Propagates the exception to the caller.
In this session, you will learn about the various types of exceptions that can occur in a Microsoft®
SQL Server™ 2005 system. In addition, you will learn to design strategies for detecting exceptions
at the appropriate layer. You will also learn to log and communicate exceptions according to your
business requirements.
Session Objectives
Describe the various types of exceptions that can be detected in a SQL Server 2005 system and
how they affect applications and users.
Design strategies to detect exceptions at the appropriate layer.
Design strategies to log and communicate exceptions according to business requirements.
Section Overview
Applications should have a clearly defined strategy for providing consistent and coherent
information to a caller. This strategy should include exception management.
In this section, you will learn about the various types of exceptions that can occur in a SQL Server
2005 system and about the various exception severity levels. You will also learn about platform
monitoring tools that can be used to provide more information to system administrators. Using this
information, system administrators can take proactive measures against exceptions by fine-tuning
and adjusting applications.
Section Objectives
Explain the various types of exceptions that can occur in a database system.
Explain the various exception severity levels.
Explain the techniques for programmatically exposing exceptions to common Microsoft
Windows® administrative tools.
Explain the various techniques for handling exceptions produced by data integrity violations.
Apply the guidelines for creating a user-defined exception strategy.
Explain how to create user-defined messages and how to write applications to use them.
Introduction
Exceptions are not necessarily errors. They can be informational messages that inform users,
database administrators, or other applications about how a database system is working. Whether or
not an exception represents an error is determined by the application in which it occurs.
The various types of exceptions are classified based on the situations in which they originate. This
topic covers the types of exceptions and the situations in which they originate.
Informational Messages
Informational messages are exceptions that are not considered critical. An informational message,
informs the user or the calling application about a certain condition in a database or about how the
database system is functioning.
Introduction
The severity level of an exception indicates how critical the exception is. It also indicates actions
that a database system should take to prevent the exception or to recover from it. Applications can
react to exceptions based on their severity levels and decide which course of action to adopt.
Introduction
When an error is detected in an application, system or database administrators can identify the
exceptions by using Windows and SQL Server tools. These tools are used as logging engines, and
monitoring and tracing utilities. The information stored by these tools enables the application
development team to handle errors appropriately.
Demonstration Overview
In this demonstration, your instructor will illustrate how to programmatically expose exceptions to
common Windows administrative tools.
Introduction
Data integrity violations are the most common source of exceptions in database applications. How
you handle these exceptions depends on the context and scenario of the exceptions.
This discussion covers how data integrity violations occur, the type of exceptions produced by data
integrity violations, and the techniques for handling them.
Discussion Questions
1. Why is declarative referential integrity better for error handling?
2. How costly is rolling back a transaction when there is an error?
3. Is there anything to roll back when there is an integrity violation? How much of the transaction is
rolled back in such an event?
4. Do you roll back transactions in triggers? Is there an alternative?
5. Are there any differences in the behavior of the INSTEAD OF and AFTER triggers related to
data integrity checks?
Introduction
The predefined messages in SQL Server 2005 might not be enough to provide appropriate
exception handling for the following reasons:
The message might be too cryptic for users to grasp.
The message might contain confidential internal information that is not intended for external
callers.
Some messages are to be handled only by system or database administrators.
•
Applications should implement a proper exception-handling strategy. To achieve this goal, the
applications should declare clear rules for using specific exceptions appropriate to the intended
recipient. The recipient of exceptions can be an end user, another application, or a component. The
recipient will use the exception and the information generated by the exception to determine a
specific course of action.
• If the messages are meant for users, check the language used. Messages should contain as
much information as possible in order for users to react to the exception.
• Messages meant for an application or a system, contain relevant information only.
Design messages that are accepted internationally.
• Create as many localized messages as needed for all target languages.
• If a message is parameterized to accept dynamic arguments, validate whether the arguments are
in the correct language.
Important Remember that only messages sent to the event log can fire SQL Agent Alerts.
Introduction
T-SQL provides various mechanisms to send user-defined error messages to applications.
Depending on the error type and severity, client applications can use these messages to determine
an appropriate course of action.
Demonstration Overview
In this demonstration, your instructor will illustrate how to create user-defined messages and write
applications to use them
The application also listens to Application Log in Windows Event Log and catches the message
logged by xp_logevent.
9. Close the console application window.
10. Close the Microsoft Visual Studio 2005 Beta 2 .
Section Overview
The efficiency and stability of an application depend on its ability to handle run-time exceptions or
errors. You can develop an application to resolve exceptions in a better way if you have adequate
information about the exceptions, such as what caused them and whether they originated in the
server side or client side. With the help of this information, you can decide whether an exception
requires user interaction or can be resolved by the database system.
In this section, you will learn how exceptions can be detected in a database system. You will also
learn various design strategies for detecting exceptions at the appropriate client-side layer, and how
to obtain more information about these exceptions. Additionally, you will understand the reasons
why the compile-time and run-time exceptions occur, and the guidelines for minimizing the
problems caused by these exceptions.
Section Objectives
Identify the database system layers where exceptions can be detected.
Explain how to obtain information about exceptions.
Explain the structure of the TRY…CATCH technique and the benefits of using it.
Explain why compile-time exceptions are generated, and explain the guidelines for minimizing
problems caused by these exceptions.
Explain how to detect integrity exceptions.
Explain why run-time exceptions are generated, and explain the guidelines for minimizing
problems caused by these exceptions.
Explain why deadlocks occur and how to detect them.
Introduction
In a database system, both the server and the client layers can raise exceptions to signal unexpected
conditions. When an exception is raised, both the database server and the client application should
obtain information about the exception by using adequate functions and programming techniques to
handle the exception appropriately.
Additional Information Read the section “SQL Server Error Messages” in SQL Server 2005
Books Online (BOL).
Introduction
Exception-related information is critical for database applications and for database administrators.
Database applications obtain and use this information for handling exceptions. Database
administrators require this information to manage database systems.
Exception information is available to database applications if they use the T-SQL functions
provided by SQL Server. Database applications based on ADO.NET can use specific classes to
obtain exception information.
Database administrators can access exception information by using SQL Server Profiler.
Demonstration Overview
This demonstration illustrates how to obtain information about exceptions using T-SQL code, client
ADO.NET code, and the SQL Server Profiler tool.
3. Scroll down to line 8, where the T-SQL code uses the sp_addmessage system stored procedure to
declare a custom exception. .
4. Show the RAISERROR expression in line 15.
5. Select the code from lines 1–11. To execute the selected code, press F5.
6. Select the code from lines 14–25. To execute the selected code, press F5.
7. View the results in the Results pane.
8. In Solution Explorer, in the 2783M2L2Demonstrations solution, open TryCatch.sql.
9. Scroll down to line 8, where the T-SQL code uses the sp_addmessage system stored procedure to
declare a custom exception.
10. Scroll down to line 14.
11. Show the function calls on line 23.
12. Select the code from lines 1–11. To execute the selected code, press F5.
13. Select the code from lines 14–30. To execute the selected code, press F5.
14. View the results in the Results pane.
Introduction
Using the TRY…CATCH construct expands the functionality of the @@ERROR function. The
TRY…CATCH construct provides the following advantages:
A structured programming model for easy management and maintenance of code
More contextual information with the ERROR functions
•
Note Although the TRY…CATCH construct is common in many programming languages, the
use of this construct varies with each implementation.
Gathering Information
Inside a CATCH block, you can use the following functions to obtain information about an
exception:
ERROR_LINE
ERROR_MESSAGE
ERROR_NUMBER
ERROR_PROCEDURE
ERROR_SEVERITY
ERROR_STATE
Compile-Time Exceptions
Introduction
Compile-time exceptions prevent the database engine from building an execution plan. This type of
exception is often produced by a syntactic error in the coding. Compile-time exceptions in stored
procedures and in other objects are detected when they are created. As a result, application
developers do not have too many errors to detect. However, when an application uses dynamic
execution to execute dynamically constructed queries, compile errors might occur.
Introduction
Integrity violations are generated when a database operation breaks a constraint on a database
server. Integrity is enforced by declarative integrity checks and procedural integrity
implementations.
Declarative integrity violations are managed by the database server, and the client application needs
to handle the exception raised by the server.
Demonstration Overview
In this demonstration, your instructor will illustrate how to detect integrity exceptions
7. If prompted to select a database reference, from the Available References list, choose mia-
sql\sqlinst1.master.dbo, or click Add New Reference. In the New Database Reference window, in
the Server Name box, type MIA-SQL\SQLINST1, select Windows Authentication, and then
select Master from the list of databases.
8. In the EnforceConstraints project, in code view, open Form1.cs.
9. Scroll to line 22 to view the ADO.NET code that executes the InsertTable1SP stored procedure.
Notice that lines 33 and 34 caused the UNIQUE constraint violation because they tried to insert the
same value twice.
10. Right-click the EnforceConstraints project, and then on the shortcut menu, click Set as
StartUp Project.
11. To execute the application, press F5.
12. On the sample application, click Entity Integrity.
13. Click OK to close the message box.
14. Close the Checking Integrity Demo application.
Run-Time Exceptions
Introduction
Run-time exceptions prevent the database engine from finishing the execution of a statement. This
type of exception is difficult to detect during the development phase, because it is introduced as a
result of unexpected behavior, such as passing incorrect arguments to functions and assigning data
that exceeds the limits of a data type.
Introduction
A deadlock is a condition in which two transactions or processes cannot proceed because they are
waiting for each other’s mutually locked resources. SQL Server 2005 implements an automatic
deadlock detection engine that detects and breaks a deadlock by terminating one of the processes.
A client application should be developed to handle this type of exception and re-execute the
statement that caused the deadlock.
Demonstration Overview
In this demonstration, your instructor will explain why deadlocks occur and how to detect them.
Section Overview
Exceptions can be defined as unexpected events that occur during the execution of an application. If
exceptions are not handled by the application itself, they are handled by the operating system. In
this case, the operating system terminates the application process. When users try to handle
exceptions, it often results in loss of data.
An exception handling strategy is often not taken into account when you consider the business
requirements for an application. As a software developer or a database architect, you must
understand that exception handling techniques affect the performance of an application, which in
turn can affect the company’s image and revenue.
In this section, you will learn how to log and communicate exceptions according to your business
requirements. You will also learn how to manage exceptions on the server and on the client and
how to transfer messages between them. In addition, you will learn about SQL injection attacks and
how to avoid these attacks.
Section Objectives
Explain the considerations for managing exceptions.
Explain the considerations for logging exceptions and for determining the locations where
exceptions can be stored.
Explain the importance of producing meaningful and useful error messages.
Explain how to manage exceptions on the server and on the client and how to transfer messages
between them.
Apply the guidelines for proactively managing exceptions.
Explain the guidelines for filtering user input to avoid SQL injection attacks.
Introduction
When designing a system, you must ensure that the system is capable of:
Gathering information.
Logging error information.
Sending notifications.
Performing cleanup.
Displaying information to users.
By building these capabilities into applications, developers can develop self-protecting applications
that will notify system administrators whenever there is an unexpected situation that the application
cannot handle. Developers can also create applications that give adequate contextual information
and feedback on the problems. This information will enable the development team to analyze and
rectify the problem.
In this topic, you will learn about the various considerations for managing exceptions.
Logging Exceptions
If an exception is not logged, it might not be noticed by the system manager or the development
team. By logging exceptions, the development team ensures that it shall be notified about problems
from which the application cannot recover by itself.
Note Logging information without proper planning might result in circular logging. In circular
logging, certain logged information triggers a process that logs more information. Thus,
circular logging starts an endless process of logging information, which consumes important
system resources.
Preventing Exceptions
Monitoring and notifications are two important processes by which you can prevent exceptions
from occurring.
Monitoring: An application should provide monitoring tools to enable system administrators to
determine the health of the application.
Notifications: An application should quickly and accurately notify system administrators of any
problem that the application experiences. Without appropriate notifications, exceptions might not
be detected.
Introduction
Applications should be designed to expose instrumentation information. The instrumentation
information should be decoupled from the application code. Logging is an important part of the
instrumentation of applications. If exceptions are logged, developers need not change the
application source code every time a modification is required in the notification mechanism.
In this topic, you will learn about exception logs and the guidelines for logging exceptions.
Exception Logs
The exception log should record all the necessary information so that the different stakeholders can
resolve the exception. Moreover, the logs should be located close to the layer where the exception
occurs, and the logs should be easily accessible to the system administrators. The log format and
storage depends on the log location. The log files can be maintained in textual, XML, relational, or
custom binary format.
Guidelines
When you are designing an application, you should follow certain guidelines for logging
exceptions.
The following are some of the important guidelines that you should follow for logging exceptions:
The application should keep a log for each application boundary.
• Exceptions are produced by different layers. These layers should be as self-contained and self-
sufficient as possible. By maintaining a log for each application boundary, the dependency of
an application layer on other layers to log exceptions is eliminated.
• Client-side log (if communication with the server is intermittent or if the application is written
as a Smart Client application).
Create several types of logs, each type depending on the type of information that the log will hold
and the type of actions required.
• Each log typically serves a different purpose, and can be analyzed by different teams. Splitting
the log information into different logical logs makes the processing of these logs more
efficient.
• The following are various kinds of logs you can create:
Administrative log
Reporting log
Communications log
Custom log
Choose the relevant log format and storage option.
• Each log format and storage type has specific advantages and disadvantages and serves
different purposes. The log should be in an appropriate format for the callers to process.
• Each storage option provides various log formats such as textual, XML, relational, and custom
binary format. The storage options include:
Enterprise Instrumentation Framework (EIF)
Windows Event Log service
A central, relational database such as SQL Server 2005
Custom log file
Introduction
You should produce meaningful and useful error messages relevant to the given caller. The caller
can be a user, another internal component, or an external application. The error messages should
contain information that will enable the caller to resolve the exception.
Discussion Questions
1. Why do you use error messages?
2. Do you transfer system exceptions directly to other layers, or do you transfer your own custom
exceptions?
3. Are your messages designed to be understood by the target audience? For example, do you create
different types of messages for people and components?
4. Do you parameterize messages?
5. Do you try to avoid sending messages when there is nothing that the target user or component
can do about the error?
6. Is it important to use exception logging and monitoring mechanisms to facilitate administration
and operations?
Introduction
The capabilities for handling and managing errors in the server components and the client
components of an application vary.
Following is the process of handling an exception in a distributed application:
1. The server handles the error on the server-side and logs the error for the system administrator to
check.
2. The server-side error is then transferred from the server to the client.
3. The client handles the error and reacts to it. The client application might decide to present a
message to the user.
This demonstration will show how to evaluate the different techniques for managing database
exceptions on client applications.
Demonstration Overview
The instructor will demonstrate a complete cycle for handling an error both on the server side and
on the client side.
The application will cause a data integrity conflict while updating data inside a DataSet object.
6. Right-click the ManagingErrors project, and on the shortcut menu, click Set as StartUp Project.
7. To execute the application, press F5.
The client application executes, opens, and displays the data.
8. For the first credit card on the list with CreditCardID value equal to one, double-click the
ExpMonth column to enter the edit mode, and then enter a value greater than or equal to 1, but less
than or equal to 12.
9. After making the change, press ENTER to exit the edit mode and move to the second row.
10. For the second credit card on the list with CreditCardID value equal to two, double-click the
ExpMonth column to enter the edit mode, and then enter a value greater than or equal to 1, but less
than or equal to 12.
11. After making the change, press ENTER to exit the edit mode.
12. Return to SQL Server Management Studio. For the first credit card on the list with
CreditCardID value equal to one, in the CreditType column, change the card type from
“SuperiorCard” to “InferiorCard.”
13. After making the change, press ENTER to exit the edit mode.
14. Return to the Credit Card Manager application.
15. On the toolbar, click Save Updates.
The first row is marked with a red exclamation point, indicating that there is a concurrency
violation on this record.
16. Open Event Viewer, and in the left pane of the Event Viewer, select the Application log.
17. In the Source column, at the top of the list, double-click the CreditCardManager event and
read the message. “Row ID:1, cause Concurrency violation: the UpdateCommand affected 0 of the
expected 1 records.”
18. Click OK to close the Event Properties dialog box.
19. Close Event Viewer.
20. Close Credit Card Manager.
21. Close SQL Server Management Studio.
Introduction
Exception handling should start as a reactive measure. However, as an application matures through
several application development life cycles, common exceptions should be handled and addressed
to prevent such exceptions from recurring.
By following guidelines to prevent exceptions, you can transform exception handling into a
proactive measure that will improve the quality of the application and enhance the user experience.
Introduction
An SQL injection attack is a technique in which the administrators can use non-validated input to
pass SQL commands to a database for execution. You can prevent SQL injection attacks by
Demonstration Overview
A SQL injection attack is a technique used to pass and execute SQL commands through non-
validated inputs to a database management system.
The instructor will demonstrate how to cause and prevent a SQL injection attack. This demonstrates
a proactive measure to avoid unhandled exceptions.
Task Overview
This task executes an application and launches an SQL injection attack on a client.
To execute an application and launch an SQL injection attack on a client, perform the following
steps.
1. Open Program.cs.
Notice that there are two lines of code. One line of code executes the Attack form, and the other
executes the Defend form.
2. Type // before the Application.Run(new Defend()); line of code to make it a comment.
Comment out the Application.Run(new Attack()); line of code.
3. Right-click the SQLInjection project and select Set as StartUp Project.
4. To execute the client application, press F5.
The application opens the Attack window, which contains a box, a search button, and a grid to
display the results.
5. In the box, type 3333112, and then click Search.
6. Browse the data grid by clicking the plus (+) signs until the results appear. Notice that nine rows
are displayed.
7. In the box, type 3333112’ or 1=1 --, and then click Search.
8. Browse the data grid by clicking the plus (+) signs until the results appear. Notice that several
rows are displayed.
9. In the box, type 3333112%’; SELECT * FROM Sales.Currency --, and then click Search.
10. Browse the data grid by clicking the plus (+) signs. Notice that Table and Table1 are the two
results of the search.
11. To view the expected results, select Table.
12. Browse back by clicking the left arrow on the top right corner of the grid.
13. Select Table1 to view the currency list, which is an unexpected result.
14. Close the Search Form – Attack application.
Next Steps
Introduction
The information in this section supplements the content provided in Session 2.
Next steps include visiting the Microsoft Web site to download:
Exception Management Architecture Guide
• http://www.microsoft.com/downloads/details.aspx?FamilyId=73742594-DB15-4703-8892-
75A569C4EB83&displaylang=en
Discussion Questions
1. What was most valuable to you in this session?
2. Based on this session, have you changed your mind about anything?
3. Are you planning to do anything differently on the job based on what you learned in this session?
If so, what?
Contents
Session Overview 1
Section 1: Common Scenarios for Cursor-
Based vs. Result Set–Based Operations 2
Section 2: Selecting Appropriate Server-Side
Cursors 13
Section 3: Selecting Appropriate Client-Side
Cursors 24
Next Steps 34
Discussion: Session Summary 35
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, <The publications specialist places the list of trademarks provided by the copy editor
here. Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order.>
are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or
other countries.
<The publications specialist inserts mention of specific, contractually obligated to, third-party
trademarks, provided by the copy editor>
Session Overview
Relational database systems work in a set-oriented manner. To save valuable system resources, they
use specific algorithms to optimize the processing of multiple rows.
However, many applications work with individual objects that are stored in individual rows in one
or more tables. This is often referred to as row-by-row processing, because the applications need to
access the database one row at a time.
All SQL dialects provide cursors to support row-by-row operations. As developers and database
administrators, you must understand the importance of cursors in a database application and ensure
that the cursors perform only those functions that they were designed to perform.
Depending upon the scenario, it might be appropriate to use server-side cursors or client-side
cursors. In some cases, it is better not to use cursors at all.
There are no definite guidelines on whether or not to use cursors. Each programming technique has
its own advantages and disadvantages, and this is also true for cursors. Discovering the adequate
application scope for cursors is the main purpose of this session. It explains the scenarios in which
cursors are appropriate, and how to use them to optimize the use of system resources.
Session Objectives
Explain when cursors are appropriate and when they are not.
Explain the considerations for selecting server-side cursors.
Explain the considerations for selecting client-side cursors.
Section Overview
Many applications use cursors. Developers, as well as systems and database administrators, must
understand why they use cursors instead of other programming techniques.
This section compares the different programming techniques that you can use when dealing with
operations that are ideal for row-by-row operations. This section also explains the scenarios in
which you should use a cursor-based approach and the scenarios in which you should use a set-
based approach.
Database programmers should select an approach based on the design of the database system. They
should also consider the performance, usability, and maintainability of the system.
In this section, you will learn when cursors are appropriate and when they are not.
Section Objectives
Explain why cursors are used in an application.
Explain the difference between row-based and set-based operations.
Explain the guidelines for using set-based operations.
Explain the guidelines for using row-based operations.
Explain how to replace cursors with set-oriented operations.
Introduction
Programmers use cursors in database applications for various reasons, including the following:
Cursors are a natural programming technique.
Using alternatives to cursors is not always optimal.
It might be too difficult to solve a problem without using cursors.
In some database applications, the data access component automatically creates cursors. However,
most database applications do not create Transact-SQL cursors explicitly, and they implement row-
by-row operations by looping through prebuilt row-sets. Adopting this technique deteriorates the
database application performance. Instead, improve performance by using cursors natively in
database applications.
This section discusses the various reasons for using cursors and the problems that are considered
difficult or impossible to solve without using cursors.
Discussion Questions
1. Why do you use cursors?
2. What business problems can you solve by using cursors?
3. Do you use dynamic T-SQL execution inside cursors?
4. Do you combine cursors with temporary tables?
5. Do you fetch column values into variables? What do you do with those variables later in the
code?
6. Do you keep more than one cursor open in the same procedure?
7. What type of cursors do you typically use?
8. Do you use cursors inside triggers?
9. Do you use cursors inside user-defined functions?
Introduction
Microsoft® SQL Server™ implements different types of cursors in unique ways. Each
implementation has specific implications in terms of storage required, concurrency, and
performance.
Understanding the behavior of each cursor helps you to select the appropriate type of cursor for
each situation. You will also be able to meet business requirements by using server resources in an
optimal manner.
This presentation shows how different types of cursors run in a SQL Server database and how these
differences affect system resources, concurrency, and database server performance.
Discussion Questions
1. Which cursor type uses more disk space?
2. Which cursor type uses more processing power?
3. Which cursor type produces fewer concurrency problems?
4. What is a result-set operation?
5. Why do programmers process data row by row instead of using result sets?
6. Explain how SQL Server executes result-set operations more efficiently than row-by-row
operations do.
7. Are there good and bad cursor strategies?
Introduction
The design of the relational database management system (RDBMS) optimizes input/ouput (I/O) by
using optimized algorithms that process result sets efficiently. Most RDBMS offer some type of
cursor-based operations. However, consider other alternatives before using cursor-based operations.
This topic discusses important guidelines that you should follow to benefit from the result set–
based algorithms of SQL Server.
Favor Queries that Affect Groups of Rows Rather Than One Row at a
Time
The storage architecture of SQL Server is optimized to efficiently access data by sequentially
reading the data and the index pages. Based on this architecture, applications do not need to reread
a page to search for another piece of information that might be available on the page.
When an application requests operations that affect one row at a time, SQL Server might have to
read the same page multiple times.
Searching for a range of data can be very efficient when the query uses an index designed for this
purpose. The range of data can be read sequentially because the data is stored in a specific order in
the leaf level of the index. Sending a database request that is designed to take advantage of this
database feature is more efficient than designing client-based algorithms to access the same range
of data in a different way.
In these cases, the query would not benefit from being a set-oriented operation, because SQL Server
will serialize the execution.
This repetitive call to an external execution plan could result in performance problems.
Introduction
Cursors are a powerful feature that can solve complex business scenarios in a database application.
However, cursors are not properly used in many applications.
Generally, developers follow various methods of designing applications, and most of them have
strong opinions about when and why to use cursors.
During this discussion, you can share your experiences with effectively using cursors. You can also
share potential alternatives that are relevant to the discussion.
Discussion Questions
1. Is your application constrained by development time or execution time?
2. Do you need to build statements to execute dynamically depending on row values?
3. Do you need to execute data definition language (DDL) commands dynamically?
4. Are calculations so complex and varied that they need to be evaluated independently on a row-
by-row basis?
5. How many different types of calculations do you need to apply?
6. Did you try to split these operations into a group of independent, set-oriented operations?
Introduction
Earlier topics in this session discussed the appropriateness of using cursors and result-set
operations. As database developers and administrators, you should understand and be able to
identify the situations in which you should replace cursors with a set operation.
Demonstration Overview
In this demonstration, your instructor will explain two typical uses of cursors and demonstrate how
to convert them into solutions that do not use cursors and that improve performance.
These are only two examples of how to identify situations where cursors were used to solve
solutions that require a different approach.
6. Using the mouse, select the call to the method GetAllPOsWithClientJoin in line 78, and
then press F12 to browse to the definition of the method.
7. Notice the call to the GetPOHeaders method in line 69.
8. Notice the foreach loop in line 75.
9. Notice the call to the GetPODetailByID method in line 78.
10. Scroll up to line 13, to the definition of the DataLayer class and the
header_sql_command and details_sql_command constant variables.
11. Right-click the ClientSideJoin project, and then click Set as StartUp Project.
12. To run the application, press F5.
13. The application opens the form Client Side Join.
14. Verify that the Client Side option is selected.
15. To run the sample, click Start.
Notice the value presented for Elapsed Time.
10. In the ResetDatabase project, in the Create Scripts folder, open the
ApplyCategoryAdjustment.sql file.
11. In the Create Scripts folder, right-click the ApplyCategoryAdjustment.sql file, and on the
shortcut menu, click Run On.
12. If prompted to select a database reference, to connect to the AdventureWorks database on the
MIA-SQL\SQLINST1 server, choose from the list of servers, or create a new reference.
13. Right-click the ExecuteComplexOperation project, and click Set as StartUp Project.
14. To run the application, press F5.
15. The application opens the Execute Complex Operation form.
16. Verify that the Server Side option is selected.
17. To run the sample, click Start.
18. Close the Execute Complex Operation application.
19. Return to Visual Studio 2005 Beta 2.
20. In the ResetDatabase project, open the CursorAdjustment.sql file.
21. Close Visual Studio 2005 Beta 2.
Discussion Questions
1. Referring to the two examples in this demonstration, explain why these cursors need to be
replaced.
2. Is there additional development overhead to replace these cursors?
3. Is there additional maintenance overhead?
4. Do you actually gain performance?
5. Do you reduce contention?
Section Overview
Server-side cursors should be used only when cursor operations are required to support application
functionality, such as when you want to process only certain rows of the entire result set or when
you want to retrieve only a part of the result set from the server.
Server-side cursors offer better performance because only the fetched data is sent over the network,
and the client application does not need to cache large amounts of data.
To select which type of server-side cursors to use, database developers must understand their
functionality, how SQL Server 2005 implements server-side cursors, and how conditions in the
database server affect cursor functioning. This section explains the factors to consider before
selecting server-side cursors.
Section Objectives
Explain the life cycle of a T-SQL cursor and the server-side resources that the cursor uses.
Explain the performance implications of using row-by-row operations that do not use cursors.
Explain how the transaction isolation level affects the behavior of various cursor types.
Describe scenarios in which positional updates are appropriate.
Introduction
The life cycle of a cursor consists of the following stages:
1. Declaring the cursor and associating it with the result set of a T-SQL statement
2. Executing the T-SQL statement to populate the cursor
3. Retrieving rows in the cursor
4. Closing the cursor
Each of these operations affects resource utilization on the database server. Resource utilization
depends on several factors, such as the cursor type, the number of rows read, cursor behavior, and
the locking scheme.
Demonstration Overview
There are some general steps followed when creating a cursor:
Declare the cursor, and associate it with the result set of a Transact-SQL statement.
Execute the Transact-SQL statement to populate the cursor.
Retrieve the rows in the cursor that interest you.
Close the cursor.
Each of these operations represents a cost in terms of resource usage on the database server. This
cost can be high or low, depending on several factors such as the cursor type, number of rows read,
cursor behavior, and locking scheme.
In this demonstration, your instructor will demonstrate how to measure the cost of running different
types of cursors.
Additional Information Cursors force the database engine to repeatedly fetch rows, negotiate
blocking, manage locks, and transmit results.
Using more locks than required impacts the tempdb database. The impact varies according to
the type of cursor used.
The forward-only, read-only cursor is the fastest and least resource-intensive way to get data
from the server. This type of cursor was earlier known as a “firehose” cursor or a local fast-
forward cursor.
A cursor that has a DECLARE statement that selects data from a temporary table usually
causes a recompile. Therefore, avoid using cursors over temporary tables.
To learn more about cursor types, cursor locking, and the impact of these on the tempdb
database, read the chapter “Cursors (Database Engine)” in SQL Server 2005 Books Online
(BOL).
5. Click Close.
6. Open SQL Server 2005 Profiler.
7. To create a new trace, on the File menu, click New Trace.
8. If asked to connect to SQL Server, use Windows Authentication to connect to the MIA-
SQL\SQLINST1 server.
9. SQL Server Profiler displays the Trace Properties window.
10. On the Use the Template property, click Blank.
Cursors CursorClose
CursorExecute
CursorOpen
CursorPrepare
TSQL SQL:BatchCompleted
Introduction
T-SQL is a set-based language, so it does not work well with solutions that need to perform row-
by-row operations within a result set.
Generally, the performance of server-side T-SQL cursors is lower than the performance of a set-
based solution. T-SQL cursors do not fully utilize the power of a relational database engine, which
is optimized for non-sequential, set-based queries.
Demonstration Overview
Every cursor-based query can be coded in an equivalent set-based solution. Your instructor will
demonstrate different techniques for avoiding T-SQL server side cursors and will explain the
performance implications of using row-by-row operations that do not use cursors.
Introduction
Transaction isolation level affects the behavior of cursors and of regular SELECT statements in a
similar manner. Transaction isolation level is used to control how transaction activities are isolated
from each other. By using appropriate locking mechanisms, you can manage concurrent access to
information that is being used by several simultaneous transactions.
The difference between the behavior of transactions with SELECT statements and the behavior of
the transactions when using cursors is the moment in time when the locks are acquired and released.
This timing depends on the type of lock, the locking schema, and the isolation level specified.
Demonstration Overview
Your instructor will demonstrate how each T-SQL server-side cursor type is affected by the
transaction isolation level, and how both the cursor type and the isolation level affect the locking
mechanism in SQL Server 2005.
MSSQL$SQLINST1:Transactions Transactions
MSSQL$SQLINST1:Locks Lock Requests/sec Database
MSSQL$SQLINST1:Locks Lock Requests/sec Key
MSSQL$SQLINST1:Locks Lock Requests/sec Object
MSSQL$SQLINST1:Locks Lock Requests/sec Page
5. Click Close.
10. adLockReadOnly
11. Click Execute.
12. Select the following combination of boxes in the list:
13. adOpenDynamic
14. adXactSerializable
15. adLockPessimistic
16. Click Execute.
17. If time permits, try different combinations, and explain the effect of each combination.
18. Close Visual Studio 2005 Beta 2.
19. Close Performance Monitor.
Introduction
When implementing server-side cursors, database developers might want to update the result set
that is being navigated on a row-by-row basis.
The function of positional updates is similar to that of the row-by-row update function of a
structural programming language. T-SQL is a set-oriented language and, therefore, trying to enforce
a row-by-row update mechanism might affect the overall performance of the database server.
Discussion Questions
1. When should you update records row-by-row?
2. Are you using variables that are being fetched as values for the columns to be updated?
3. Are you using these variables as arguments in the WHERE clause of the UPDATE statements?
4. Are there any performance implications of doing this?
Section Overview
Some data access providers enable the client application to cache a copy of a result set into the
memory on the client machine and fetch each row one-by-one to the client application. This is
called a client-side cursor.
The main benefit of client-side cursors is that the connection to the database can be closed, and
therefore, no locks are held while browsing through the result set. This improves client application
performance.
Server-side and client-side cursors provide different approaches to browsing data on a row-by-row
basis. Based on the database requirements, application developers must decide whether to use
server-side or client-side cursors.
This section provides information about the various client data access libraries that support client-
side cursors. It explains how to use cursors from SQL Native Client (SQLNCLI) and how to
perform row-by-row operations by using DataSet and DataReader objects. You will also learn
about the SQL Server activities produced by these operations.
Section Objectives
Describe the client data access libraries that support client-side cursors.
Explain how to use cursors from SQLNCLI.
Explain how to perform row-by-row operations by using DataSets and DataReaders, and explain
the SQL Server activity produced by these operations.
Explain the guidelines for selecting client-side cursors.
Introduction
Implementation of client-side cursors depends on which data access provider you choose.
Application developers usually choose data access providers based on factors such as database
server support, transaction support, security features, performance, and technical support.
Developers should also consider client-side features such as connection pooling and client-side
cursors.
Each data access provider supports a different feature set. Some of the data access providers, such
as OLE Database (OLE DB) and Open Database Connectivity (ODBC), were designed to work
with multiple data sources. Some of the providers, such as SqlClient and the Sql Native data access
provider, were designed to work specifically with a single data source and to provide native support
for that data source.
In the OLE DB, ODBC, and Active data object (ADO) specifications, a cursor is implicitly opened
over any result set that is returned by a T-SQL statement. However, application developers can
modify this behavior by changing the properties of the object that will execute the T-SQL
statement.
Additional Information For more information about data access providers, refer to Session 1,
“Choosing Data Access Technologies and an Object Model.”
Introduction
The SQLNCLI data access provider is the most recent SQL Server 2005 data provider. It supports
the previous OLE DB and ODBC interfaces, as well as the new features of SQL Server, such as
XML, new data types, Multiple Active Result Sets (MARS), and user-defined types.
Demonstration Overview
In this demonstration, your instructor will show how to use cursors from SQLNCLI.
In this task, your instructor will show how to navigate a client-side cursor opened using both OLE
DB and ODBC providers with the SQL Native Client data access provider. Monitor the T-SQL
code created by using SQL Server Profiler.
To execute client-side cursors by using OLE DB and ODBC with the SQL Native Client data
access provider, perform the following steps.
1. Open Visual Studio 2005 Beta 2.
2. Browse to D:\Democode\Section03, and open the MOC2783M3L3.sln solution.
3. In the SNACursors project, open the DataLayer.cs file.
4. Scroll to the definition of the ExecuteCursor method in line 21.
Introduction
The Dataset and DataTableReader classes are parts of the Microsoft ADO.NET generic classes, and
the DataReader class is a part of the ADO.NET SqlClient data access provider.
SqlClient supports only client-side cursors. You should use an SqlCommand object to execute an
SQL query and return the result set back to the calling object in the client application. The client
application can manipulate this buffered result set on a row-by-row basis by using the
SqlDataReader class, or as a set by using the Dataset class. The DataSet class can also be navigated
with a DataTableReader object.
Demonstration Overview
In this demonstration, your instructor will demonstrate how to create a client-side cursor in
ADO.NET and the T-SQL statements that this generates.
4. SQL Server Profiler will display the Trace Properties window. To select the default settings,
click Run, and then start monitoring.
5. Switch to Visual Studio 2005 Beta 2.
6. Browse to the MOC2783M3L3.sln solution.
7. In the ADO.NETCursors project, open the DataLayer.cs file.
8. Scroll down to the definition of the ExecuteDataReader method in line 71.
9. To set a breakpoint on the method definition, press F9.
10. Right-click the ADO.NETCursors project, and click Set as StartUp Project.
11. To run the sample application, press F5.
12. Select SqlDataReader, and then click GO.
13. The control will return to Visual Studio Beta 2. To navigate through the code execution in
debug mode, press F10.
14. After navigating through all the source code on the debug path, switch to SQL Server Profiler.
15. Navigate through all the recorded rows, and notice the T-SQL code that was executed.
16. In SQL Server Profiler, close the running trace. On the File menu, click Close, and in the box,
click Yes.
13. After navigating through all the source code on the debug path, switch to SQL Server Profiler.
14. Browse through all the recorded rows, and notice the T-SQL code that was executed.
15. Close the running trace in SQL Server Profiler; on the File menu, click Close; and click Yes in
the box that appears.
Introduction
Both client-side and server-side cursors are designed to work with data on a row-by-row basis, but
they provide different implementation architectures and are not designed to work together.
You should not use client-side and server-side cursors together because the potential benefits of one
type of cursor could be drastically reduced by using the other cursor type in the same process. You
must choose between these two implementations. Both provide advantages and disadvantages in
specific scenarios.
It is important to note that the data access provider that you choose will determine the feature set
available when implementing client-side cursors.
• Changes in the database will not be visible to the client-side cursors until the changes are
synchronized with the database.
• Synchronizing the changes with the database could create concurrency violations that must be
handled through one of the following recommended practices:
• Including only the primary key columns in the WHERE clause
• Including all columns in the WHERE clause
• Including Unique key columns and the Timestamp columns in the WHERE
clause
• Including Unique key columns and the Modified columns in the WHERE
clause
Memory usage
• The client computer needs to cache large amounts of data and maintain information about the
cursor position. Therefore, the client machine should have enough memory to handle the size
of the entire result set.
Note For more information on handling concurrency issues, see the “Managing Concurrency”
topic in the .NET Data Access Architecture Guide at:
http://msdn.microsoft.com/practices/guidetype/Guides/default.aspx?pull=/library/en-
us/dnbda/html/daag.asp
Next Steps
Introduction
The information in this section supplements the content provided in Session 3.
Improving SQL Server performance
• http://msdn.microsoft.com/library/en-us/dnpag/html/scalenet.asp
“Sequential to Set-Based,” SQL Server Magazine, November 2001
• http://www.windowsitpro.com/Article/ArticleID/22431/22431.html
Client-Side Cursors Versus. Server-Side Cursors
• http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vsentpro/html/veconclientsidecursorsversusserversidecursors.asp
Discussion Questions
1. What was most valuable to you in this session?
2. Based on this session, have you changed your mind about anything?
3. Are you planning to do anything differently on the job based on what you learned in this session?
If so, what?
4. Would you still use cursors in the same ways that you did at the beginning of the session? If not,
how will you use them differently?
5. Do you still think that cursors are an appropriate solution for some scenarios?
Contents
Session Overview 1
Section 1: Introduction to MARS 2
Section 2: Designing Query Strategies for
Multiple Reads 17
Section 3: Designing Query Strategies for
Mixing Reads and Writes in the Same
Connection 25
Section 4: Concurrency Considerations When
Using MARS 37
Next Steps 45
Discussion: Session Summary 46
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, Windows, ActiveX and Visual Studio are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
Session Overview
Versions of Microsoft® SQL Server™ earlier than SQL Server 2005 have a restriction on issuing
multiple requests (for read or write operations) over the same connection. This is a limitation of the
communication protocol used in those versions.
Several data access providers try to simulate the behavior on the client, but this affects application
performance.
In SQL Server 2005, Multiple Active Result Sets (MARS) allows applications to have multiple
result sets open and to interleave reading from them. MARS also makes it possible to execute
stored procedures or INSERT, UPDATE, or DELETE operations while the result sets are open.
This session focuses on when and how MARS can improve application response.
Session Objectives
Explain why MARS is more useful than the result-set execution in SQL Server 2000 is.
Explain when multiple simultaneous reads can be beneficial for an application and the
implications of using this technique.
Describe scenarios in which it might be beneficial to use MARS to combine write and read
operations.
Explain the locking implications of using MARS and how these locks affect other transactions.
Section Overview
In earlier versions of SQL Server, database applications were not able to maintain multiple active
statements on a connection. An application had to process or cancel all the result sets from one
batch before it could execute any other batch on that connection.
In SQL Server 2005, connections can be enabled to support MARS. MARS allows applications to
have more than one active result set per connection, thereby improving application response time
and the end-user experience.
In this section, you will learn how MARS works and compare its use with that of the result-set
execution in SQL Server 2000.
Section Objectives
Explain how Microsoft ActiveX® Data Objects (ADO) manages multiple result sets created from
the same connection.
Explain how SQL Server 2005 processes batches that use MARS.
Identify the client libraries that support MARS, and explain how to enable MARS in each of these
libraries.
Explain how row versioning works.
Describe the implications that using the Snapshot isolation level has on SQL Server resources.
Explain the situations in which MARS is appropriate.
Introduction
In earlier versions of SQL Server, the Microsoft OLE DB provider for SQL Server (SQLOLEDB)
was the only data access provider that enabled applications to use implicit multiple connections.
These multiple connections, which were opened by the data access provider in the context of the
client side, resulted in multiple results executing over the same connection. However, the SqlClient
provider in Microsoft ADO.NET did not allow multiple result sets to execute over the same
connection, so it could not use SQLOLEDB's multiple connection function.
Demonstration Overview
In this demonstration, your instructor will show how SQLOLEDB simulates multiple result sets
created from the same connection and will explain how this internal process might affect
application performance.
Task Overview
In this task, you will review the code needed to use SQLOLEDB to simulate MARS behavior by
opening implicit, non-pooled connections to cause the execution of multiple statements.
To review the code for executing multiple commands with SQLOLEDB, perform the following
steps.
1. Open the Microsoft Visual Studio® 2005 development environment.
2. Browse to D:\Democode\Section01\Demonstration1, and open the Demonstration1.sln solution.
3. Open the Form1.cs file in code view and view the connection string in line 11.
4. Scroll down to the ExecuteADOQuery method in line 32. Notice that only one
ADODB.Connection object is created.
5. In line 44, the connection cn is assigned to the qry1 command.
6. In line 47, the connection cn is assigned to the qry2 command.
7. In line 52, view the WHILE construction, and notice that qry2 is executed for every record in
qry1
Task Overview
This task illustrates how SQLOLEDB simulates MARS behavior by opening implicit, non-pooled
connections to execute multiple statements.
To execute multiple commands with SQLOLEDB, perform the following steps.
1. If you have closed Visual Studio, follow steps 1–4 from Task 1 before continuing.
2. Open SQL Server Profiler.
3. To create a new trace, on the File menu, click New Trace.
4. If prompted to connect to SQL Server, connect to the MIA-SQL\SQLINST1 server. SQL Server
Profiler will show the Trace Properties window.
5. To select the default values, click Run. SQL Server Profiler will start monitoring.
6. Switch to Visual Studio, and press F5 to start running the sample application in Debug mode. An
application named Multiple Results Sets with ADO loads.
7. To start running the sample application, click GO. The mouse cursor will change to an hourglass
icon. SQL Server should start receiving many requests. The two list boxes in the application user
interface (UI) will start to fill up with values. The box on the left displays the EmployeeIDs, and the
box on the right displays the employee first name and last name properties. The execution ends
when the mouse cursor changes to the arrow icon, and the application UI displays the elapsed time.
8. Close the Multiple Results Sets with ADO application.
10. Quickly scroll through the results to view the pattern of events that was recorded. The SQL
Server Profiler window should show a regular pattern like the following:
Open connection (Audit Login event)
Execute Query (SQL:BatchStarting and SQL:BatchCompleted events)
Close connection (Audit Logout event)
11. Close SQL Server Profiler.
12. Close Visual Studio 2005.
Introduction
The implementation of MARS in SQL Server 2005 allows applications to submit more than one
batch of statements in an interleaved fashion over the same connection to SQL Server.
Data access providers that support MARS provide some optimization to enhance connections using
MARS. The optimization involves pooling expensive resources, such as the request-level execution
environment that is created as a copy of the session-level execution environment. To understand
how MARS works, you must understand the features of the session-level and request-level
execution environments.
Additional Information For more information on MARS, read “Multiple Active Result Sets
(MARS) in SQL Server 2005” by Christian Kleinerman at:
(http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/MARSinSQL05.asp)
Introduction
MARS requires changes to the communication protocol used to communicate with SQL Server.
Client applications communicate with the database server by using a data access provider library.
Not all data access provider libraries support MARS. Database developers should know which data
access providers allow applications to create connections that support MARS.
In this topic, you will learn about some of the features that data access libraries implement to
support MARS.
Connection Strings
To configure MARS, use the following code:
ADO.NET 2.0 SqlClient
• multipleActiveResultSets = true | false (connection string setting)
SQL Native Client ODBC
• Set SQLSetConnectAttr with: SQL_COPT_SS_MARS_ENABLED =
SQL_MARS_ENABLED_YES | SQL_MARS_ENABLED_NO
• Mars_Connection = yes | no (connection string setting)
SQL Native Client OLE-DB:
• SSPROP_INIT_MARSCONNECTION = VARIANT_TRUE | VARIANT_FALSE (data
source initialization property)
• MarsConn = true | false (connection string setting)
SQL Native Client ADO:
• Mars_Connection = true | false (connection string setting)
Introduction
Row versioning provides the required infrastructure for optimistic concurrency control. Row
versioning is based on the principle that multiple readers and writers should not block each other,
and that each should work with its own version of the data. However, writers always block writers.
New readers will, depending on the chosen transaction isolation level, be able to see the data that is
being modified by other transactions.
Row versioning forms the basis for a new transaction isolation level called Snapshot isolation, and
for a new setting for the Read Committed isolation level called Read Committed Snapshot.
Row versioning can also be used for creating inserted and deleted tables that are used by triggers or
when using MARS.
In this topic, you will learn how row versioning works and how it is used by MARS.
As a best practice, optimize TempDB to create one file per physical CPU in the server. Store
TempDB files in fast arrays of disks configured as RAID 5 or RAID 10.
For more information on MARS in SQL Server 2005, see the article “Multiple Active Result
Sets (MARS) in SQL Server 2005” on the Microsoft TechNet Web site
(http://www.microsoft.com/technet/prodtechnol/sql/2005/marssql05.mspx).
For information about the overhead and costs of using row-level versioning, see the “Row
Versioning Resource Usage” topic in SQL Server Books Online (ms-
help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/0d4d63f4-7685-44a7-9537-
20fe2f97dfc1.htm).
For more information about optimizing TempDB, see the Knowledge Base article “FIX:
Concurrency Enhancements for the TempDB Database” on the Microsoft Help and Support
Web site (http://support.microsoft.com/default.aspx?scid=kb;en-us;328551).
Introduction
SQL Server provides a new transaction isolation level called Snapshot isolation. The Snapshot
isolation level uses row versioning to allow concurrent read operations over the same data, thereby
avoiding locking mechanisms.
Demonstration Overview
In this demonstration, your instructor will show how the Snapshot isolation level works and how it
affects server resources such as space consumption in TempDB.
Task Overview
In this task, you will set up the environment needed to execute the sample application. You will
create a new table to simulate concurrent read and write operations, set up Microsoft Windows®
Performance Monitor to monitor the relevant performance counters, and modify the
AdventureWorks database to support Snapshot transactions.
To set up the monitoring and execution environment, perform the following steps.
1. Open the Visual Studio 2005 development environment.
2. Browse to D:\Democode\Section01\Demonstration1, and open the Demonstration2.sln file.
3. In the ResetDatabase project, in the Change Scripts folder, open the EnableSnapshotIsolation.sql
file. View the ALTER DATABASE statement.
4. Right-click the EnableSnapshotIsolation.sql file, and then click Run On.
5. If prompted, create a new database reference, and use the MIA-SQL\SQLINST1 server instance
to connect to the AdventureWorks database.
6. In the Create Scripts folder, in the ResetDatabase project, open the CreateTable.sql file. View
the CREATE TABLE statement.
7. Right-click the CreateTable.sql file, and then click Run On.
8. If prompted, create a new database reference, and use the MIA-SQL\SQLINST1 server instance
to connect to the AdventureWorks database.
9. Open Microsoft Windows® Performance Monitor.
10. Press DEL or click the X sign on the toolbar as many times as necessary to delete all the
default running counters (if any).
11. To add new counters, click the plus (+) sign on the toolbar.
12. Add the following counters by selecting the appropriate values from the lists and clicking the
Add button:
Performance Object: MSSQL$SQLINST1:Transactions, Counter: Free Space in TempDB
(KB)
Performance Object: MSSQL$SQLINST1:Transactions, Counter: Snapshot Transactions
Performance Object: MSSQL$SQLINST1:Transactions, Counter: Transactions
Performance Object: MSSQL$SQLINST1:Locks, Counter: Lock Requests/sec,
Instance:_Total
13. To close the Add Counters window, click Close. Start monitoring.
Task Overview
In this task, you will review the code needed to show how the Snapshot Isolation level works.
To review the code, perform the following steps:
1. Return to the Visual Studio 2005 development environment.
2. In the Demonstration2 project, open the Form1.cs file in code view.
3. Scroll down to the definition of the ExecuteWriteOperations method in line 74.
4. Scroll down to the definition of the ExecuteReadOperations method in line 101.
5. Scroll down to the definition of the OpenReadConnection method in line 152.
Task Overview
In this task, you will execute the sample application to show how the Snapshot Isolation level
works.
To execute multiple read and write operations, perform the following steps:
1. Right-click the Demonstration2 project, and then click Set as StartUp Project.
2. To start executing the application in Debug mode, press F5.
3. Arrange the windows so that the sample application UI and Windows Performance Monitor are
both visible.
The Transaction Snapshot Isolation Level window is loaded and will start executing.
Notice how the Last Read field is updated every two seconds.
Notice the current number of rows retrieved by the read operation on the SELECT COUNT(*)
field.
Notice that the Performance Monitor window displays regular lines with constant values.
4. Click Start.
Notice the Number of writes executed field.
Notice the current number of rows retrieved by the read operation on the SELECT COUNT(*)
field.
Notice that the Performance Monitor window displays the increment in the Lock Request/sec
counter value.
5. Click Stop.
6. Click Reset Connection.
Notice that the Performance Monitor window displays constant values again after the Lock
Request/sec counter stabilizes.
Introduction
MARS is a programming model enhancement that allows multiple requests to interleave on a client
computer over the same connection to the server. Although MARS does not support parallel
execution on a client computer, it might yield some performance benefits if used correctly and
when appropriate.
MARS is almost invisible to the application developer because it is only a setting on the connection
string. Be careful, however, when enabling it for certain scenarios.
Discussion Questions
1. What techniques do you use to minimize response time?
2. What techniques do you use to minimize network traffic when reading related data?
3. Do you have ADO code that needs to be migrated to ADO.NET and is missing this functionality?
4. How many connections do you open to SQL Server from each instance of an application? Why?
Section Overview
The MARS infrastructure permits multiple requests to execute in an interleaved fashion over the
same connection. MARS utilizes row-level versioning as an optimistic concurrency control so that
multiple readers will not block each other and writers will not block readers. When reading related
results, MARS improves application response time and the overall performance.
This section focuses on the scenarios in which multiple simultaneous reads can be beneficial for an
application, and on the implications of using this technique.
Section Objectives
Explain how MARS can improve the user experience by reducing response time.
Evaluate the considerations for using MARS to support multiple related results.
Compare the alternatives to MARS for implementing multiple active read operations.
Introduction
Using MARS is beneficial in specific scenarios, such as reading multiple related result sets over the
same connection.
Demonstration Overview
In this demonstration, your instructor will show how MARS can improve application performance
by reading multiple result sets using the same connection and filling user interface controls while
the results arrive, thereby reducing response time.
Task Overview
In this task, you will review the code needed to create a MARS connection.
To review the code, perform the following steps:
1. Open the Visual Studio 2005 development environment.
2. Browse to D:\Democode\Section02\, and open the MARSResponseTime.sln solution.
3. Open the code view for the Form1.cs file.
4. View the connection string in line 17.
5. View the sqlCommandText variable in line 18.
6. View the btGO_Click method in line 58.
Task Overview
In this task, you will set up the monitoring environment by using a dynamic view that returns the
number of physical and logical connections currently being used to connect to the database server.
To monitor MARS, perform the following steps:
1. Open SQL Server Management Studio.
2. If prompted to connect to SQL Server, connect to the MIA-SQL\SQLINST1 server.
3. To open a new query window, press CTRL+N.
4. Type the following query in the query window:
SELECT * FROM SYS.DM_EXEC_CONNECTIONS
WHERE SESSION_ID = ?
Task Overview
In this task, you will execute the sample application, creating five threads, executing a
SqlCommand on each thread, and filling a box from each thread. All five commands use the same
physical connection.
You will use SQL Server Management Studio to monitor the number of physical and logical
connections currently opened to SQL Server.
To execute MARS, perform the following steps:
1. Return to Visual Studio 2005 development environment.
2. To start running the sample application, press F5. The MARS Response Time window is loaded.
3. Click GO. The application displays a message box with the System Process ID. Notice the ID,
and do not close the message box.
4. Return to SQL Server Management Studio.
5. In the query window opened in Task 2, modify the query by replacing the ? symbol with the
number displayed in the message box.
6. To execute the query, press F5.
7. Return to the sample application, and then in the message box, click OK. The application starts
filling the five boxes with names.
8. Return to the sample application, and click Close Connection.
9. Return to SQL Server Management Studio.
10. To execute the query again, press F5.
11. Close the MARS Response Time application.
12. Close SQL Server Management Studio.
Introduction
MARS changes some of the basic assumptions about multiple statements that execute concurrently.
The main benefit of MARS is that you can use the same connection for multiple reads without
having to create new connections.
The MARS implementation in SQL Server 2005 avoids blocking issues by allowing the
interleaving of compatible commands at specific points while some other commands are not
interleaved to avoid conflicts.
This topic focuses on the various points that you should consider for using MARS.
Using the same connection for more than 10 concurrent commands causes overhead.
• If you are using ADO.NET 2.0 SqlClient to connect to the database server, do not use the same
connection for more than 10 commands at the same time, because the application will incur
serious overhead. The MARS implementation keeps a hard-coded pool of up to 10 reusable
logical sessions. After the tenth session, sessions are created and destroyed per request.
Issuing short result sets generated by single SQL statements that read information (SELECT,
FETCH, RECEIVE) provides a better overall performance.
• MARS uses row-level versioning when mixing multiple read operations. This new type of
concurrency management mechanism does not use locks.
• Single SQL statements execute in an implicit and independent transactional context. Therefore,
there is no resource contention on the database server.
Additional Information
For more information on MARS, visit the blog
http://blogs.msdn.com/dataaccess/archive/2005/08/02/446894.aspx.
Introduction
There are many alternatives to MARS for implementing multiple active read requests. However,
none of these alternatives is designed specifically to allow multiple requests over the same
connection. By using these alternatives, applications incur extra overhead.
MARS was designed to allow multiple requests to execute over the same connection. Moreover,
MARS implicitly takes care of the complexity involved in implementing such a solution.
In this discussion, you will compare the alternatives to MARS for implementing multiple active
reads.
Discussion Questions
1. Can server-side cursors replace MARS?
Section Overview
When using MARS, applications can use the same connection to issue multiple requests to the
database server. For example, you can read multiple related sets through a single connection to a
database server. Database developers and administrators must understand what happens when there
is a mix of read and write operations over the same connection.
Because MARS allows mixing read and write operations over the same connection, write
statements can potentially block read statements that are being executed over the same connection.
To take advantage of MARS and minimize blocking between read and write operations, carefully
design the execution order of the read and write statements in queries.
Section Objectives
Explain the valid combinations of read and write operations that you can use in MARS.
Explain how to use MARS to implement some common combinations of read and write
operations.
Explain the guidelines for using MARS to mix read and write operations.
Explain how to perform SQL Server Service Broker operations while reading an active result set.
Compare the alternatives to MARS for combining read and write operations.
Introduction
Any database request can be sent over MARS, including the following:
Requests to retrieve data
Requests to execute stored procedures
Data definition language (DDL) statements
Data manipulation language (DML) statements
However, a database request might be incompatible with the previous requests executed over the
same connection in terms of locking, blocking, and transactional behavior.
To use MARS to send multiple database requests over a connection, you must arrange the requests
in the correct order and combination.
Additional Information
• For more information about how interleaving operations work, read “Multiple Active Result
Sets (MARS) in SQL Server 2005” by Christian Kleinerman
(http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/MARSinSQL05.asp).
• For more information about how MARS executes multiple requests within transactions, read
the next section of this session, “Concurrency Considerations When Using MARS.”
Introduction
Combining read and write operations by using MARS results in a non-deterministic execution,
because interleaving depends on which type of commands are to be executed and on the order in
which the commands are processed by the database server.
Row versioning permits you to ease the requirements for interleaving incompatible commands,
because each read command works with its own copy or snapshot of the rows, thereby reducing
contention and blocking.
In this demonstration, you will learn how to implement some common combinations of read and
write operations by using MARS.
Demonstration Overview
In this demonstration, some common combinations of read and write operations are implemented
by using MARS. The sample application uses the following types of queries:
• Interleaving Update commands with Select commands
• Interleaving Delete commands with Select commands
• Interleaving Insert commands with Select commands
• Interleaving stored procedure execution with Select commands
These all follow the same basic code template, which can be reutilized in other applications.
Task Overview
In this task, you will configure the execution environment. In this task, two new tables will be
created and one of them will be filled with sample data. A new stored procedure also needs to be
created. All of these objects will support the application code, which you will review in the next
task.
To set up the execution environment, perform the following steps:
1. Open the Visual Studio 2005 development environment.
2. Browse to D:\Democode\Section03\Demonstration1, and open the MARSReadAndWrite.sln
solution.
3. In the Create Scripts folder, right-click the CreateTable.sql file, and then click Run On.
4. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
5. Right-click the CreateDoSomethingProc.sql file, and then click Run On.
6. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
7. In Visual Studio Solution Explorer, right-click the Demonstration1 project, and then click Set
as StartUp Project.
Task Overview
In this task, you will review the code needed to call multiple read and write operations with MARS.
A common template for such calls is shown. You can reuse the template in your applications.
To review a pattern to call read and write operations with MARS, perform the following steps:
1. In Visual Studio Solution Explorer, in the Demonstration1 project, open the code view of the file
Form1.cs.
2. Scroll down to the btGO_Click method in line 18.
3. Scroll down to the ExecuteINSERT method in line 51.
4. Scroll down to the ExecuteUPDATE method in line 68.
5. Scroll down to the ExecuteSP method in line 85.
6. Scroll down to the ExecuteDELETE method in line 102.
7. In Visual Studio Solution Explorer, in the Demonstration1 project, open the code view of the file
CallingPattern.cs.
Task Overview
In this task, you will execute the sample application, which uses the previously-reviewed common
template to call multiple read and write operations with MARS. The application will report the
execution time for each statement by changing some configurations. For example, response time
will vary depending on whether or not transactions are enabled.
To execute multiple read and write operations with MARS, perform the following steps:
1. To start executing the sample application, press F5. The Combining Read and Write Operations
Using MARS window is loaded on the screen.
2. Click GO. Notice the elapsed times.
3. Click GO again. Notice the elapsed times again.
4. Select the Transactional check box, and then in the list, select the RepeatableRead value.
5. Click GO again. Notice the elapsed times again.
6. In the list box, select the Serializable value.
7. Click GO again. Notice the elapsed times again.
8. Close the Combining Read and Write Operations Using MARS application.
9. Close the Visual Studio 2005 development environment.
Introduction
Because of the way that MARS is designed and the way that database servers maintain data
consistency, be careful when mixing read and write operations.
Database developers should understand the implications that invalid combinations of operations
might have on both the client and the server. On the client, invalid combinations of operations
might affect the interleaving performed by MARS. Poor usage of MARS can lead to latency and
blocking on the server.
In this topic, you will learn about the guidelines for mixing read and write operations using MARS.
Introduction
Using SQL Server SQL Server Service Broker, you can build reliable, asynchronous, and message-
based database applications.
SQL Server SQL Server Service Broker operations can also be called through a connection that is
configured with MARS. SQL Server Service Broker operations interleave with other compatible
read operations and wait for the completion of incompatible operations. For example, one request
may execute a DDL statement to modify a SQL Server Service Broker queue schema while another
request tries to read from the same queue.
Demonstration Overview
In this demonstration, your instructor will show how to perform SQL Server Service Broker
operations while reading an active result set and executing a stored procedure. Using MARS
eliminates the need for a separate connection to SQL Server to use SQL Server Service Broker.
Task Overview
In this task, you will configure the execution environment. A new stored procedure and all the
infrastructure for SQL Server Service Broker need to be created. All of these objects will support
the application code, which will be reviewed in the next task.
To set up the execution environment, perform the following steps:
1. Open the Visual Studio 2005 development environment.
2. Browse to D:\Democode\Section03\Demonstration2, and open the SSBandMARS.sln file.
3. In the Section03 folder, open the ResetDatabase project.
4. In the Create Scripts folder, right-click the CreateTable.sql file, and then click Run On.
5. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
6. In the Create Scripts folder, open the CreateComplexSP.sql file.
7. Right-click the CreateComplexSP.sql file, and then click Run On.
8. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
9. In the Create Scripts folder, open the CreateSSBInfrastructure.sql file.
10. Right-click the CreateSSBInfrastructure.sql file, and then click Run On.
11. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
12. In Visual Studio Solution Explorer, right-click the Demonstration2 project, and then click Set
as StartUp Project.
Task 2: Reviewing How to Call Read Operations with MARS Using SQL
Server SQL Server Service Broker
Task Overview
In this task, you will review the code needed to call multiple read operations using MARS,
including a SELECT statement, a stored procedure, and a SQL Server Service Broker service.
To call read operations with MARS and use SQL Server Service Broker, perform the following
steps:
1. In Visual Studio Solution Explorer, in the Demonstration2 project, open the code view of the
Program.cs file.
2. Scroll down to the Main method in line number 28. Your instructor will review the code
implemented by the Main method. Scroll down as required to keep pace with the instructor.
3. To execute the sample application, press F5. A console window loads into memory and starts
executing. The following messages is printed on the screen:
“SQL Server Service Broker conversation handler: identifier”
From 1 to 99 of:
“Navigating record: n”
“Sending message to SQL Server Service Broker”
“Retrieving all messages waiting in SQL Server Service Broker queue”
From 0 to 98 of:
Message0: identifier
Introduction
MARS provides a way for client-side logic to execute multiple read and write operations over the
same connection to a database server. MARS is appropriate in some usage scenarios, but in some
others, MARS could signify an overhead or is otherwise not appropriate.
Are there any alternatives?
Discussion Questions
1. Can server-side cursors replace MARS?
2. Do you need to interleave read and write operations in the same connection?
3. How can you interleave read and write operations without using MARS?
Section Overview
Using MARS to issue multiple requests to a database server through a single physical connection
has various implications on the client and the server, especially with regard to locking behavior.
MARS requires various server resources to control concurrent access. For example, MARS
requires:
Memory to maintain multiple request-level sessions.
Locks on tables and databases to control concurrency.
Disk space to support row versioning in the TempDB database.
This section focuses on the locking implications of using MARS and on how these locks affect
other transactions running concurrently on a database server.
Section Objectives
Explain the locking behavior of MARS for the various transaction isolation levels.
Explain how to monitor locks and blocked connections while using MARS.
Explain the guidelines for maximizing concurrency while using MARS.
Introduction
Similar to other database requests, requests made over a MARS-enabled connection need to interact
with the SQL Server Lock Manager to control concurrent access to resources such as tables, rows,
and indexes.
Allocation pages, system tables usage, locking mechanisms, and transaction isolation levels behave
in the same way, regardless of whether the connection uses MARS.
However, because MARS allows multiple requests to be sent over the same connection, SQL
Server needs to prevent requests executing over the same connection from blocking each other.
When using MARS connections, locking behavior will depend on:
The execution of transactional requests.
The type of operations to be executed.
MARS allows only one transaction over a physical MARS executes multiple implicit transactions (one for
connection, regardless of the number active requests. each active request) over a physical connection.
The database server uses regular locking, blocking, and The database server uses regular locking, blocking, and
isolation semantics at the transaction level. isolation semantics at the request level.
Compatible operations are requests that will not block Incompatible operations are requests that will block each
each other when executed concurrently over the same other when executed concurrently over the same MARS
MARS connection. connection.
MARS will interleave multiple requests over the same MARS will not interleave other requests until the
connection. completion of the blocking request.
The database server uses row-level versioning. The database server will request locks on resources from
the SQL Server Lock Manager.
Additional Information For more information about locking, read the chapter “Locking in
the Database Engine” in SQL Server Books Online (ms-
help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/c626d75f-ff62-41bb-9519-
10db3b50bee5.htm).
Introduction
Database administrators should continuously monitor MARS behavior. SQL Server 2005
implements a set of tools, counters, and dynamic views to obtain real-time information about
resource consumption by MARS.
The benefits of monitoring MARS behavior are:
• Early detection of locking and blocking.
• Understanding the access pattern of applications using MARS.
Demonstration Overview
In this demonstration, the sample application will execute different type of queries combining read
and write operations. Your instructor will use SQL Server tools to monitor locks and blocked
connections while using MARS and will show how to detect, in the code, the issue that is producing
the block.
Task Overview
In this task, you will configure the execution and monitoring environments.
To set up the execution environment, perform the following steps.
1. Open SQL Server Management Studio.
2. If prompted to connect to SQL Server, connect to the MIA-SQL\SQLINST1 server.
3. To open the Object Explorer window, press F8.
4. In the Object Explorer window, open the tree shown.
5. Expand the Management folder.
6. Double-click the Activity Monitor node.
7. Open the Visual Studio 2005 development environment.
8. Browse to D:\Democode\Section04\Demonstration1, and open the Demonstration1.sln file.
9. In the Create Scripts folder of the ResetDatabase project, right-click the CreateTable.sql file
and then click Run On.
10. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
11. Right-click the CreateDoSomethingProc.sql file, and then click Run On.
12. If prompted to create a database reference, use the MIA-SQL\SQLINST1 server instance to
create a new database reference, and connect to the AdventureWorks database.
Task Overview
In this task, you will use the SQL Server Management Studio Activity Monitor tool to monitor the
type of locks being used and the T-SQL command that is causing the locks to occur.
To monitor locking and blocking in SQL Server 2005, perform the following steps.
1. To start running the sample application, press F5.
2. The Combining Read and Write Operations Using MARS window is loaded on the screen.
3. Click GO. (Do not wait for any results yet. Continue with the next step.)
4. Return to the Activity Monitor window and ensure that the Process Info option is selected in the
Select a Page pane in the upper left.
5. To refresh the window, press F5.
Scroll to the right and view the other columns that were not visible earlier.
6. To exercise the window, return to the sample application, click GO, quickly return to the
Activity Monitor window, and press F5. Continue doing this until you see a value other than 0 on
The columns can be reordered for easier viewing by clicking the column header and dragging it to
the desired position on the grid.
Notice the value in the Blocked by column for all the rows that have the Database column set to
AdventureWorks and that have the Application column set to .NET SqlClient Data Provider.
Notice the Process ID shown in the Blocked by column; it will be used in the subsequent steps.
7. In the Select a page pane in the upper left, select the Locks by Process option.
8. In the Selected process box, select the process ID that appeared in the Blocked by column in the
Process Info view.
Notice the value in the Object ID column.
9. In the Select a Page pane in the upper left, select the Locks by Object option.
10. In the Selected object box, click AdventureWorks.Table2.
11. Click the Description column header to order by this column.
12. Scroll down if necessary to see all the values.
Introduction
Concurrency defines the amount of parallelism accepted by an application. The larger the number
of concurrent actions occurring in an application, the better the response time for providing the
results to the caller application or end-user.
Concurrency is not an automatic feature of using MARS, and you should follow guidelines to
maximize concurrency.
These guidelines provide information about how to implement efficient MARS-enabled
applications.
The benefits of following these guidelines include:
Improved application performance.
Better execution of applications running concurrently on a database server.
Better resource management by the database server.
When executing requests under a MARS-enabled connection, consider the following two potential
bottlenecks:
Contention at the connection level when requests are not able to execute in an interleaved fashion.
Contention at the database level because of the locks held by the operations that are being
executed.
In this topic, you will learn about the various guidelines for maximizing concurrency while using
MARS.
Next Steps
Introduction
The information in this section supplements the content provided in Session 4.
“Multiple Active Result Sets (MARS) in SQL Server 2005” by Christian Kleinerman
• http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/MARSinSQL05.asp
“Locking in the Database Engine” in SQL Server Books Online
• ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/c626d75f-ff62-41bb-9519-
10db3b50bee5.htm
“Tuning and Optimizing Queries using Microsoft SQL Server 2005.” This course is a part of the
Microsoft official curriculum for SQL Server 2005. This course covers the server-side aspects of
using MARS connections.
Discussion Questions
1. What was most valuable to you in this session?
2. Based on what you learned in this session, have you changed your mind about anything?
Contents
Session Overview 3
Section 1: Why Caching Is Important 4
Section 2: Data and Query Caching in SQL
Server 2005 15
Section 3: Using Caching Technologies
Outside of SQL Server 25
Section 4: Custom Caching Techniques 36
Discussion: Session Summary 48
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, Windows, and Visual Studio are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
Session Overview
Introduction
Caching is the process of persisting a copy of data and objects locally. Caching allows for faster
access to the required data and reduces the overhead associated with frequent retrieval of data from
the original data source. Object caching reduces the overhead of object creation and destruction.
Developers can improve the performance and scalability of database applications by applying
caching techniques in various layers of the application. Different caching techniques can be applied
to the database, middle tier, and client layers of an application.
Developers must understand the appropriate situations in which to use the various caching
techniques. By designing an appropriate caching strategy, developers can quickly build high-quality
applications that require the use of these techniques.
Microsoft® SQL Server™ 2005 provides various internal caching mechanisms. Although these
caching mechanisms are automatically managed by SQL Server, developers can optimize their use
by making them interact with the database management system in appropriate ways.
This session focuses on optimizing system resources by caching data and objects in the appropriate
layers. Correctly optimizing applications by implementing caching will result in reduced resource
utilization and therefore better performance of the system. Resources such as memory, physical I/O,
and network bandwidth can also be optimized by using caching methodologies.
Session Objectives
Explain why caching is important.
Explain the advantages of using the data and query caching that is automatically performed by
SQL Server 2005.
Explain how caching data outside of SQL Server works and how to manage conflicts that might
arise.
Explain the various ways to cache frequently used data, objects, and results in the appropriate tier
to improve performance.
Section Overview
The various types of caching technologies that can be used include connection pooling, parameter
set caching, and in-memory caching of data or business objects.
Developers must understand that there are certain disadvantages in any caching scenario. In most
situations, developers worry about not caching enough data to ensure application performance.
However, excessive data can also be cached, and in such cases, the amount of resource utilization
involved in building the cache can outweigh its benefits to the application.
The appropriate caching technique to be used depends on the application tier that is being
considered. For example, denormalization of data can be used in the database, but it cannot be used
in the application tier, where an in-memory cache might be more effective. When designing a
solution, developers must consider the scenario in which caching will occur.
In this section, you will learn about the performance implications of different types of caching. You
will also learn how to determine the appropriate amount of data to cache.
Section Objectives
Describe how caching affects application performance.
Explain the performance implications of using various types of caching.
Describe how you can help application developers determine the amount of data to cache.
Explain the guidelines for using caching techniques in each layer of a database application.
Introduction
Data caching plays an important role in improving performance for most database applications. In
addition, caching frequently used objects might also produce important performance benefits. This
fact is often ignored by database developers.
You can use various types of caching techniques in the application tier, such as:
Connection pooling, which caches database connections in a shared pool, avoiding expensive and
repetitive allocation and deallocation of connection resources.
Stored procedure parameter set caching, where commonly used parameters for stored procedures
are held in the application memory instead of being reinitialized for each call.
In-memory caching of DataSet objects to hold data that does not change frequently.
Demonstration Overview
In this demonstration, the instructor will run an application containing prebuilt tests that show the
results of data access with and without each of these caching mechanisms. The demonstration will
explain the benefit of using these caching techniques to improve application performance.
Task Overview
In this task, your instructor will demonstrate the benefits of database connection pooling by running
tests with and without connection pooling enabled.
To use database connection pooling, perform the following steps:
Task Overview
In this task, your instructor will demonstrate the benefits of in-memory data caching by running a
test that retrieves data from the database and a test that retrieves data directly from an in-memory
cache after the first request.
To use in-memory data caching, perform the following steps:
Task Overview
In this task, your instructor will shows the benefits of caching parameter sets by using the parameter
caching mechanism of the DAAB. Two tests will be run: a test in which the cache is bypassed and a
test in which the cache is used.
To use stored procedure parameter caching, perform the following steps:
Introduction
Most developers are familiar with caching data in the application tier to reduce database roundtrips.
In some cases, caching business objects can also be beneficial. Data and object caching enable
reduction of resource bottlenecks, thereby improving application performance and scalability.
However, it is important to remember that when designing a caching mechanism, cached data or
objects can change. In these cases, appropriate action must be taken to ensure that data consumers
are also updated so that they do not receive the wrong information.
In this topic, you will learn about the benefits of data and object caching.
Because you must not cache excessive data, determining the appropriate level of caching is an
important task to perform during the design and testing phases of the database application.
You should cache only data that has a high probability of being reused or data that requires
significant system resources to be retrieved.
Be aware of the fact that caching excessive data might hurt response time, decreasing the perceived
performance from the user point of view.
Introduction
When assisting application developers in designing caching strategies, it is important that database
developers thoroughly understand the problem domain. Application developers will often be
tempted to cache more data than necessary and might not fully consider the following issues:
Security of cached data
Resource utilization for holding the data in memory
Changes requiring conflict resolution when the data is modified
Database developers should be able to work with application developers to help them to understand
these issues.
Discussion Questions
1. How do you help application developers determine how much data to cache?
5. How can an application determine whether cached data has changed since the last time it
was fetched?
Introduction
Database applications are generally split into layers to facilitate the encapsulation of the logic and
overall application scalability. Various caching techniques can be used at each layer to maximize
the performance of that layer’s functionality and overall application performance. Techniques that
are applicable to any given layer might not be applicable to another layer. For example, caching of
business objects might not apply outside the business layer and maintaining cached data by
serializing to disk might not be a good idea outside the user interface layer. Developers must have
adequate knowledge of the caching techniques to determine the appropriate caching option for each
layer and also to determine why those techniques can be useful in certain scenarios.
Database Server
Guideline Reason
Read only as much data as necessary when Reading unnecessary data into memory will force other
querying tables. important data out of the buffer cache.
Monitor buffer cache usage by using the Page This counter shows the average life span of a page in the
Life Expectancy performance counter. buffer cache. The higher this number, the longer the data
will stay in memory, and the fewer physical I/O
operations will be necessary to retrieve the data.
Avoid recompilation of stored procedures. When stored procedures are compiled, the query plan is
put into a cache called the procedure cache.
Recompilation invalidates this cache and can be
expensive.
Denormalize data for queries involving If you denormalize the data, especially for summary
complex joins or aggregations. reports, fewer data pages will have to be read to satisfy
the query. If fewer pages are read, fewer physical I/O
operations will occur, resulting in better performance.
Do not denormalize data that changes often. If the data changes often, updating the denormalized table
can make data modifications expensive. Queries do not
return synchronized data when the denormalized table
does not contain data that is consistent with the
normalized version.
Application Layer
Guideline Reason
Use Microsoft ADO.NET connection pooling ADO.NET connection pooling, which is enabled by
to reduce the cost of opening and closing default, reuses database connections instead of opening
database connections. and closing these connections for each database request.
This can significantly improve efficiency. Note that only
connections with the same parameters (including security
context) can be pooled.
Avoid caching sensitive data in shared Data in shared memory areas might be accessed by
memory. consumers who do not have appropriate credentials. You
must consider this security risk when designing a caching
strategy.
Be careful when caching data that can be If another process updates data that is cached, the data in
updated in the database by other applications the cache will no longer be valid. You might need to
or processes. implement custom conflict resolution logic, which can be
complex and in itself cause performance degradation.
Create a business object cache to reduce Some business objects are very expensive to create and
resource utilization. destroy. If these objects can be reused or shared, this cost
can be decreased.
Web Services
Guideline Reason
Cache intermediate results to reduce calls to Reducing the workload of the Web service will result in
the application tier. faster response. You can cache partial results to reduce
calls to the application tier.
Be aware of data that can change between If data changes in the database or application level, the
requests. Web service’s cache will be invalid.
Front-End Applications
Guideline Reason
Cache data in-memory for same-session reuse, Data that can be read by a client in many different
such as reformatting or sorting. formats does not need to be re-retrieved from the
application tier. Caching that data will reduce network
traffic and conserve server resources.
Cache dynamic user interface components in Interface components, which are often stored in the
memory to save application and database database, can be cached in memory instead of being re-
roundtrips. retrieved each time the user selects a new option.
In Microsoft Windows® or on a Web server Objects and interface components can be cached to disk
hosting ASP.NET applications, applications so that when the application starts, it will not need to
can cache objects to disk if they are expensive extract all of the data from the database.
to create and their data does not change often.
Use timestamps or checksums to update When data is cached to disk, the application can request
objects cached to disk when the application changes from the database based on a timestamp or
starts. checksum, instead of re-retrieving the entire set of data.
Important The most important consideration when working with a caching scheme is to
determine what to do in the case of outdated data. If you do not consider how to update the
cache when the data changes, applications might return inconsistent and inaccurate results.
Although performance is important, it is more important that applications deliver correct
information to users. By carefully balancing caching techniques in different tiers of an
application, developers can improve performance, thereby creating a positive user experience.
Introduction
SQL Server 2005 uses complex data caching mechanisms to deliver the best performance possible.
Although SQL Server can automatically manage the cache, database developers can write code that
will help maximize the performance of the server. It is therefore important for database developers
to understand how the memory pools of SQL Server operate and to learn to work with the internal
caching mechanisms of SQL Server.
In this section, you will learn about the advantages of using the data caching and query caching that
are automatically performed by SQL Server 2005.
Section Objectives
Explain how data caching works in SQL Server 2005.
Explain how SQL Server 2005 caches queries and objects.
Explain the guidelines for maximizing cache utilization.
Explain how to monitor cache utilization.
Introduction
SQL Server manages a single piece of data in multiple places at any point in time. Therefore,
multiple copies of the data item must be synchronized to avoid data consistency problems. SQL
Server manages this synchronization internally, but the synchronization can be complex. Database
developers must understand how these processes work so that applications can be written to make
the best use of the caching mechanisms that SQL Server provides.
Discussion Questions
1. How many copies of data does SQL Server keep at any point in time?
3. Does the number of copies change when Multiple Active Result Sets (MARS) are used?
5. Is there any difference in the way SQL Server caches transaction log records?
Introduction
The main cache memory pools of SQL Server 2005 are the procedure cache and the buffer cache.
Both caches support developer interaction to some extent; developers can view the contents of the
caches and also clear them. Database developers must understand how these memory pools work
and know how to control them when tracking down performance problems.
Demonstration Overview
In this demonstration, the instructor will explain how SQL Server 2005 caches queries and objects.
Task Overview
In this task, your instructor will use dynamic management views to display the contents of the
procedure cache.
To display the contents of the procedure cache and then clear the cache, perform the following
steps:
Task 2: Displaying the Contents of the Buffer Cache and Then Clearing
the Cache
Task Overview
In this task, your instructor will use Dynamic Management Views to show the contents of the buffer
cache.
To display the contents of the buffer cache and then clear the cache, perform the following steps:
Introduction
Although SQL Server manages its cache resources automatically, there are many ways in which a
database developer can help maximize their usage. By following specific guidelines, database
developers can ensure that:
Cached query plans will be reused.
Cached data will stay in the buffer pool for as long as possible.
The most important benefits of SQL Server cache reuse are that query plan recompiles are avoided
and large amounts of data are not read unnecessarily. Reading excessive data might invalidate other
parts of the buffer cache. By carefully monitoring and working to maximize cache reuse,
developers can create applications that perform well and are scalable.
Read only rows that need to be processed. When reading more rows than required, some
unnecessary rows might be read from other data pages.
Reading excessive data pages into memory will force
other data out of the buffer cache.
Avoid reading too many columns This guideline specifically applies to large, variable-
unnecessarily, especially in shared stored width column types such as VARCHAR and TEXT.
procedures and views. The data for these columns might reside on data pages
that are separate from the rest of the data. Unnecessarily
reading this data will force SQL Server to load those
data pages into memory.
Avoid reading large amounts of binary large The SQL Server buffer pool is shared among all the data
object (BLOB) data in SQL Server instances at any point in time. Therefore, when large amounts of
that host online transaction processing (OLTP) data are read, the cache from an OLTP application can
applications. quickly be pushed out of memory.
Avoid using Extensible Markup Language Reading a single scalar value from an XML column can
(XML) in situations where relational data is be expensive because the entire column will have to be
more appropriate. loaded into memory first. Shredding XML into
relational structures eliminates this overhead.
Avoid using nonparameterized, ad hoc, and Nonparameterized SQL might not be able to participate
dynamic SQL. in query plan reuse and might produce many new query
plans, each of which will require memory.
Monitor for and try to avoid procedure Procedure recompilation can be expensive and will
recompilation. block the execution of the procedure.
Install multiple SQL Server instances to adjust Multiple instances can be used to isolate memory, keep
shared resources. certain processes from consuming more memory than
they should, and ensure that those processes will not
destroy the cache reutilization potential of the data that
is already in the cache.
Introduction
SQL Server 2005 provides many tools (such as SQL Server Profiler and Performance Monitor) that
database developers can use to determine how the Database Engine is working.
Using Performance Monitor, developers can view the state of the various counters that are exposed
by SQL Server, including the counters that help to determine the state of the SQL Server memory
caches.
Cache events are also exposed in SQL Server Profiler. You can run traces to determine when
cached objects are reused and when objects are added or removed from the cache.
Demonstration Overview
In this demonstration, the instructor will explain the use of two SQL Server 2005 tools: SQL Server
Profiler and Performance Monitor.
Task Overview
In this task, your instructor will use SQL Server Profiler to show how the procedure cache is used
by the SQL Server query optimizer.
To use SQL Server Profiler to show query cache utilization, perform the following steps:
Task Overview
In this task, your instructor will use Performance Monitor to show buffer cache activity.
To use Performance Monitor to monitor cache utilization, perform the following steps:
1. In SQL Server Management Studio, select lines 47 and 48 of the script, and then press F5.
2. Open Performance Monitor.
3. Right-click the graph area, and then click Add Counters.
4. In the Performance object list, select MSSQL$SQLINST1:Buffer Manager.
5. In the Select counters from list, select Buffer cache hit ratio, and then click the Add
6. Select the counter Database pages, and then click the Add button.
7. Click Close.
8. Show the class the initial state of the performance counters in the Performance Monitor
graph.
9. Switch back to SQL Server Management Studio.
10. Select lines 51 and 52 of the script, and then press F5.
11. Switch back to Performance Monitor, and show the students the new state of the
performance counters in the graph.
12. Close Performance Monitor.
Task Overview
In this task, your instructor will use Dynamic Management Views to show the memory consumed
by cached plans.
To use Dynamic Management Views to show memory used by cached query plans, perform the
following step:
In SQL Server Management Studio, select lines 57 and 73 of the script, and then press F5.
Section Overview
Although SQL Server has various internal caches that can provide higher performance for data
retrieval, other types of external caches should also be considered to improve the overall application
performance. For example:
The DAAB provides parameter set caching.
The Caching Application Block provides object caching.
Using query notifications, which is a new feature in ADO.NET, the application can easily detect
data changes that occur in the database.
In this section, you will learn how to cache data outside SQL Server and how to manage the data
modification conflicts that might occur.
Section Objectives
Explain how ADO.NET caching works.
Explain how the DAAB caches objects.
Explain how the Caching Application Block works.
Explain the process of query notification.
Explain how conflicts can occur when using cached data is used.
Introduction
ADO.NET is a cache-friendly database connection library that is designed for data reuse and
database disconnection. ADO.NET uses pooling technology to avoid repeatedly creating and
disposing of database connections during times when the database application is busy.
The ADO.NET DataSet class is the key to the ADO.NET disconnected architecture. DataSet
objects retrieve the requested data from the database by using an intermediate DataAdapter object
and are then disconnected from the database. DataSets do not require an active database connection
to maintain a set of data in memory. Database connections are opened only to update data that
changes in the DataSet.
Demonstration Overview
In this demonstration, the instructor will illustrate how ADO.NET provides reuse and disconnection
functionality while working with database connections. This demonstration also illustrates the use of
pooling technology to avoid having to continually create and dispose of database connections in busy
database application scenarios.
This demonstration also demonstrates that the ADO.NET DataSet class is the key to the disconnected
architecture.
Task Overview
In this task, your instructor will change the ADO.NET connection string to configure the
connection pool.
To configure ADO.NET connection pooling, perform the following steps:
Task Overview
In this task, your instructor will show that the ADO.NET DataSet object is completely
disconnected from the database and that it is a data cache.
To use ADO.NET DataSets to cache data, perform the following steps:
Introduction
The DAAB can cache stored procedure parameter sets. The application block can query the
database and determine the parameters for a stored procedure. The application block then builds a
set of parameters that are cached at the application layer and are reusable for subsequent requests.
This caching improves the performance of the system, because determining the parameters for a
stored procedure and creating the set of parameter objects can be resource intensive.
Demonstration Overview
In this demonstration, your instructor will illustrate the capability of the DAAB to cache stored
procedure parameter sets. The application block can interrogate the database and determine the
parameters for a stored procedure and then build a set of parameters that are cached at the application
tier and are reusable for subsequent requests. This caching can be a huge performance improvement,
because determining the parameters for a stored procedure and creating the set of parameter objects
can be expensive.
Task Overview
In this task, your instructor will demonstrate how the DAAB can be used to cache parameter sets.
To use the DAAB to cache parameter sets, perform the following steps:
Introduction
The Caching Application Block provides a standard solution for applications that require object
caching capabilities. Objects are cached by using a key-value pair with a string as the key.
Any type of object can be cached by using the application block, but it is best to cache objects that
do not change often. The Caching Application Block passes back a reference to the cached object
instead of creating a deep copy. Therefore, multiple threads can hold a reference to the same cached
object at the same time.
Consider a situation in which one of the threads changes the object. The changes will be visible to
every thread, but any of the threads might be relying on the previous value of the object. Such
situations are difficult to resolve in shared cache situations, and so it is better to avoid caching
objects that change often.
Demonstration Overview
In this demonstration, your instructor will illustrate a standard solution that the Caching Application
Block provides for applications that require object caching capabilities. The Caching Application
Block passes back a reference to the cached object, rather than a deep copy. Many threads can
therefore hold a reference to the same cached object at the same time. If one of these threads changes
the object, the changes will be visible to every thread, but one of the threads might rely on the
previous value of the object. Scenarios such as these are difficult to resolve in shared cache situations,
and it is better to avoid them if possible.
Task Overview
In this task, your instructor will demonstrate how to use the Caching Application Block to cache
objects.
To use the Caching Application Block to cache objects, perform the following steps:
Discussion Questions
1. Do you need to repeatedly access static data or data that rarely changes?
4. Do you think that using a standard application block could provide any benefits? Why?
Introduction
The ADO.NET DataSet class is an ideal data caching container. It requires no active connection to
the database and operates in a fully disconnected mode. However, because the DataSet class is
disconnected, it is not aware of the changes that are made to the underlying data in the database.
Query notification is a new feature in ADO.NET 2.0 that can be used to solve this problem. This
feature uses a SQL Server Service Broker queue to wait for changes to data. When data is changed,
an event is immediately raised and the application is notified about the situation. The application
will have to be programmed to resolve the conflict. It is easier to detect data changes by using query
notifications than by using other techniques, such as continually polling the database for changes or
implementing custom triggers. Using query notifications requires minimal programming by the
developer and less work by the system, because this feature is designed to conserve system
resources.
Discussion Questions
1. Do you think your applications could benefit from the query notification feature?
2. Would you expect a reduction in network traffic by using this feature or an increase?
3. How do you plan to deal with query notifications that represent changes to data being
edited in the client application?
Introduction
When caching any kind of data, one of the main considerations is what to do when the data changes
outside the cache. It is important to consider the following:
How will the cache be notified about the changes to the data?
How will the cache retrieve the new data?
Should other levels of the application, or users, be notified about the changes?
When designing caching strategies, developers must keep in mind that the underlying data might
change at some point in time. This change can cause conflicts if the data in the cache is not the
same as the data in the database. Based on your specific business requirements, appropriate action
to resolve these conflicts must be initiated. Resolution can mean updating the cache, notifying the
user that there is a change, or in some cases, taking no action.
Demonstration Overview
In this demonstration, your instructor will illustrate that when caching any kind of data, the main
consideration is what to do when the data changes outside the cache. It is important to consider how
the cache will be notified that the data has changed, how the cache will retrieve the new data, and
whether other levels of the application, or users, should be notified of the change.
Task Overview
In this task, your instructor will show how to identify cached-data conflicts by using query
notifications.
To identify cached-data conflicts, perform the following steps:
1. In Solution Explorer, right-click the MOC2783M5L3_1 project, and then click Set as
StartUp Project.
2. To start the application, press F5.
3. Click the Start Test button.
4. Open SQL Server Management Studio.
5. Browse to and open the MOC2783M5L3.sql file.
6. Run the script.
7. Switch back to the running application, and view the message box.
Discussion Questions
1. How do you plan to detect conflicts?
2. If changes are applied to columns other than the column you edited in the client
application, would you consider this a conflict?
3. If the same column is edited on the server and the client, would you consider this a
conflict?
Introduction
You can use different caching techniques in various layers. In the data layer, denormalization can
improve the performance of complex queries. In the user interface layer, data can be cached in
memory or serialized to disk to reduce network and database traffic when requesting dynamic
options.
Each of these caching techniques can help in certain scenarios. However, database developers must
ensure that cached data is consistent with source data. Caching mechanisms should be clearly
defined and implemented during the initial development process.
This section focuses on the various ways to cache frequently used data and objects in the
appropriate level to improve overall application performance.
Section Objectives
Identify alternative techniques for caching data and objects, and describe scenarios in which each
technique is appropriate.
Explain the best practices for using denormalization to cache frequently used data.
Explain the best practices for managing dynamic user interfaces efficiently by caching data in the
client.
Explain the best practices for serializing objects to improve the performance of client applications.
Explain how to use custom caching strategies on the server and the client.
Introduction
Caching is a broad topic and depends on the context of specific development challenges. There are
no concrete answers to the questions of database developers about caching, but various general
guidelines can be followed to maximize the return on investment and reduce the time taken to build
caching solutions.
Apart from the system and standard caching techniques, database developers can use alternative
caching techniques, such as denormalization or building a distributed hierarchical cache structure.
Database developers should have a good understanding of the various types of caching techniques
so that they can choose the appropriate technique for each scenario.
Discussion Questions
1. Why should you consider data caching?
2. Are there ways to cache data and objects other than what has been discussed in this course?
3. Is the default system-supplied caching technique the best option in all scenarios?
4. Have you ever denormalized data to improve performance? If so, to which level?
5. Do you keep application strings and resources cached locally in the client application?
Introduction
Denormalizing (or flattening) database tables can significantly improve the performance of large,
complex queries—especially those with many joins or complex aggregations. This technique can be
useful when queries need to read many data pages to produce a few pages of output. For example,
denormalization can be used in reporting systems in which data does not need to be updated in real
time.
Mixed OLTP and Decision Support systems are a common environment for denormalization
techniques. However, when using denormalization, developers must consider the anomalies that
might occur during data updates. The developer must ensure that when data changes in the source
tables, the denormalized tables return data that is consistent with the change. Failure to do so can
result in situations such as applications returning summary data on some screens that does not
properly correspond to the detailed data on other screens.
Scenario
When you develop a complex reporting application, queries commonly use many tables and require
a variety of aggregations. In some situations, you cannot improve the performance of the
application to fulfill the user requirements even after creating many indexes and fine-tuning the
queries. In such cases, denormalization might improve the performance of the application to fulfill
the user requirements.
After you have tried all of the standard reporting techniques—including hardware tuning, index
tuning, and query tuning—this scenario becomes applicable. Because this is a reporting application,
it can be assumed that the data does not change often and that the old data in the cache will not be a
problem. Remember that denormalized tables should be used in addition to normalized tables, not
instead of them. Normalized tables should still be used as the source of the data for the
denormalized tables to guarantee data integrity and correctness. In this situation, because the data
will not change, it might be safe to assume that the cost of updating the denormalized tables will not
You can denormalize complex queries by using a table that will be updated during a data load
process or by using an indexed view. This reduces the work when returning the data, thereby
improving the performance of the application and the query time.
Best Practices
Before considering denormalization, you should try other techniques such as index tuning,
partitioning, and client-side caching. You must also ensure proper configuration of the hardware.
Use SQL Server Profiler or SET STATISTICS IO to determine the number of pages that
SQL Server reads for a given query.
Use denormalization when data does not need to be updated often. When data is updated, it will
have to be maintained in both the source tables and the denormalized tables, which might result in
slow inserts and updates.
Introduction
Some applications use the database as a store for dynamic user interface elements, returning to the
database repeatedly as users navigate between various options.
Scenario
Consider an application that organizes products by category. Whenever a user selects a valid
category from a list, the application sends a request to the database and displays the appropriate
products in another list. In such situations, user interface elements can be cached to reduce database
sever and network traffic and increase the responsiveness of the user interfaces.
In this example, if a user selects a product category, the products in that category can be cached in
memory so that if the user reselects the same category later, a database query will be unnecessary.
Although this seems like a small improvement when you consider a single user or session, the
amount of network and database server resources that are saved can be large when you consider a
large number of concurrent users.
Developers should determine whether changes to the data must be reflected immediately in the
user interface.
• If options need to be updated in real time as they change in the database, caching is not
appropriate.
• If options must be validated only at application startup time and retrieving the options is not
expensive, an in-memory caching scheme might be appropriate.
• If retrieval of options is expensive, serialization of options to a local disk might be a good
option. This is covered in detail in the next section.
• Checksums or timestamps can be used to query the database for updates as they occur.
Elements can be cached in memory by using hash tables or other associative container objects,
using parent options as keys.
Introduction
Although in-memory caching of user interface elements can improve the performance of the
system, for some applications, an in-memory cache might not provide sufficient benefits. When the
number of user interface elements is large or when those interface elements are complex,
serialization and local on-disk caching might be appropriate so that the client does not have to re-
create the objects when needed.
To determine whether serialization techniques should be applied to an application, developers
should consider factors such as the amount of network traffic needed to retrieve a full set of user
interface elements and the amount of time required to create objects for the user interface elements.
These factors should be balanced against the amount of client-side disk space that local caching will
consume and the amount of time required for deserializing objects from disk.
The on-disk caching technique should update the data. Generally, this type of update will occur
when the application starts and will involve a check against a checksum or timestamp column in the
database. If these are found to be different, the cached objects can be selectively updated on the
client.
The common language runtime (CLR) user-defined type feature in SQL Server 2005 can be used as
a serialized object store, but this might not be a good way of implementing this caching technique
because it might incur performance penalties when making network requests to the database server.
Other performance problems might occur due to increasing database server resource contention.
Scenario
When working with an application that has an especially dynamic user interface, you might find
that many user interface elements are stored in the database—for example, strings that describe
options or that are used to populate drop-down menus. A good example of this would be an
application that deals with geographical information. The user interface might have a drop-down
menu that enables the user to select a country, which upon selection populates a drop-down menu
for states/provinces within the selected country. Upon selecting an area from the second drop-down
list, the user might be able to select a city from a third drop-down list, and so on.
When testing the application, you might discover that when the application requests these values
every time they are required, the performance of the system is adversely impacted. Instead of
querying the database each time, strings and other client resources can be locally serialized and
stored on disk. This will allow the application to start without querying the database and causing a
performance problem.
Introduction
This demonstration will show some of the benefits of the caching techniques discussed in this
section. The demonstration will show denormalization by using an indexed view, in-memory
caching of user interface elements, and on-disk caching of user interface elements.
Each of these methods can be used in various scenarios to improve application performance and
scalability. Indexed views are useful in database scenarios for decreasing the cost of complex
queries. User interface elements can be cached in memory on the client to reduce database
roundtrips. On-disk caching can be useful in situations in which elements are expensive to create
and do not need to be completely refreshed every time the application is started.
Demonstration Overview
In this demonstration, your instructor will show some of the benefits of the caching techniques
discussed in this section. The demonstration will illustrate denormalization by using an indexed view,
in-memory caching of user interface elements, and on-disk caching of user interface elements.
Task Overview
In this task, your instructor will show how to create an indexed view based on a query and that
querying the view instead of the source data will provide a much faster response.
To use an indexed view to denormalize data, perform the following steps:
Task Overview
In this task, your instructor will show that by caching user interface elements in memory instead of
retrieving them dynamically from the database, user interface response time can be improved.
To cache user interface elements in memory, perform the following steps:
Task Overview
In this task, your instructor will show that by caching serialized user interface elements to disk,
applications that make use of caching can more quickly start and respond to user requests.
To cache serialized user interface elements to disk, perform the following steps:
Introduction
This session focused on optimizing system resources by caching data and objects in the appropriate
layers. You learned that correctly optimizing applications by implementing caching results in reduced
resource utilization and consequently better system performance. You also learned that resources such
as memory, physical I/O, and network bandwidth can also be optimized by using caching
methodologies.
Discussion Questions
1. What was most valuable to you in this session?
2. Based on this session, have you changed any of your previous ideas regarding data
caching?
3. Are you planning to do anything differently on the job based on what you learned in this
session? If so, what?
Contents
Session Overview 1
Section 1: Identifying the Need to Scale 2
Section 2: Scaling Database Applications to
Avoid Concurrency Contention 13
14
Section 3: Scaling SQL Server Database
Systems 24
Section 4: Scaling Database Applications
by Using Service-Oriented Architecture 40
Section 5: Improving Availability and
Scalability by Scaling Out Front-End
Systems 53
Discussion: Session Summary 62
Clinic Evaluation 63
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory,
regarding these manufacturers or the use of the products with any Microsoft technologies. The
inclusion of a manufacturer or product does not imply endorsement of Microsoft of the
manufacturer or product. Links are provided to third party sites. Such sites are not under the
control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link
contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for
webcasting or any other form of transmission received from any linked site. Microsoft is providing
these links to you only as a convenience, and the inclusion of any link does not imply endorsement
of Microsoft of the site or the products contained therein.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, Windows, and Visual Studio are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
Session Overview
Session Overview
The latest software development tools, together with readily available, proven patterns and best
practices, enable you to easily create and develop complex software. Because these development
tools deal with many of the low-level details, software developers can focus on writing the code
necessary to implement the business logic. Although these tools make software development easier,
software complexity continues to increase, primarily because of increasing user expectations.
All applications have functional requirements that define what the database application should do,
as well as nonfunctional requirements that define service-level expectations, such as application
response time, security needs, and communication protocol restrictions. To meet these
requirements, it is important to plan for scalability in your database applications. Scalability is the
measure of how the software reacts to the load placed on it as the amount of data and the number of
user connections and transactions increase.
This session focuses on how to assess scalability needs and design the best architecture to scale
your system to meet the needs and expectations of users.
Session Objectives
Identify when to scale database applications and what layer to scale.
Select the appropriate technology to avoid concurrency problems and improve application
performance.
Evaluate whether scaling out or scaling up is appropriate for the scalability requirements of your
database system.
Explain how to improve middle-tier processing by using multiple instances of Web services and
object pooling.
Section Overview
Scaling is the process of efficiently supporting system growth. Identifying the need to scale
essentially means adjusting expectations to newer performance objectives.
To identify when to scale database applications, the development team must regularly test and
monitor thresholds. Thresholds are constraints based on the limitations of the current hardware and
software configuration.
The process of capacity planning should be complemented with careful analysis of business
requirements, expected business growth, and solution usage patterns. These are some of the factors
that can aid you in predicting the future load on an application.
This section focuses on identifying when to scale database applications and how to design a
scalable data access system.
Section Objectives
Describe the process of capacity planning.
Explain the considerations for designing a scalable data access system.
Explain the methods for determining whether data access is a performance and scalability
bottleneck, and explain the guidelines for choosing a particular method.
Introduction
Capacity planning is the process that is used to ensure that a solution will be able to support future
demand and workload. It is important to incorporate capacity planning into the planning phase to
ensure that the system will be able to bear future performance and scalability demands caused by
higher workloads.
Capacity planning is supported by many subordinate activities, such as testing and performance
monitoring and log analysis. It is also supported by other activities that provide information about
how the system is being used and how it responds to the current workload.
The capacity planning process is a critical input when designing and architecting your solution.
Most application performance and scalability issues are not caused by scarce system resources but
by ineffective architecture and coding practices.
Quick definition Upgrade or fine-tune current hardware. Distribute the load over a group of federated
servers.
Decision • Costs. • Data partitioning.
parameters • Hardware upgrade capacity. • Replication configuration.
• Configuration. • Complex disaster recovery and
failover.
• System availability requirements.
Recommended • When bottleneck is on system • When heavy load requires more power
resources. than can be handled by one server.
If a database application is distributed among different infrastructure layers, you should conduct
extensive testing to monitor the performance objectives and thresholds of each layer.
2. Measure the thresholds on constrained resources. Constrained resources are system resources
that are reaching their threshold level with the current load on the system; for example, an
application that is consuming 90 percent of processor time.
• Identify the thresholds.
• Identify the average usage pattern of each constrained resource.
4. Calculate the future volume and the usage pattern costs of the future volume.
• Based on the previous measurements, calculate the approximate load level that the application
can handle under the current conditions.
• Modify the variables and calculate which conditions should vary to reach the expected load
level.
When calculating future capacity, you must consider the possibility of code changes and future
additions that might require more system resources.
Discussion Question
What do you think about first when you think you need to scale a database application? Consider
the following:
• Scale up vs. scale out
• Which layer to scale
Additional Information
For an introduction to performing capacity planning, see “How to: Perform Capacity Planning
for .NET Applications” in the Improving .NET Application Performance and Scalability Guide
on the Microsoft Patterns & Practices Web site at
http://msdn.microsoft.com/practices/guidetype/Guides/default.aspx?pull=/library/en-
us/dnpag/html/scalenethowto06.asp.
For more information about how scaling up or scaling out affects database applications, see
“Improving SQL Server Performance” in the Improving .NET Application Performance and
Scalability Guide on the Microsoft Patterns & Practices Web site at
http://msdn.microsoft.com/practices/guidetype/Guides/default.aspx?pull=/library/en-
us/dnpag/html/scalenetchapt14.asp.
For more information about the layering pattern and how it might affect application
performance, see “Deployment Patterns” on the Microsoft Patterns & Practices Web site at
http://msdn.microsoft.com/practices/Topics/arch/default.aspx?pull=/library/en-
us/dnpatterns/html/EspDeploymentPatterns.asp.
Many other third-party methodologies for measuring and benchmarking system performance
are available—for example, the methodologies defined by Transaction Processing Performance
Council (http://www.tpc.org).
Introduction
The database server is expected to be the most optimized and efficient component in the entire
application architecture. For example, the response time of the database server is measured in
milliseconds. The database server manages large and complex sets of data and is heavily accessed
for read and write operations. The database server must manage concurrent access to the data, and it
must maintain data integrity and security. There are various considerations for designing a scalable
database access system. This topic focuses on the following considerations:
Appropriate situations for scaling the system hardware
Considerations for designing a database system to support an increased number of connections
Considerations for designing a database system to support an increased number of transactions
Consideration Reason
Introduction
The primary input for any capacity planning is testing and monitoring. Application developers
should continuously monitor the application’s health to identify performance and scalability
bottlenecks and verify that resource usage remains lower than the thresholds set by the performance
objectives.
Data access could become a bottleneck in situations in which the application suffers scalability
problems due to inappropriate connection patterns and excessive data consumption.
Demonstration Overview
The best performance and scalability gains result from fine-tuning the application code rather than
spending excessive time tuning the operating system or SQL Server configuration. Applying some
database development best practices can help database administrators achieve better levels of
database execution performance.
However, it is important to monitor a database application to be able to detect when data access is
the performance and scalability bottleneck in the application.
In this demonstration, your instructor will show how to use common monitoring tools to detect
potential performance issues, identify code that is causing performance problems, and determine
the best way to solve the problem.
11. Right-click Default.aspx, and then on the shortcut menu, click View in Browser.
12. The application opens in a Microsoft Internet Explorer window. When the page finishes loading,
it shows a phrase with the text The average value isin red.
13. To copy the URL, in Internet Explorer, on the Address bar, right-click the URL, and then click
Copy.
14. To close the Default.aspx file, on the title bar, click Close.
15. Return to Visual Studio 2005.
16. In the LoadTest project, double-click WebTest1.webtest.
17. To open the Properties pane, in the WebTest1.webtest window, click the URL under the
WebTest1 node, and then press F4.
18. In the Properties pane, select the URL item, paste the URL copied from Internet Explorer, and
then press ENTER.
19. In Solution Explorer, in the LoadTest project, double-click 15UserLoad.loadtest.
20. To open the Properties pane, in the 15UserLoad.loadtest window, in the Run Settings folder,
click the Local node, and then press F4.
21. Notice the properties for the load test.
Section Overview
Locking is a technique used by SQL Server 2005 to synchronize the actions executed by multiple
concurrent transactions. Sometimes transactions and requests begin to queue when they are waiting
for a lock to be released. In such cases, concurrency contention occurs. This type of contention
severely affects the scalability of database applications by limiting the number of concurrent
operations that these systems can execute.
A database system is scalable if it is possible to increase the number of transactions, the number of
simultaneous connections, and the volume of data to be processed. Contention is a major barrier to
scalability. This section explains the different techniques for minimizing contention and thereby
maximizing the scalability of a database system.
There are various techniques that avoid holding locks—for example, row-level versioning, which is
the basis of the new Snapshot isolation level in SQL Server 2005.
This section focuses on selecting the appropriate methodology to avoid concurrency problems and
thereby improve application performance.
Section Objectives
Explain how concurrency contention occurs.
Explain the guidelines for using database snapshots for improving concurrency.
Explain the advantages and disadvantages of using the Snapshot isolation level.
Explain the process of denormalizing a database to improve concurrency.
Introduction
Application users often complain about poor system performance. However, in most cases, what
the users actually experience is poor response time. Concurrency and locking are among the causes
of poor response time, as database processes wait for the release of locks held by other transactions.
When two requests try to modify the same row concurrently, SQL Server will raise a concurrency
error to the last executing process. The error indicates that the row is being modified by another
uncommitted transaction.
There are two types of concurrency control: pessimistic and optimistic.
Pessimistic concurrency control assumes that reading operations will be affected by data-
modification operations from other processes. Therefore, this type of concurrency enforces a
locking mechanism to coordinate access to resources such as tables, rows, extents, and pages.
Optimistic concurrency control assumes that reading operations must not be affected by data-
modification operations from other processes. Therefore, instead of locks, optimistic concurrency
control is implemented in SQL Server 2005 as a row-versioning technique for read data. This
allows read and write operations to execute concurrently. Concurrency contention does not occur
when optimistic concurrency is used.
Use database snapshots. Provides a read-only static view of Allows separation of read and write
a database. operations.
Use optimistic concurrency Does not keep active locks for a Allows separation of read and write
with the Snapshot isolation long period. operations.
level. Keeps multiple versions of updated
data.
Denormalize the database. Introduces preaggregated or Improves query response time and
summarized data to satisfy minimizes locking, thereby
commonly executed queries. increasing system scalability.
Avoid user input in Avoids running a transaction that Avoids holding locks until the input
transactions. requires end-user input to commit. is received.
Keep transactions as short Executes validation or any other Locks are held only as long as they
as possible. logic code that is not dependent on are required.
the transaction outside the
transaction scope.
Opens lazy; commits early.
Keep transactions in one If a transaction spans more than one Locks are held longer than needed
batch. batch, network latency affects the while waiting for network packets.
network packet sending and
retrieval process, thereby increasing
the transaction execution time.
Use transaction isolation Lowers the isolation level to less Some scenarios can avoid locking
hints where appropriate. restrictive isolation levels where or at least use less restrictive
appropriate. locking schemes.
Access resources in the When multiple processes execute Avoids deadlocking.
same order. the same steps, accesses shared
resources in the same order.
tempdb Contention
The tempdb database could also suffer from contention due to concurrency issues.
The following types of operations use the tempdb database intensively and should be carefully used
and monitored frequently as the load on the application grows:
Multiple Active Result Sets (MARS)
The Snapshot transaction isolation level
Repeated creation and dropping of temporary tables (local or global)
Table variables that use tempdb for storage purposes
Work tables associated with CURSORS
Work tables associated with an ORDER BY clause
Work tables associated with an GROUP BY clause
Work files associated with HASH PLANS
Additional Information
Read Microsoft Knowledge Base article 75722, “INF: Reducing Lock Contention in SQL
Server” at http://support.microsoft.com/default.aspx?scid=kb;en-us;75722&sd=tech.
Read the “Transactions” topic in Chapter 14, “Improving SQL Server Performance,” in the
Improving .NET Application Performance and Scalability Guide at
http://msdn.microsoft.com/practices/guidetype/Guides/default.aspx?pull=/library/en-
us/dnpag/html/scalenetchapt14.asp#scalenetchapt14%20_topic9.
For more information about tempdb contention and some strategies for enhancing concurrency
on the tempdb database, read “KB328551 – FIX: Concurrency enhancements for the tempdb
database” at http://support.microsoft.com/default.aspx?scid=kb;en-us;328551.
Introduction
Database snapshots are a new feature in SQL Server 2005. A database snapshot is a static, read-
only copy of the current committed state of the source database. You can use database snapshots to
improve concurrency in a database application by taking a snapshot of the current state of the data
for reporting purposes, separating read and write operations.
The internal structures of SQL Server that support database snapshots are optimized in such a way
that space consumption is minimized when reading data.
Demonstration Overview
In this demonstration, your instructor will explain the guidelines for using database snapshots to
improve concurrency. Two applications will run in parallel. Each application will show the number
of transactions being executed per second. One application will write data, and the other application
will read data.
Both applications will connect to the same database. A database snapshot will then be created, and
the reader application will use the snapshot database. The number of transactions per second will be
higher for both applications after applying this change.
Introduction
The Snapshot isolation level is a new transaction isolation level based on row-versioning
techniques. It is considered an optimistic concurrency control technique because it minimizes
locking on the original data and permits concurrent execution of read and write operations without
blocking each other.
As a recommended technique to avoid concurrency contention, the Snapshot isolation level has
advantages as well as disadvantages. For example, it separates read and write operations, thereby
maximizing concurrency. However, this advantage comes at a cost, because the system needs to
automatically maintain multiple copies of data. In some cases, maintaining multiple copies of data
could produce update conflicts.
Developers should consider these advantages and disadvantages when designing data access
methodologies, to ensure that the Snapshot isolation level is used appropriately.
Discussion Questions
1. Do you need to migrate Oracle database applications to SQL Server?
3. Have you experienced excessive lock wait time in your applications? How did you diagnose that
lock wait time was the problem?
4. Have you experienced any problems in scaling tempdb to support the Snapshot isolation level?
5. How do you plan to deal with conflicts when different data versions are updated?
6. Would you select the Snapshot isolation level per transaction or per database? Why?
7. What are some of the disadvantages of using the Snapshot isolation level?
Introduction
The normalization rules generate data models that are optimized for OLTP applications with
combined read and write operations. In some cases, an application can benefit by not fully applying
the normalization rules. This design pattern is also known as denormalizing.
Denormalization can be applied to improve the database system performance. There are various
strategies available to denormalize data. You should choose the appropriate strategy based on the
data access patterns and the data structure specific to your application.
Discussion Questions
1. Have you ever considered denormalizing data to improve concurrency?
4. Would you implement business entities as a set of related independent tables (using vertical
partitioning)?
5. When would you choose indexed views, and when would you choose redundant summary tables?
8. Would using computed columns support better concurrency? Why would you use computed
columns?
Section Overview
The two strategies for scaling a database server are scaling up and scaling out. Scaling up is the
process of increasing hardware resources—for example, by adding more memory or processing
power. Scaling out refers to distributing the current installation to a multiserver physical
architecture.
Various technologies and strategies are available for distributing data among a group of federated
servers. Database administrators should be careful when choosing the appropriate technology.
Solution designers must choose to scale up or scale out, depending on the scalability requirements
of a given database system. This section provides considerations and guidelines for choosing the
appropriate option.
Section Objectives
Explain the guidelines for distributing data and requests across multiple tables, databases,
instances, and servers.
Explain how data partitioning works.
Explain the benefits of implementing well-defined interfaces to encapsulate the physical
implementation of distributed database systems.
Explain the guidelines for scaling applications by using Service Broker.
Compare the performance and flexibility of relational systems with that of SQL Server Analysis
Services.
Introduction
You can scale out a database system by separating or dividing the database tables based on certain
criteria. The pattern of data reads and writes should determine how you divide the tables themselves
and how you provide access to the data.
Separating the data at the logical level allows database administrators to distribute the data
physically. As a result, the system might benefit from using the appropriate hardware and specific
system settings to optimize data access. For example, you can distribute data across multiple disks
based on a specific column value, such as the fiscal year for sales information.
SQL Server 2005 provides various strategies for distributing data and requests.
Tables Use table partitioning. Data can be Denormalize the data schema by storing a single
partitioned according to a partition key entity across multiple tables linked by a one-to-
and distributed automatically through a one relationship.
set of filegroups.
Databases Allow different database configuration Allow different database configuration and
and security settings. security settings.
Use distributed partitioned views to Use distributed partitioned views to expose data as
expose data as a single unit that is a single unit that is independent of storage.
independent of storage.
Instances Allow different installation options and Allow different installation options and server
server resource administration. resource administration.
Use distributed partitioned views to Use distributed partitioned views to expose data as
expose data as a single unit that is a single unit that is independent of storage.
independent of storage.
Servers Use federated servers. This allows a Use federated servers. This allows a hardware and
hardware and software configuration that software configuration that provides independent,
provides independent, parallel access to parallel access to data that is stored on different
data that is stored on different servers. servers.
Use distributed partitioned views to Use distributed partitioned views to expose data as
expose data as a single unit that is a single unit that is independent of storage.
independent of storage.
Additional Information
For more information about configuring log shipping, see “Configuring Log Shipping” in SQL
Server 2005 Books Online at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx.
Introduction
Data partitioning is not a new SQL Server feature. In SQL Server 6.5, database developers and
database administrators were able to partition data horizontally based on a specific value.
SQL Server 7 and SQL Server 2000 introduced the concept of partitioned views.
SQL Server 2005 offers a new approach to data partitioning that is loosely coupled, highly scalable,
and reusable and that simplifies database administration and maintenance.
Discussion Questions
1. How is data partitioning different from the partitioned view feature that is available in SQL
Server 2000?
Additional Information
For more information about data partitioning, see the article “Partitioned Tables and Indexes in
SQL Server 2005” at http://msdn.microsoft.com/SQL/learn/arch/default.aspx?pull=/library/en-
us/dnsql90/html/sql2k5partition.asp.
Introduction
Changes in the physical implementation of database objects affect the maintainability of the
database applications that use these objects. The maintainability of a database system depends on its
ability to adapt to these changes without affecting existing applications.
The various types of public interfaces in a database system are stored procedures, user-defined
functions, and views. Abstraction is a term that is used to define an alternative view of a system that
presents only the relevant elements and hides the implementation details that are not relevant.
Abstraction is a powerful mechanism to create maintainable systems.
Database applications can benefit from abstraction by implementing well-defined interfaces to
encapsulate the physical implementation of distributed database systems.
Provide dynamic calculated and aggregated data that is not available in the physical model.
Maintain and change the underlying physical model without affecting the current applications.
Allow for different security permissions at the logical and physical levels to allow the user access
to the public interface but not to the physical implementation.
Provide another layer of security to protect the physical implementation—for example, from SQL
injection attacks.
Introduction
SQL Server Service Broker is a platform for developing loosely coupled services for database
applications. Service Broker is fully integrated with SQL Server.
When databases are heavily accessed, they can offload some of the data processing to another
server or queue the requests until there is less demand to access the database. This action
automatically scales the application. This is possible because of the transactional queuing capability
provided by Service Broker.
In this demonstration, your instructor will show how to scale applications by using Service Broker.
Your instructor will also explain the guidelines that you must follow for scaling applications by
using Service Broker.
Demonstration Overview
Service Broker includes the ability to execute stored procedures asynchronously. In this
demonstration, your instructor will first synchronously execute an expensive (resource-intensive)
procedure. To enhance application scalability, Service Broker will be enabled to execute the
expensive procedure asynchronously, thereby improving the response time.
Use Service Broker routing for distributing messages to different computers without affecting the
application code.
Additional Information
For a complete introduction to SQL Server Service Broker, see “An Introduction to SQL
Server Service Broker” at http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnsql90/html/sqlsvcbroker.asp.
Introduction
In some cases, OLTP database applications need to present complex reports by aggregating data
from multiple sources and making multidimensional calculations. SQL Server Analysis Services
provides tools to design, implement, and maintain such application types and can be fully integrated
with OLTP applications.
SQL Server Analysis Services provides a platform on which to build, administer, and deploy
business intelligence solutions. It also provides online analytical processing (OLAP) and data-
mining functionality.
SQL Server Reporting Services is a platform on which to build, administer, and deploy reporting
and data-analysis solutions that can be consumed from .NET Framework applications and by
applications for other platforms.
By combining the power of these two SQL Server services, regular OLTP applications can choose
from various available options when implementing complex data aggregation and reporting.
Demonstration Overview
In this demonstration, your instructor will show a report generated against relational data and will
then show the same report generated against an OLAP cube. Your instructor will also explain some
of the advantages of using OLAP in certain scenarios, especially when working with multiple-
dimension data or aggregated data.
Additional Information
For more information about Analysis Services, see Course 2074, Designing and Implementing
OLAP Solutions Using Microsoft SQL Server 2000
(http://www.microsoft.com/learning/syllabi/en-us/2074afinal.mspx) and Course 2093,
Implementing Business Logic with MDX in Microsoft SQL Server 2000
(http://www.microsoft.com/learning/syllabi/en-us/2093afinal.mspx).
Introduction
Over the years, the database server’s role in system architecture has changed from being a complete
system in a monolithic architecture to being a part of a bigger, distributed, multi-tiered architecture.
Service-oriented architecture (SOA) is a new architecture model that proposes to expose internal
business processes as a set of services. The services are loosely coupled, allowing for easier
distribution, scalability, and maintainability. The number of requests to a service and the number of
transactions per second that must be supported is higher in a service-oriented architecture than in an
isolated application that does not shares the architecture’s processes.
Service-oriented architecture is based on the distributed, multi-tier architectural model. Internally,
services might distribute the workload through multiple logical tiers that define and process the
business logic and rules.
This section focuses on how to improve middle-tier processing by using various middle-tier-
specific technologies.
Section Objectives
Describe the different ways to create middle-tier strategies to connect to SQL Server.
Evaluate the considerations for building a data access layer based on COM+ components.
Explain the guidelines for building Web services to provide data access.
Explain the guidelines for building Hypertext Transfer Protocol (HTTP) endpoints to scale up data
access in SQL Server 2005.
Explain how to move code between tiers to reduce the workload on a bottlenecked tier.
Introduction
In a multi-tiered application, much of the processing is executed on the middle tier. You should
separate the different processing needs into different logical layers that together form the middle
tier. Further dividing the middle tier into separate logical services allows for distribution,
scalability, and maintainability.
Client applications can access the middle tier directly through various service interfaces. The same
middle tier can be reused by multiple client applications that require the same services provided by
the middle tier.
There are different strategies for implementing a middle tier by using Microsoft development
platforms. Application architects should decide which strategy to follow based on their
organization’s communication, security, scalability, maintenance, and administration needs.
Middle-Tier Components
The middle tier serves multiple needs in a distributed application, and it can be divided into
multiple subtiers to facilitate maintenance. Each subtier specializes in a particular logical task and
can be implemented by any number of components.
Following are the recommended subtiers in a middle tier:
Service interfaces
Business workflows
Business components
Business entities
Service agents
Middle-Tier Responsibilities
The middle tier is responsible for the following activities:
Exposes business logic as a service
Orchestrates, enforces, calculates, and processes business rules and tasks
Accesses multiple data sources to retrieve or save necessary data to process the business rules
Provides a data-source-independent representation of the data to be passed to calling client
applications
Additional Information
For more information about the architecture of .NET Framework applications, see the
Application Architecture for .NET: Designing Applications and Services Guide on the
Microsoft Pattern & Practices Web site at
http://msdn.microsoft.com/practices/guidetype/Guides/default.aspx?pull=/library/en-
us/dnbda/html/distapp.asp.
Introduction
Included in Microsoft Windows, COM+ is a set of services used to build highly scalable middle-tier
components. Middle-tier components include the business logic and data access layers. The COM+
infrastructure provides several services that assist in building and deploying robust middle-tier
components. As a result, developers can focus on the business logic rather than on low-level
requirements when scaling the application.
However, application architects should carefully evaluate when to use COM+ components. COM+
relies on other Windows services such as the Distributed Transaction Coordinator (DTC) and
Message Queuing. Using COM+ results in applications that have specific implementation,
deployment, and administration requirements.
Activities can span multiple components distributed among multiple servers—for example, to
create distributed transactions.
COM+ provides a hosting environment for components and allows system administrators to
configure security, activation, memory consumption, transactions, and other such services from a
visual administrative console.
Disadvantages
You might experience a small decrease in performance when using COM+ interoperability
services from the .NET Framework managed code.
Different COM+ versions provide different services—for example, COM+ 1.0 supports only the
Serializable transaction isolation level.
COM+ components create a dependency between the application and several Windows services
such as COM+ itself, MSMQ, and the DTC.
Using COM+ imposes restrictions on the deployment model, communication protocols, and
administrative tasks. For example, it is difficult to consume services provided by COM+
components over the Internet because of the underlying remote procedure call (RPC)
infrastructure used by COM+.
The simplicity of the declarative programming model, combined with developers’ inadequate
knowledge of the inner workings of COM+, could cause the use of the provided services in
inappropriate scenarios.
Note: Microsoft .NET Framework 2.0 proposes a new model for handling transactions on the
middle tier and solves the issues imposed by COM+. New classes for transaction management
are declared inside the System.Transactions namespace.
System.Transactions proposes two types of transactional managers: Lightweight Transaction
Manager (LTM) and OleTx Transaction Manager (functionality formerly provided by the
DTC). LTM is used for local transactions, and OleTx is used for distributed transactions. The
component does not need to be registered in COM+ to take advantage of this feature.
Additional Information
For an overview of COM+, including its history and what it means for the middle tier, see
“COM+ Overview for Microsoft Visual Basic Programmers” at
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dncomser/html/complus4vb.asp.
For an introduction to COM+ and the services it provides, see “Understanding Enterprise
Services (COM+) in .NET” at http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dndotnet/html/entserv.asp.
For more information about System.Transactions and the changes in the .NET Framework
2.0, see “Introducing System.Transactions in the .NET Framework 2.0” at
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dndotnet/html/introsystemtransact.asp.
Introduction
XML Web services expose the application logic to external clients and other services in a loosely
coupled manner.
If an XML Web service is designed based on the industry standards for communication, data
representation, and security, it can be consumed by applications running on different platforms and
written in different programming languages.
When exposing a middle tier through XML Web services, applications need to fine-tune, manage,
and administer the middle-tier resources carefully. This is because the application logic will be
available to external entities that the systems administrator might not be able to control or fine-tune.
Regularly monitor the amount of data being sent to and from the service. Serializing and
deserializing XML data can be an expensive process.
Watch and favor processes that can be executed asynchronously.
Keep the transaction scope inside the service. XML Web services do not support distributed
transactions outside the service boundary.
Introduction
Because earlier versions of SQL Server supported only the proprietary Tabular Data Stream (TDS)
communication protocol, client applications had to use a data access provider to create the TDS
packages to communicate with SQL Server.
SQL Server 2005 extends support to other communication protocols, such as HTTP, to allow client
applications to connect without using a data access provider.
By creating an HTTP endpoint, SQL Server starts to listen on a specific communication port for
messages that are translated into the execution of registered stored procedures or user-defined scalar
functions.
Manage endpoints to limit connect permissions to specific users or groups that are going to be
consuming the endpoint. Avoid granting access to the public role.
Additional Information
For more guidelines on using HTTP endpoints, see the topic “Best Practices for Using Native
XML Web Services” in SQL Server Books Online at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx.
Introduction
Component-based programming allows encapsulation of the programming logic in reusable units of
code. This code can be used at any layer or tier in the application and can even be reused by
multiple applications. Software architects design the distribution and location of reusable units
around the physical boundaries of the application.
The location of the code is decided based on the following factors:
Security restrictions
Class access modifiers
Transactional requirements
Communication requirements
Deployment issues
Overall physical configuration and distribution of server
In some scenarios, moving code between tiers can improve application scalability by reducing the
workload on a heavily used application tier or feature.
In this demonstration, your instructor will show how to move code between tiers to reduce the
workload on a bottlenecked tier.
Demonstration Overview
In this demonstration, your instructor will show a simple example of a business rule that is checked
initially in the database server, moved to the data access layer, moved to an XML Web service, and
finally moved to the client application.
Section Overview
Scaling the physical infrastructure of a distributed system usually involves two techniques: scaling
up and scaling out.
Scaling up involves increasing the resources on the current hardware—for example, by increasing
the amount of memory, adding more processors, and upgrading the hard disks.
Scaling out can be applied to any layer of a database application as follows:
Presentation layer, by configuring a Web farm for an ASP.NET Web application
Middle-tier, business layer, by configuring multiple middle-tier servers in a Windows cluster
Data layer, by using the various options available for scaling out SQL Server
Changes made to a single layer will have a direct impact on the other layers. Therefore, scaling out
should be a system-wide improvement.
This section focuses on scaling out front-end systems for Windows-based applications and Web
applications to improve system response time and availability.
Section Objectives
Explain the types of applications for which scaling out is appropriate.
Explain the considerations for improving the availability and scalability of Windows-based
applications.
Explain the guidelines for scaling out Web front-end systems.
Explain the guidelines for implementing data access in Web farms.
Introduction
All applications cannot be scaled out, and scaling out does not always provide benefits.
Scaling out increases the complexity of system management, development, resource management,
networking, security, and fine-tuning.
Scaling out a system involves adding more servers and distributing the workload among them. The
operating system, application services, resource management, and all other control systems (such as
network interface controllers, multiple processor controllers, and disk controllers) should be aware
of this physical architecture to provide the expected benefits.
Scaling out a single tier will have an impact on system performance. Improper scaling out of a
single tier will create an imbalance between all of the tiers, add overhead, and decrease the effects
of scaling out.
Discussion Questions
1. Is scaling out front-end systems relevant to Web applications or Windows-based applications?
2. Would you solve your scalability needs without scaling out the back end?
3. Is there any potential synchronization problem to consider when scaling out front-end systems?
Introduction
Windows-based applications can be designed according to the following architectural models:
monolithic, two-tier (client/server), N-tier (which includes three-tier), and/or service-oriented.
Some of these architecture models require the physical distribution of the application components
to allow reuse of such components and to support larger working payloads. In these cases, the
presentation layer is deployed on the client machines and communicates with the remotely
deployed middle-tier components.
Following are the benefits of developing distributed Windows-based applications:
Take advantage of local computer resources.
Provide a better end-user experience.
Cache data locally.
Connect to multiple middle-tier components dynamically or to multiple data sources such as
remote Web Services.
Take advantage of local disk storage in disconnected environments. For example, you can
continue working on a laptop computer even when there is a network failure.
This topic focuses on the various considerations for improving the availability and scalability of
Windows-based applications.
Improving Response Time by Decoupling the Front End from the Back
End as Much as Possible
Decoupling involves lowering the dependency level between the front end and the back end. The
following table describes some of the dependencies you must consider.
Dependency Description
Data representation The front end should know only about the internal logical data representation
known as Business Entities. Business Entities should be designed to model data
independent of storage options and physical distribution.
Distributed User interface actions should not be enlisted inside distributed transactions
transactions because this will create long-running transactions. These transactions will keep
locks for a long time and reduce database concurrency.
Data sources The front end should not know how the data is stored physically and which data
sources exist. An abstraction layer should be built between the front end and the
back end so that the back end can be modified without affecting the front end.
Introduction
Web applications provide an easier administration and deployment model than that provided by
Windows-based applications. The application does not need to be installed on the client machine,
but the client will navigate to the server on which the application is hosted by using a browser.
Various servers, frameworks, and services interact with each other to serve a single Web request.
For example, IIS receives the request and passes it on to the ASP.NET runtime framework, and the
Web pages with custom code then execute the request and might also execute remote calls to
middle-tier servers.
Although database developers must have adequate knowledge of the various platforms and servers
for scaling out Web front-end systems, the ability of an application to scale out depends more on
the application architecture than on the underlying infrastructure.
In this topic, you will learn about the guidelines to apply for improving the availability and
scalability of Web front-end systems.
Introduction
When scaling out a Web application, requests will be distributed across multiple servers configured
in an NLB cluster.
It is important to provide an optimal data access architecture for a database application. Application
architects must decide how to deploy the application components into the distributed physical
architecture.
This topic focuses on some of the guidelines that you must follow when implementing data access
layers in Web farms.
Avoid remote method Whenever possible, each Web farm member should have its own data
calls. access layer. By avoiding remote method calls, you can avoid expensive
network trips, data serialization, and network latency.
Develop the data Stateless components support both scale-up and scale-out strategies.
access layer as Stateless components avoid server affinity and optimize memory
stateless components. management by using only short-life objects.
Deploy SQL Server in Instead of managing multiple database servers—for example, one
a clustered database for each node in the Web farm—implement a scalable database
infrastructure. architecture by scaling out the database installation into a database
cluster. The cluster will be seen as a single entity by calling clients.
Consider data Do not partition the data into multiple servers only for scalability reasons
partitioning only when if the data is not congruent with the partitioning model or does not clearly
the data permits it. contain a partition key. Otherwise, it will be difficult to administer and
MCT USE ONLY. STUDENT USE PROHIBITED maintain data, and applications might not benefit from data partitioning.
62 Session 6: Designing a Scalable Data Tier for Database Applications
Introduction
This session focused on how to assess scalability needs and design the best architecture to scale
your system to meet the needs and expectations of users. You learned how to identify when to scale
database applications and what layer to scale. You also learned how to select the appropriate
technology to avoid concurrency problems and improve application performance. Finally, you
learned how to evaluate whether scaling out or scaling up is appropriate for the scalability
requirements of your database system.
Discussion Questions
1. What was most valuable to you in this session?
2. Have you changed your mind about anything based on this session?
3. Are you planning to do anything differently on the job based on what you learned in this session?
If so, what?
Clinic Evaluation
Your evaluation of this course will help Microsoft understand the quality of your learning
experience.
Please work with your training provider to access the clinic evaluation form.
Microsoft will keep your answers to this survey private and confidential and will use your
responses to improve your future learning experience. Your open and honest feedback is very
valuable.