Answered by:
Plugin to Call Stored Procedure Timing Out

Question
-
CRM 2011 on premise, UR3; SQL Server is on Same Box
I am executing a simple plugin to call a stored procedure to do some counting. The stored procedure will execute fine from SQL management studio and the c# code will execute just fine from a windows form application.
But when the code is imbedded and called from a plugin it times out. I have used SQL profiler and can see the execute being called, but it times out. Here is the error, code snippet follows.
====================
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the StoredProc plug-in.System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.===================
myConn = new SqlConnection(@"Data Source=xxxServerName;Initial Catalog=xxxxxxx_MSCRM;Integrated Security=True");
myConn.Open();
SqlCommand cmd = new SqlCommand("sp_UpdateCounts", myConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
Wednesday, September 28, 2011 2:40 PM
Answers
-
It is not supported to call stored procedures on the CRM database from a plugin. You might be running into deadlocks or other problems, when you are inside the plugin there might be some tables locked. In general, calling SQL directly from the plugin can have multiple side effects and is something you should avoid because each time you apply an Update Rollup or a n upgrade, your customizations will be at risk of stopping to work or failing the update/upgrade.
In another note, plugins have a 1-2 timeout, so unless you can guarantee that your stored procedure will finish in that amount of time, you should not use plugins. Only workflows (and custom workflow activities) can handle long-running requests.
Gonzalo | gonzaloruizcrm.blogspot.com
- Proposed as answer by Gonzalo Ruiz RModerator Wednesday, September 28, 2011 3:07 PM
- Marked as answer by Kurt Reynolds Wednesday, September 28, 2011 5:10 PM
Wednesday, September 28, 2011 3:07 PMModerator
All replies
-
It is not supported to call stored procedures on the CRM database from a plugin. You might be running into deadlocks or other problems, when you are inside the plugin there might be some tables locked. In general, calling SQL directly from the plugin can have multiple side effects and is something you should avoid because each time you apply an Update Rollup or a n upgrade, your customizations will be at risk of stopping to work or failing the update/upgrade.
In another note, plugins have a 1-2 timeout, so unless you can guarantee that your stored procedure will finish in that amount of time, you should not use plugins. Only workflows (and custom workflow activities) can handle long-running requests.
Gonzalo | gonzaloruizcrm.blogspot.com
- Proposed as answer by Gonzalo Ruiz RModerator Wednesday, September 28, 2011 3:07 PM
- Marked as answer by Kurt Reynolds Wednesday, September 28, 2011 5:10 PM
Wednesday, September 28, 2011 3:07 PMModerator -
Thanks for the quick reply. I understand that stored procedures are not guaranteed to upgrade, but some things just can't be done inside the framework very efficiently, in some cases not at all.
Many thanks
Kurt
kurt.reynolds@microsoft.comWednesday, September 28, 2011 5:12 PM