What is the SQL Server CLR Integration Life Cycle?

前端 未结 5 786
[愿得一人]
[愿得一人] 2020-12-31 20:38

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

The entry point to any CLR code from SQL Server is a static method. Typically you\'ll only create objects that exi

5条回答
  •  庸人自扰
    2020-12-31 20:57

    SQL Server allows static readonly members if assembly is deployed with Unsafe permission level.

    Practically objects are retained in memory until SQL service is stopped/restarted.

    Regarding concurrency, your object and methods should be thread-safe as everywhere else.

    For example:

    public static class MyCLRClass
    {
        private static readonly ReaderWriterLock rwlock = new ReaderWriterLock();
        private static readonly ArrayList list = new ArrayList();
    
        private static void AddToList(object obj)
        {
            rwlock.AcquireWriterLock(1000);
            try
            {
                list.Add(obj);
            }
            finally
            {
                rwlock.ReleaseLock();
            }
        }
    
        [SqlProcedure(Name="MyCLRProc")]
        public static void MyCLRProc()
        {
            rwlock.AcquireReaderLock(1000);
            try
            {
                SqlContext.Pipe.Send(string.Format("items in list: {0}", list.Count));
            }
            finally
            {
                rwlock.ReleaseLock();
            }
        }
    }
    

    I use such things in SQL CLR and it works.

提交回复
热议问题