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
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.