Middle
What is a scope statement?
sobes.tech AI
Answer from AI
A lock in a database is a mechanism that controls concurrent access to shared data to prevent conflicts and ensure data integrity during parallel transactions.
When one transaction locks data, other transactions trying to access the same data will wait until the lock is released.
That is, a lock involves:
- Lock state (locked/unlocked).
- Waiting mechanism (e.g., queue of threads waiting for the lock to be released).
In C#, the lock keyword works with an object that acts as a lock marker. Example:
private readonly object _lockObj = new object();
void CriticalSection()
{
lock(_lockObj)
{
// Code that should be executed only by one thread at a time
}
}
Here, _lockObj is used to manage access. Internally, the CLR uses this object to control the lock state.