Skip to main content
gamedev.net
🔒 Locked

MSSQL, SQL Transactions

Started by Thevenin Aug 3, 2007 at 8:22 PM 1 replies 500+ views
Original Post
Thevenin
Thevenin
I cannot quite get my mind around SQL Transactions in MSSQL, so I'm just going to ask if this scenario is possible. Two users are posting on a forum: Adam , and Eve. Posting on said thread requires grabbing the LAST_TOPIC_ID, incrementing and saving it, and then of course, using the value in the POST that is to be entered into TABLE_POSTS. Now, Adam and Eve are posting at the EXACT same time. Their posts requests get sent to an MSSQL Stored Procedure that does the above, but encapsulated in a "BEGIN TRANSACTION {sql statements here} COMMIT". Without the TRANSACTION, it is apparent that there is a race-condition here, and that one post could overwrite the other (or worse). But it's not clear to me whether the TRANSACTION is going to prevent the race-condition. The point of transactions appear to be to reverse things when detectable errors occur, but detecting a race-condition is, in itself, subject to a race-condition. So, someone explain this to me... lol
Antheus
Antheus
Transaction locks the database (depends, but it can). As such, entire transaction is atomic, and cannot be interrupted - there is no race condition, as long as the entire sequence of operations is performed as a transaction.

The fact that they are posting at the same time only leads to degraded performance, but does not affect integrity of the data.

Transactions roll-back is unrelated to this, it's merely an automated failure handling mechanism.
Thevenin
Thevenin
Thank you, my good man!

That's the confirmation I couldn't get on #gamedev.

ratings+=4;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.