dat321 sqlclr-- 在 sql server 2005 中运行 .net 程序

26
DAT321 DAT321 SQLCLR-- SQLCLR-- SQL Server 2005 SQL Server 2005 在在 在在 .NET .NET 在在 在在 在在在 在在在 [email protected] [email protected] 在在在在在 在在在在在 在在 在在 ( ( 在在 在在 ) ) 在在在在 在在在在

Upload: stephen-ochoa

Post on 12-Mar-2016

153 views

Category:

Documents


11 download

DESCRIPTION

DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序. 邓英达 [email protected] 产品技术部 微软 ( 中国 ) 有限公司. 内容列表. 第一部分:概念介绍 第二部分:技术架构 + 实战演示 第三部分:监控与查错. Common Language Runtime 概述. CLR 是 C#, VB.NET 等语言的运行环境 代码编译为中间语言层 Intermediate Language (IL) 自动内存管理 (GC) 通用数据类型 代码检验 代码访问安全 丰富的类库支持 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

DAT321DAT321SQLCLR--SQLCLR-- 在在 SQL Server 2005SQL Server 2005 中运中运行行 .NET.NET 程序程序

邓英达邓英达[email protected]@microsoft.com产品技术部产品技术部微软微软 (( 中国中国 )) 有限公司有限公司

Page 2: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序
Page 3: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

内容列表内容列表第一部分:概念介绍第一部分:概念介绍第二部分:技术架构第二部分:技术架构 ++ 实战演示实战演示第三部分:监控与查错第三部分:监控与查错

Page 4: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

Common Language RuntimeCommon Language Runtime 概述概述CLR CLR 是 是 C#, VB.NETC#, VB.NET 等语言的运行环境等语言的运行环境代码编译为中间语言层 代码编译为中间语言层 Intermediate Intermediate Language (IL) Language (IL)

自动内存管理 自动内存管理 (GC)(GC)通用数据类型通用数据类型代码检验 代码检验 代码访问安全代码访问安全

丰富的类库支持丰富的类库支持应用程序域 应用程序域 – – 隔离单元隔离单元支持程序调试支持程序调试

Page 5: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

.NET Framework .NET Framework 和 和 CLR CLR CLR CLR 执行模型执行模型

VBVB源代码源代码CompilerCompiler

C++C++C#C#

CompilerCompilerCompilerCompiler

AssemblyAssemblyIL CodeIL Code

AssemblyAssemblyIL CodeIL Code

AssemblyAssemblyIL CodeIL Code

操作系统服务操作系统服务

公共语言运行时公共语言运行时即时编译器即时编译器 JIT CompilerJIT Compiler

二进制代码二进制代码

管理代码管理代码ManagedManagedcodecode

UnmanagedUnmanagedComponentComponent

Page 6: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

在在 SQL2005SQL2005 中运行中运行 CLRCLR

VS 2005 VS 2005 ProjectProject

Assembly: “TaxLib.dll”

VB, C#, …VB, C#, …

SQL ServerSQL Server

SQL Data Definition: SQL Data Definition: create create assembly … assembly … create function … create function … create create procedure … procedure … create trigger … create trigger … create create type …type …

SQL Queries: SQL Queries: select select sum(tax(sal,state) ) sum(tax(sal,state) ) from Emp where county from Emp where county = ‘King’= ‘King’

Runtime hosted inside

SQL

BuildBuild

deploydeploy

Page 7: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

内容列表内容列表第一部分:概念介绍第一部分:概念介绍第二部分:技术架构第二部分:技术架构 ++ 实战演示实战演示第三部分:监控与查错第三部分:监控与查错

Page 8: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

SQL CLRSQL CLR 创建与调用创建与调用

Page 9: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

在在 SQLSQL 中中 Hosting CLRHosting CLR

CLRCLR 通过通过 SQL ServerSQL Server 进行进行 ::MemoryMemoryThreadsThreadsSynchronizationSynchronization

SQLSQL 和和 CLRCLR 协作完成:协作完成:Assembly LoadingAssembly LoadingSecurity managementSecurity managementApp domainsApp domainsEscalation policy for exceptionsEscalation policy for exceptions

SQL Server SQL Server ProcessProcess

WindowsWindows

SQL OSSQL OS

CLRCLR

HostingHostingLayerLayer

Page 10: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

技术架构技术架构内存管理内存管理程序集管理程序集管理应用程序域管理应用程序域管理CPUCPU 管理管理 : : 线程、同步、死锁等线程、同步、死锁等安全管理安全管理

Page 11: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

内存管理内存管理All memory allocation from CLR through SQL ServerAll memory allocation from CLR through SQL ServerMost of CLR memory (GC Heap) comes from multi-Most of CLR memory (GC Heap) comes from multi-page memory allocator (outside Buffer-pool, from page memory allocator (outside Buffer-pool, from “MemToLeave”)“MemToLeave”)

Max server memoryMax server memory setting does not cover CLR memory setting does not cover CLR memoryOn memory pressure, SQL and CLR coordinate in On memory pressure, SQL and CLR coordinate in order to initiate a GCorder to initiate a GCDMVs for monitoring CLR memory usageDMVs for monitoring CLR memory usage

Sys.dm_os_memory_clerksSys.dm_os_memory_clerksSys.dm_os_memory_objectsSys.dm_os_memory_objects

Memory clerks of interest: SQLCLR, Memory clerks of interest: SQLCLR, SQLCLRASSEMBLYSQLCLRASSEMBLYPerf counters: everything under .NET CLR MemoryPerf counters: everything under .NET CLR Memory

Page 12: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

程序集管理程序集管理SQL Server sets a pre-defined list of FX SQL Server sets a pre-defined list of FX assemblies that can be referenced in user assemblies that can be referenced in user assemblies.assemblies.

This ‘blessed’ list is set by SQL Server at the This ‘blessed’ list is set by SQL Server at the time of initializing CLRtime of initializing CLRCLR automatically loads these from GACCLR automatically loads these from GACThese assemblies underwent heavy reliability These assemblies underwent heavy reliability work to meet the server reliability barwork to meet the server reliability barCertain restrictions apply in these assemblies Certain restrictions apply in these assemblies (e.g. cannot do Process.Exit)(e.g. cannot do Process.Exit)

All other assembly load requests come to All other assembly load requests come to SQL Server.SQL Server.All user assemblies required by a query All user assemblies required by a query should be present in the database. should be present in the database.

Page 13: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

程序集管理程序集管理Blessed AssembliesBlessed Assemblies

Microsoft.VisualBasicMicrosoft.VisualBasicMscorlibMscorlibSystemSystemSystem.DataSystem.DataSystem.Data.SqlXmlSystem.Data.SqlXmlSystem.Data.OracleClientSystem.Data.OracleClientSystem.TransactionsSystem.TransactionsSystem.ConfigurationSystem.ConfigurationSystem.XmlSystem.XmlSystem.SecuritySystem.SecuritySystem.Web.ServicesSystem.Web.ServicesMicrosoft.VisualCMicrosoft.VisualCCustomMarshalersCustomMarshalers

Page 14: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

应用程序域管理应用程序域管理App domains: CLR mechanism for isolating codeApp domains: CLR mechanism for isolating codeApp domains are created and managed by SQL ServerApp domains are created and managed by SQL Server

Appropriate security is defined at creationAppropriate security is defined at creationFailure behavior is controlled by SQL ServerFailure behavior is controlled by SQL Server

Granularity:Granularity:One per assembly owner in each databaseOne per assembly owner in each databaseAll assemblies owned by same user are in same app domainAll assemblies owned by same user are in same app domain

SQL processCLRAppDomain 1

AppDomain 2

AppDomain 3

Database A

Database B

User a

User b

Page 15: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

CPUCPU 管理管理 : : 线程线程 , , 同步同步All managed “threads” are mapped to tasks managed by the All managed “threads” are mapped to tasks managed by the SQL Server schedulerSQL Server scheduler

Co-operative schedulingCo-operative schedulingException: GC threadsException: GC threads

SQL Scheduler detects non-yielding tasks (managed or T-SQL) SQL Scheduler detects non-yielding tasks (managed or T-SQL) and “punishes” themand “punishes” them

Force the task to stop, put back in the queue and miss some turnsForce the task to stop, put back in the queue and miss some turnsCallback to SQL Server on P/Invoke (when calling native code)Callback to SQL Server on P/Invoke (when calling native code)

Switched to pre-emptive modeSwitched to pre-emptive modeCLR lock requests go through SQL ServerCLR lock requests go through SQL Server

Unified deadlock detection between locks held by CLR and native Unified deadlock detection between locks held by CLR and native SQLSQL

Page 16: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

安全管理安全管理App-domain policy level on top of enterprise/machine/user-App-domain policy level on top of enterprise/machine/user-levels of policy levels of policy Net permissions given to code: intersection of all policy levelsNet permissions given to code: intersection of all policy levelsSQL Server Policy level:SQL Server Policy level:

Maps user/system assemblies to corresponding permission setsMaps user/system assemblies to corresponding permission setsUser assemblies given one of 3 permission sets based on User assemblies given one of 3 permission sets based on user-specificationuser-specification

Different SQL Permissions required for creating CLR permission Different SQL Permissions required for creating CLR permission setset

SAFE: CREATE ASSEMBLY (db level)SAFE: CREATE ASSEMBLY (db level)EA: CREATE ASSEMBLY (db level), EXTERNAL ACCESS EA: CREATE ASSEMBLY (db level), EXTERNAL ACCESS (login level)(login level)UNSAFE: Sysadmin onlyUNSAFE: Sysadmin only

Page 17: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

权限集介绍权限集介绍SAFESAFE EXTERNAL_ACCESSEXTERNAL_ACCESS UNSAFEUNSAFE

CAS permissionsCAS permissions Execute Execute onlyonly

Execute+ access Execute+ access external resourcesexternal resources

UnrestrictedUnrestricted

Programming model Programming model restrictions (no restrictions (no statics, no HPAed statics, no HPAed attributes)attributes)

YesYes YesYes NoNo

Local data accessLocal data access YesYes YesYes YesYes

Verifiability requiredVerifiability required YesYes YesYes NoNo

Page 18: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

外部资源操作与运行权限外部资源操作与运行权限

Page 19: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

SQL CLRSQL CLR 与与 T-SQLT-SQL 的区别的区别

Page 20: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

内容列表内容列表第一部分:概念介绍第一部分:概念介绍第二部分:技术架构第二部分:技术架构 ++ 实战演示实战演示第三部分:监控与查错第三部分:监控与查错

Page 21: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

监控与查错监控与查错Profiler and Performance CountersProfiler and Performance Counters

Profiler trace eventsProfiler trace eventsCLR:Assembly Load – monitor assembly CLR:Assembly Load – monitor assembly load requests (success and failures)load requests (success and failures)SQL:BatchStarting, BatchCompletedSQL:BatchStarting, BatchCompletedSP:Starting, Completed, StmtStarting, SP:Starting, Completed, StmtStarting, StmtCompleted – monitor execution of StmtCompleted – monitor execution of T-SQL and CLR routinesT-SQL and CLR routines

Performance CountersPerformance CountersSQL Server:CLR-CLR Execution – Total SQL Server:CLR-CLR Execution – Total time spent in CLR executiontime spent in CLR execution.NET CLR Memory.NET CLR Memory

Page 22: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

监控与查错监控与查错DMVs and Catalog viewsDMVs and Catalog views

sys.assembly*sys.assembly*sys.dm_os_memory_clerkssys.dm_os_memory_clerkssys.dm_clr*sys.dm_clr*sys.dm_exec_query_statssys.dm_exec_query_statssys.dm_exec_requestssys.dm_exec_requestssys.dm_exec_cached_planssys.dm_exec_cached_plans

Page 23: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

参考网站参考网站“Using CLR Integration” white paper: http://msdn.microsoft.com/library/en-us/dnsql90/html/sqlclrguidance.asp

SQL Server 2005 books online

Stay tuned for more whitepapers on internals and manageability

Send us your feedback: [email protected]

Page 24: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序
Page 25: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序

Your FeedbackYour Feedbackis Important!is Important!

Page 26: DAT321 SQLCLR-- 在 SQL Server 2005 中运行 .NET 程序