Skip to content

Commit 9e0cbdd

Browse files
committed
alternative: isolation=SERIALIZABLE [chapter_05_aggregate_ends]
1 parent 99ecc03 commit 9e0cbdd

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎src/allocation/repository.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,5 @@ def add(self, product):
2222
self.session.add(product)
2323

2424
def get(self, sku):
25-
return self.session.query(model.Product) \
26-
.filter_by(sku=sku) \
27-
.with_for_update() \
28-
.first()
25+
return self.session.query(model.Product).filter_by(sku=sku).first()
2926

‎src/allocation/unit_of_work.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def products(self) -> repository.AbstractRepository:
3535

3636
DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine(
3737
config.get_postgres_uri(),
38+
isolation_level="SERIALIZABLE",
3839
))
3940

4041
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):

‎tests/integration/test_uow.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class MyException(Exception):
7575
import traceback
7676
import time
7777

78-
def try_to_allocate(orderid, sku, session_factory, exceptions):
78+
def try_to_allocate(orderid, sku, exceptions):
7979
line = model.OrderLine(orderid, sku, 10)
8080
try:
81-
with unit_of_work.start(session_factory) as uow:
81+
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
8282
product = uow.products.get(sku=sku)
8383
product.allocate(line)
8484
time.sleep(0.2)
@@ -88,16 +88,16 @@ def try_to_allocate(orderid, sku, session_factory, exceptions):
8888
exceptions.append(e)
8989

9090

91-
def test_integrity_of_concurrent_updates_to_product_version(postgres_session_factory):
91+
def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory):
9292
sku, batch = random_ref('s'), random_ref('b')
9393
session = postgres_session_factory()
9494
insert_batch(session, batch, sku, 100, eta=None, product_version=3)
9595
session.commit()
9696

9797
exceptions = []
9898
o1, o2 = random_ref('o1'), random_ref('o2')
99-
target1 = lambda: try_to_allocate(o1, sku, postgres_session_factory, exceptions)
100-
target2 = lambda: try_to_allocate(o2, sku, postgres_session_factory, exceptions)
99+
target1 = lambda: try_to_allocate(o1, sku, exceptions)
100+
target2 = lambda: try_to_allocate(o2, sku, exceptions)
101101
t1 = threading.Thread(target=target1)
102102
t2 = threading.Thread(target=target2)
103103
t1.start()
@@ -109,15 +109,18 @@ def test_integrity_of_concurrent_updates_to_product_version(postgres_session_fac
109109
"SELECT version_number FROM products WHERE sku=:sku",
110110
dict(sku=sku),
111111
)
112-
assert version == 5
113-
assert exceptions == []
112+
assert version == 4
113+
exception = [exceptions]
114+
assert 'could not serialize access due to concurrent update' in str(exception)
115+
114116
orders = list(session.execute(
115117
"SELECT orderid FROM allocations"
116118
" JOIN batches ON allocations.batch_id = batches.id"
117119
" JOIN order_lines ON allocations.orderline_id = order_lines.id"
118120
" WHERE order_lines.sku=:sku",
119121
dict(sku=sku),
120122
))
121-
orders = set([orders[0][0], orders[1][0]])
122-
assert orders == {o1, o2}
123+
assert len(orders) == 1
124+
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
125+
uow.session.execute('select 1')
123126

0 commit comments

Comments
 (0)