SQL CLR awaitable not getting executed

半世苍凉 提交于 2019-12-19 12:22:40

问题


It is my understanding that the awaitable completes the remaining part of the executing code when they return back from the wait. I am trying to get this to work in an sql clr, and this is not working as both awaited process and codes beneath it are not getting executed. In debug method, the control just returns after executing the await line.

how can i make this work, as the requirement is for clr to be executed without blocking the main thread so that other work can continue well in the db. I really need this to work, and have been on it for 2 days now.

NB This works well if method is sync. And the stored proc assemble is registered as unsafe in sql server. I am using mssql 2012 and visual studio 2015.

public partial class StoredProcedures
{
   [Microsoft.SqlServer.Server.SqlProcedure]
   public static async void sp_push_stock_update(SqlString transactionPrimaryKey, SqlString transactionType)
   {

       using (SqlConnection conn = new SqlConnection("context connection=true"))
       {

           StockUpdateClient.stock_fetcher _stockFetcher = new StockUpdateClient.stock_fetcher(conn);

           List<String> skuList = await new 
           System.Threading.Tasks.Task<List<string>>(()=> 
           _stockFetcher.getItems(transactionPrimaryKey, transactionType));

           //Code does not get here
           performLogging(skuList):


        }

    }
}

回答1:


the requirement is for clr to be executed without blocking the main thread

Have you tried without the threading and experienced that it did indeed block the thread? It is possible that you are over-complicating this. Still, some options available are:

  1. Surround the potentially long-running call with Thread.BeginThreadAffinity() and Thread.EndThreadAfinity() as recommended in this comment.

  2. Do multi-threading in what amounts to the "old-fashioned" way by creating a new Thread that handles the long-running call, and wait for it to complete in a loop that calls thread.sleep(x);:

    Thread _RunYouLongTime = new Thread(delegate);
    _RunYouLongTime.Start();
    while (_RunYouLongTime.IsAlive)
    {
      System.Threading.Thread.Sleep(100);
    }
    
  3. If the long-running call is a HttpRequest / Web Service, increase the ServicePointManager.DefaultConnectionLimit Property. This should probably be done regardless of how, or even if, you do threading!



来源:https://stackoverflow.com/questions/39735746/sql-clr-awaitable-not-getting-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!