why deadlock?

  • I have a stored procedure that does this:

    create procedure MyProcedure

    @orderid int

    as

    begin tran

    insert into tblMytable select @orderid, getdate()

    ......

    .......

    .........

    delete from tblMytable with (rowlock) where orderid = @orderid

    commit

    whenever two processes running at the same time, with/without the same @orderid, Running profiler shows the delete statement is where they run into deadlock, the lock resource is on rid of tblMytable. They both shows owning U lock and requesting X lock.

    I dont get it. Could someone please explain it to me? Why there is U lock?

    Thanks!

  • Do you have index on orderid? If not, tablescan can try to access the same row in both transactions I think, leading to deadlock.

    Piotr

    ...and your only reply is slàinte mhath

  • Thanks!

    I do not have index on orderid, let me add it and see it fix it.

  • It works now with the index! Thanks for the help!

  • What if you remove BEGIN TRAN and COMMIT?

  • Sorry it has to be wrapped up in one transaction.

    Thanks for the reply.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply