So.. I think I went about my question wrong last time so I am revisiting this with something way more focused and see if anyone can enlighten me.
I have the following two transactions going on at the same time by different clients.
Revert
--Ex: Asset 1234, version 2 which has a metadata id of 4321
-- @asset_id = 1234
-- @metadata_id = 4321
-- @edit_lock = 1
DELETE FROM Dependencies
WHERE metadata_id = @metadata_id
DELETE FROM FileDependencies
WHERE metadata_id = @metadata_id
--Delete this last since all foreign references refer to him
DELETE FROM AssetMetaData WHERE metadata_id = @metadata_id
Save
-- Ex: Asset: 6789, version 3 which is metadata_id: 9876
-- @metadata_id = 9876
UPDATE AssetMetaData
SET name = 'name', more_data = 'data'
WHERE metadata_id = @metadata_id
--Clean us out so we dont need to figure out what is new, what is changed, what is deleting, just start fresh
DELETE FROM Dependencies
WHERE metadata_id = @metadata_id
--Called once per dependency
INSERT INTO Dependencies (metadata_id, dependency_id)
@metadata_id, @dependency_id
--Clean us out so we dont need to figure out what is new, what is changed, what is deleting, just start fresh
DELETE FROM FileDependencies
WHERE metadata_id = @metadata_id
--Called once per dependency
INSERT INTO FileDependencies (metadata_id, file_id)
@metadata_id, @file_id
Now, I happen to know these will never be accessing the same rows, we have other means to prevent that, these actually have some checks around them to ensure that.
My issue is that they are in a different order (and I will be fixing the order but I am looking for a more robust solution) and we end up with the save with a row lock on the metadata table, and the revert with a row lock (or two) on the dependencies table. The revert then goes to delete the row from the metadata table, but within the "Delete From AssetMetaData WHERE metadata_id = @metadata_id" it attempts to get a Update lock, presumably on the same page that the Save has a row lock and, vice versa with the save accessing the dependencies table and I deadlock.
My question is.. without reordering things is there a way to make it so that this works? Like I said I know with certainty that are editing/deleting/whatever different rows.
I have the following two transactions going on at the same time by different clients.
Revert
--Ex: Asset 1234, version 2 which has a metadata id of 4321
-- @asset_id = 1234
-- @metadata_id = 4321
-- @edit_lock = 1
DELETE FROM Dependencies
WHERE metadata_id = @metadata_id
DELETE FROM FileDependencies
WHERE metadata_id = @metadata_id
--Delete this last since all foreign references refer to him
DELETE FROM AssetMetaData WHERE metadata_id = @metadata_id
Save
-- Ex: Asset: 6789, version 3 which is metadata_id: 9876
-- @metadata_id = 9876
UPDATE AssetMetaData
SET name = 'name', more_data = 'data'
WHERE metadata_id = @metadata_id
--Clean us out so we dont need to figure out what is new, what is changed, what is deleting, just start fresh
DELETE FROM Dependencies
WHERE metadata_id = @metadata_id
--Called once per dependency
INSERT INTO Dependencies (metadata_id, dependency_id)
@metadata_id, @dependency_id
--Clean us out so we dont need to figure out what is new, what is changed, what is deleting, just start fresh
DELETE FROM FileDependencies
WHERE metadata_id = @metadata_id
--Called once per dependency
INSERT INTO FileDependencies (metadata_id, file_id)
@metadata_id, @file_id
Now, I happen to know these will never be accessing the same rows, we have other means to prevent that, these actually have some checks around them to ensure that.
My issue is that they are in a different order (and I will be fixing the order but I am looking for a more robust solution) and we end up with the save with a row lock on the metadata table, and the revert with a row lock (or two) on the dependencies table. The revert then goes to delete the row from the metadata table, but within the "Delete From AssetMetaData WHERE metadata_id = @metadata_id" it attempts to get a Update lock, presumably on the same page that the Save has a row lock and, vice versa with the save accessing the dependencies table and I deadlock.
My question is.. without reordering things is there a way to make it so that this works? Like I said I know with certainty that are editing/deleting/whatever different rows.