This is a migrated thread and some comments may be shown as answers.

Stored procedure with Mutex lock

1 Answer 107 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ashley
Top achievements
Rank 1
Ashley asked on 20 Aug 2013, 10:57 AM
Hi,

I have a stored procedure I'm trying to run using:
IEnumerable<int> actualResults = this.DataSource.Context.ExecuteQuery<int>("spCore_AllocateWorkItem", System.Data.CommandType.StoredProcedure,
                docNoParam,
                docTableParam,
                clientHandleParam,
                userIdParam,
                computerIdParam,
                moduleIdParam,
                serverIdParam,
                whereParam,
                orderParam,
                maximumResultsParam,
                reconnectCodeParam);

Inside the stored proc I try to acquire a Mutex lock with:
EXEC @Result = sys.sp_getapplock
                                    @Resource = 'WorkAllocator',
                                    @LockMode = 'Exclusive',
                                    @LockOwner = 'Session',
                                    @LockTimeout = 20000
    IF @Result NOT IN (0,1)
    BEGIN
        RAISERROR ('Unable to aquire lock', 16,1)
    END

When I attempt to run this proc in SQL everything works fine. If I try to run this procedure using OpenAccess I get an error raised with the "Unable to acquire lock" message. Could you please let me know why I cannot acquire the lock and how to achieve this with OpenAccess?

Regards,
Ash

1 Answer, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 21 Aug 2013, 11:03 AM
Hello Ash,

I can confirm this strange behavior. it is not related to OpenAccess and you can simulate it even in the SQL Server Management Studio if you try to execute your stored procedure. It should give you the same error.  

I tested with the following stored procedure and it works:

ALTER PROCEDURE [dbo].[TestAppLock]
     
AS
BEGIN
    declare @Result int
    EXEC @Result = sys.sp_getapplock
                    @Resource = 'AnotherResoruceKey',
                    @LockMode = 'Exclusive',
                    @LockOwner = 'Session',
                    @LockTimeout = 2000
    IF @Result NOT IN (0,1)
    BEGIN
        print @Result
        RAISERROR ('Unable to aquire lock', 16,1)
    END
    ELSE
    BEGIN
        EXEC @Result = sys.sp_releaseapplock
                    @Resource = 'AnotherResoruceKey',
                    @LockOwner = 'Session'
    END
    return 33
END


It seems that there is something to do with the resource name = WorkAllocator. When try with this value you will see that the sys.sp_getapplock stored procedure will report result value -1 which means 'The lock request timed out.'  This could happen if there is such lock already.

If you use different key it works. This could be related to the lock retrieval/storage mechanism. Sql Server hashes the resource value and store it.

To troubleshoot the issue you could find the system table holding the lock objects and try to found out who has created it.

I hope this helps.

Regards,
Kaloyan Nikolov
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Tags
Development (API, general questions)
Asked by
Ashley
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Share this question
or