我有一个查询导致表的主键死锁。查询可以从多个不同的代码路径运行。每次需要运行查询时都会创建新的数据库连接,因此,就我所知,这不是连接重用问题。
有问题的查询是
CREATE PROCEDURE [dbo].[upd_task] @taskid INTEGER
, @state INTEGER
, @args NVARCHAR( max )
, @retries INTEGER
, @started bit
, @finished bit
, @username NVARCHAR( 50 ) AS BEGIN BEGIN TRAN
UPDATE task WITH (updlock) SET
[state] = @state
, args = @args
, retries = @retries
, started = @started
, finished = @finished
, modifier = @username
, modified = GETDATE()
WHERE
task.task_id = @taskid COMMIT END
该表定义为
CREATE TABLE [dbo].[task](
[task_id] [bigint] IDENTITY(1,1) NOT NULL,
[job_id] [bigint] NOT NULL,
[car_id] [int] NOT NULL,
[uuid] [nvarchar](60) NULL,
[user_priority] [bit] NOT NULL,
[args] [nvarchar](max) NULL,
[state] [int] NOT NULL,
[started] [bit] NOT NULL,
[finished] [bit] NOT NULL,
[failed] [bit] NOT NULL,
[error_msg] [nvarchar](max) NULL,
[retries] [int] NULL,
[exec_time] [datetime] NULL,
[created] [datetime] NULL,
[creator] [nvarchar](50) NULL,
[modified] [datetime] NULL,
[modifier] [nvarchar](50) NULL,
CONSTRAINT [PK_task_id] PRIMARY KEY CLUSTERED
(
[task_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [user_priority]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [state]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [started]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [finished]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [failed]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [retries]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_car] FOREIGN KEY([car_id])
REFERENCES [dbo].[car] ([car_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_car]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_job] FOREIGN KEY([job_id])
REFERENCES [dbo].[job] ([job_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_job]
GO