private async Task <CompleteCriticalSectionResponse> ReturnCriticalSectionTokenAsync(TaskId taskId, int taskDefinitionId, string taskExecutionId, CriticalSectionType criticalSectionType)
        {
            var response = new CompleteCriticalSectionResponse();

            using (var connection = await CreateNewConnectionAsync(taskId).ConfigureAwait(false))
            {
                SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable);

                var command = connection.CreateCommand();
                command.Transaction = transaction;

                if (criticalSectionType == CriticalSectionType.User)
                {
                    command.CommandText = TokensQueryBuilder.ReturnUserCriticalSectionTokenQuery;
                }
                else
                {
                    command.CommandText = TokensQueryBuilder.ReturnClientCriticalSectionTokenQuery;
                }

                command.CommandTimeout = ConnectionStore.Instance.GetConnection(taskId).QueryTimeoutSeconds;
                command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId;
                command.Parameters.Add("@TaskExecutionId", SqlDbType.Int).Value  = int.Parse(taskExecutionId);

                try
                {
                    await command.ExecuteNonQueryAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (SqlException sqlEx)
                {
                    TryRollBack(transaction, sqlEx);
                }
                catch (Exception ex)
                {
                    TryRollback(transaction, ex);
                }
            }

            return(response);
        }
        private CompleteCriticalSectionResponse ReturnCriticalSectionToken(TaskId taskId, int taskDefinitionId, string taskExecutionId, CriticalSectionType criticalSectionType)
        {
            var response = new CompleteCriticalSectionResponse();

            using (var connection = CreateNewConnection(taskId))
            {
                SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable);

                var command = connection.CreateCommand();
                command.Transaction = transaction;

                if (criticalSectionType == CriticalSectionType.User)
                    command.CommandText = TokensQueryBuilder.ReturnUserCriticalSectionTokenQuery;
                else
                    command.CommandText = TokensQueryBuilder.ReturnClientCriticalSectionTokenQuery;

                command.CommandTimeout = ConnectionStore.Instance.GetConnection(taskId).QueryTimeoutSeconds;
                command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId;
                command.Parameters.Add("@TaskExecutionId", SqlDbType.Int).Value = int.Parse(taskExecutionId);

                try
                {
                    command.ExecuteNonQuery();
                    transaction.Commit();
                }
                catch (SqlException sqlEx)
                {
                    TryRollBack(transaction, sqlEx);
                }
                catch (Exception ex)
                {
                    TryRollback(transaction, ex);
                }
            }

            return response;
        }