Skip to content

Commit abe16b7

Browse files
committed
adding code an README
1 parent 339507f commit abe16b7

File tree

9 files changed

+168
-1
lines changed

9 files changed

+168
-1
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/writing-locking.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
# database-concurrency-control
1+
# Database Concurrency Control Examples
2+
3+
This repository contains examples of implementing concurrency control mechanisms in Python for managing concurrent access to a shared database. Two commonly used locking mechanisms, pessimistic locking and optimistic locking, are demonstrated in separate code examples.
4+
5+
## Pessimistic Locking Example
6+
7+
The `pessimistic-locking.py` file demonstrates how to implement pessimistic locking in Python using the `threading.Lock` object. Pessimistic locking ensures exclusive access to a shared resource by acquiring a lock before performing write operations. In the example, the `update_user_record` function represents the code that updates a user record in a database. The `Lock` object is used to synchronize access to the user record, allowing only one thread to modify it at a time. This ensures data consistency and prevents conflicts.
8+
9+
To run the pessimistic locking example, execute the following command:
10+
```
11+
python pessimistic-locking.py
12+
```
13+
14+
## Optimistic Locking Example
15+
16+
The `optimistic-locking.py` file demonstrates how to implement optimistic locking in Python using versioning. Optimistic locking assumes that conflicts are rare and allows concurrent access to a shared resource. In the example, the `update_user_record` function represents the code that updates a user record in a database. It reads the current version of the user record and compares it with the stored version. If the versions match, the update operation proceeds; otherwise, a conflict is detected. This approach minimizes the need for locking and allows for better concurrency while still ensuring data integrity.
17+
18+
To run the optimistic locking example, execute the following command:
19+
```
20+
python optimistic-locking.py
21+
```
22+
23+
## Requirements
24+
25+
The code examples require Python 3.x.

optimistic-locking.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import threading
2+
import time
3+
4+
# Assume this is the user record in the database
5+
user = {
6+
'id': 1,
7+
'name': 'John Doe',
8+
'email': 'johndoe@example.com',
9+
'age': 30,
10+
'version': 1 # Version number for optimistic locking
11+
}
12+
13+
14+
# Function to update the user record
15+
def update_user_record(user_id, new_name):
16+
print(f"Thread {threading.current_thread().name} is attempting to update the user record.")
17+
18+
# Simulate some processing time
19+
time.sleep(2)
20+
21+
# Read the current version of the user record
22+
current_version = user['version']
23+
24+
# Simulate some more processing time
25+
time.sleep(2)
26+
27+
# Perform the update operation only if the version hasn't changed
28+
if current_version == user['version']:
29+
# Update the user record
30+
user['name'] = new_name
31+
user['version'] += 1
32+
33+
print(f"Thread {threading.current_thread().name} has updated the user record: {user}")
34+
else:
35+
print(
36+
f"Thread {threading.current_thread().name} detected a conflict. The user record has been updated by another thread.")
37+
38+
39+
# Create two threads that update the user record simultaneously
40+
thread1 = threading.Thread(target=update_user_record, args=(1, 'Jane Smith'))
41+
thread2 = threading.Thread(target=update_user_record, args=(1, 'Alice Johnson'))
42+
43+
# Start the threads
44+
thread1.start()
45+
thread2.start()
46+
47+
# Wait for the threads to finish
48+
thread1.join()
49+
thread2.join()
50+
51+
# Print the final user record
52+
print(f"Final user record: {user}")

pessimistic-locking.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import threading
2+
import time
3+
4+
# Assume this is the user record in the database
5+
user = {
6+
'id': 1,
7+
'name': 'John Doe',
8+
'email': 'johndoe@example.com',
9+
'age': 30
10+
}
11+
12+
# Create a lock object to synchronize access to the user record
13+
lock = threading.Lock()
14+
15+
16+
# Function to update the user record
17+
def update_user_record(user_id, new_name):
18+
print(f"Thread {threading.current_thread().name} is attempting to update the user record.")
19+
20+
# Acquire the lock to ensure exclusive access to the user record
21+
lock.acquire()
22+
print(f"Thread {threading.current_thread().name} has acquired the lock.")
23+
24+
try:
25+
# Simulate some processing time
26+
time.sleep(2)
27+
28+
# Perform the update operation
29+
user['name'] = new_name
30+
print(f"Thread {threading.current_thread().name} has updated the user record: {user}")
31+
32+
# Simulate some more processing time
33+
time.sleep(2)
34+
35+
finally:
36+
# Release the lock to allow other threads to access the user record
37+
lock.release()
38+
print(f"Thread {threading.current_thread().name} has released the lock.")
39+
40+
41+
# Create two threads that update the user record simultaneously
42+
thread1 = threading.Thread(target=update_user_record, args=(1, 'Jane Smith'))
43+
thread2 = threading.Thread(target=update_user_record, args=(1, 'Alice Johnson'))
44+
45+
# Start the threads
46+
thread1.start()
47+
thread2.start()
48+
49+
# Wait for the threads to finish
50+
thread1.join()
51+
thread2.join()
52+
53+
# Print the final user record
54+
print(f"Final user record: {user}")

0 commit comments

Comments
 (0)