How does a database deal with concurrency?
I've always thought about the following scenario:
Person A works with data at the same time as Person B (who is also working with the same data). They both commit at the same time and would have manipulated different elements of the data, so overwriting isn't the case here.
How would the database deal with this?
Comments
One can use locking, which is one of the traditional ways of handling this. Two-phase commit is also a classic. There's a slight error in your question, I believe - if person A and person B are working with the _same_ data, that's a problem, but they can work in the same _database_ without stepping on each other's toes.
Databases these days have super powerful algorithms to deal with locks. An example is the MS SQL Server and all its types of locks you can apply to your datasets. Normally there are two main categories of locks; the Pessimestic and the Optomestic. The Pessimistic prevents any other users from accessing the locked data once it is locked, while the optomistic will allow the other users to view and update data, however it will allow only the first user attempting a save to save the changes and all the other changes will be discarded.