SqlException can be thrown if ProcessingEngineTasks database has Default Cursor option set to GLOBAL


Description

If the ProcessingEngineTasks database has the Default Cursor option set to GLOBAL, an SqlException may be thrown during the update of a task when the prerequisite task has failed.
Tasks Database Screenshot

The following exception can be found in the Sitecore XP log:

System.Data.SqlClient.SqlException : A cursor with the name 'DependentTasks' already exists.
The cursor is already open.
A cursor with the name 'DependentTasks' already exists.
  at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__174_0(Task`1 result)
  at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
  at System.Threading.Tasks.Task.Execute()

Solution

In the ProcessingEngineTasks database, modify the UpdateTaskStatusById stored procedure as follows:

Replace this line:

DECLARE [DependentTasks] CURSOR FOR

with this one:

DECLARE [DependentTasks] CURSOR LOCAL FOR