Running SQL Server 2014
. I have a stored procedure where I need to write data to a global temporary table. However, it seems that the method OBJECT_ID
hangs when the procedure is called inside a transaction.
How do I remove the deadlock without removing the transaction?
Stored procedure:
CREATE PROCEDURE [dbo].[foo]
@Data [varbinary](max)
WITH ENCRYPTION AS
BEGIN
SET NOCOUNT ON
IF OBJECT_ID('tempdb..##TempData') IS NULL
CREATE TABLE ##TempData
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Data] [int] NOT NULL UNIQUE
)
INSERT INTO ##TempData ([Data]) SELECT @Data
END
GO
First connection:
BEGIN TRAN
EXEC [foo] @Data = 1
WAITFOR DELAY '00:00:20'
COMMIT TRAN
DROP TABLE ##TempData
Then in second connection:
BEGIN TRAN
EXEC [foo] @Data = 2 -- This will hang on the call to OBJECT_ID()...
COMMIT TRAN
UPDATE
Here is some C#
code that illustrates why I need to have a transaction.
var options = new TransactionOptions();
// options.IsolationLevel = IsolationLevel.ReadCommitted;
options.Timeout = TransactionManager.MaximumTimeout;
using (var transaction = new TransactionScope(TransactionScopeOption.Required, options))
{
// Write to ##TempData. Lengthy operation...
var foo = Foo(data);
// These two MUST be inside a transaction!
var bar = Bar();
var baz = Baz(); // Consume data in ##TempData
transaction.Complete();
}