"Cannot change thread mode after it is set" on sp_OACreate execution

1.1k Views Asked by At

I have the following stored procedure that uses the sp_OACreate stored procedure to call APIs:

CREATE PROCEDURE Reports.P_CallAPI_Custom 
        @Url NVARCHAR(500),
        @requestHeaderKey NVARCHAR(100),
        @requestHeaderValue NVARCHAR(512),
        @messageRequest NVARCHAR(MAX),
        @messageResponse NVARCHAR(MAX) OUTPUT
    
AS
BEGIN
    
    DECLARE @APIResponse as TABLE(APIResponseValue NVARCHAR(MAX));
    DECLARE @Object AS INT;
    DECLARE @hr AS INT;
    DECLARE @source varchar(255);  
    DECLARE @description varchar(255);
        
    -- this call errors randomly:
    EXEC @hr = sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', @Object OUT;
        
    IF @hr = 0 AND @Object IS NOT NULL
    BEGIN
        EXEC @hr = sp_OAMethod @Object, 'open', NULL, 'post',
                        @Url, --Web Service Url (invoked)
                        'false'
        IF @hr = 0
            EXEC sp_OAMethod @Object, 'setRequestHeader',
                null,@requestHeaderKey,@requestHeaderValue
    
        IF @hr = 0
            EXEC sp_OAMethod @Object, 'setRequestHeader',
                null,'Content-type','application/json'
    
        IF @hr = 0
            EXEC @hr=sp_OAMethod @Object, 'send', null, @messageRequest 
    
        IF @hr = 0
        BEGIN
    
            DECLARE @status NVARCHAR(32);
            DECLARE @statusText NVARCHAR(32);
            EXEC @hr = sp_OAGetProperty @Object, 'status', @status OUT;
            EXEC @hr = sp_OAGetProperty @Object, 'statusText', @statusText OUT;
            
            IF @status =  '200'
            BEGIN
                INSERT INTO @APIResponse (APIResponseValue)
                EXEC sp_OAGetProperty @Object, 'responseText';

                SELECT @messageResponse = APIResponseValue
                FROM @APIResponse;
            END
            ELSE
            BEGIN
                SELECT @messageResponse = 'Warning: Unauthorized. Status: '
                    +  @status + ' (' + @statusText + ')'   
                RETURN
            END
        END
        
        EXEC sp_OADestroy @Object;
    END

    IF @hr <> 0 
    BEGIN
        EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT;  
        IF @hr = 0  
        BEGIN  
            SELECT @messageResponse = 'Error:' + @source + '.' + @description 
            RETURN
        END  
        ELSE  
        BEGIN  
            SELECT @messageResponse = 'Error: sp_OAGetErrorInfo failed.' 
            RETURN  
        END
    END
END;

This procedure is being called by another stored procedure,
which in turn is being called by a report (SSRS).

Quite randomly (it works more than it fails),
I get the following error:

Cannot change thread mode after it is set

I narrowed down the issue and confirmed that the error is in the sp_OACreate execution,
but I couldn't find any useful help online regarding this error.

0

There are 0 best solutions below