什么是SQL Server的CLR集成生命周期? [英] What is the SQL Server CLR Integration Life Cycle?

查看:239
本文介绍了什么是SQL Server的CLR集成生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何CLR(.NET)对象SQL Server管理的?

How are CLR (.NET) objects managed in SQL Server?

的切入点,从SQL Server的任何CLR code是一个静态方法。通常你只会创建方法的范围内存在的对象。但是,你可以想见,存储引用静态成员的对象,让他们逃脱方法调用范围。如果SQL Server保留在跨多个存储过程/函数调用存储这些对象,那么他们可能是有用的缓存应用程序 - 尽管他们会更危险的太

The entry point to any CLR code from SQL Server is a static method. Typically you'll only create objects that exist within the scope of that method. However, you could conceivably store references to objects in static members, letting them escape the method call scope. If SQL Server retains these objects in memory across multiple stored procedure/function calls, then they could be useful for caching applications -- although they'd be more dangerous too.

SQL Server如何看待这一点?它甚至允许(非法)静态成员?如果是的话,多长时间保留在内存中?它是否垃圾收集CLR每次通话后一切吗?它是怎样处理的并发?

How does SQL Server treat this? Does it even allow (non-method) static members? If so, how long does it retain them in memory? Does it garbage collect everything after every CLR call? How does it handle concurrency?

推荐答案

在专业版的SQL Server 2005年大会由Robin Dewson和朱利安·斯金纳,它说,组件加载到数据库中,像其他数据库对象,通过所拥有一个数据库用户。通过在同一数据库相同的用户拥有的所有组件将相同的AppDomain内运行。装配由不同用户拥有的将在一个单独的AppDomain运行。

In "Pro SQL Server 2005 Assemblies" by Robin Dewson and Julian Skinner, it says that "Assemblies loaded into a database, like other database objects, are owned by a database user. All assemblies owned by the same user in the same database will run within the same AppDomain. Assemblies owned by a different user will run within a separate AppDomain."

这告诉我的是,如果你正在使用一个单一的数据库,所有的组件,你在用CREATE ASSEMBLY语句装载有相同的所有者,那么你的组件将全部在同一个应用程序域中运行。然而,在相同的应用程序域是不意味着使用相同的$ C $的c-基,所以即使是相同的dll可以装入同一应用程序域多次,并且它的类型不匹配时,即使它们具有相同的名称。当同名类型从不同的code-基地,它们的静态变量将是不同的情况下也是如此。

What this tells me is that if you're working with a single database and all the assemblies you load in with the CREATE ASSEMBLY statement have the same owner, then your assemblies will all run in the same app domain. However, being in the same AppDomain does not mean using the same code-base, so even the same dll can be loaded into the same application domains multiple times, and it's types will not match up, even though they have the same name. When same-named types are from different code-bases, their static variables will be different instances as well.

我可以看到在SQL Server CLR环境与多个组件安全地使用静态变量的唯一方法是实际只使用一个单一的组件。您可以使用ILMerge实用程序与UnionMerge选项收拾你所有的程序集为一体,并融合类具有相同的名称。这应该保证,对于给定的数据库,在你的鞋底组件,静态变量将正常工作,就像他们在一个独立的应用程序。我认为它是安全的假设应用程序域不卸载并重新加载在每个请求,但你不能依赖它永远不会被卸载,因为这会发生,每当有未处理的错误(至少如果它在不安全的模式下运行)。

The only way I can see to use static variables safely in SQL Server CLR environment with multiple assemblies is to actually just use a single assembly. You can use the ILMerge utility with the "UnionMerge" option to pack all your assemblies into one and merge classes with the same name. This should guarantee that for a given database, in your sole assembly, your static variables will work just like they would in a stand-alone application. I think it's safe to assume the application domain isn't unloaded and reloaded at every request, but you can't depend on it never being unloaded, since that will happen whenever there is an unhandled error (at least if it's running in unsafe mode).

这篇关于什么是SQL Server的CLR集成生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆