Skip to content

Commit 23640a1

Browse files
committed
Change version to 0.1.2
1 parent 4a56ee2 commit 23640a1

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

README.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ pip install postgres-lock
3232

3333
### Default operation
3434

35-
By default `postgres-lock` will use `session` lock scope in `blocking` mode. The `session` lock scope
36-
means only a single database connection can acquire the lock at a time.
35+
By default `postgres-lock` will use `session` lock scope in `blocking` mode with
36+
`rollback_on_error` enabled. The `session` lock scope means only a single database connection can
37+
acquire the lock at a time.
3738

3839
### Usage
3940

4041
All work revolves around the `Lock` class.
4142

4243
The easiest way to use `Lock` is with `with` or `async with` statements. The lock will be
43-
released automatically.
44+
released automatically. If `rollback_on_error` is enabled (default), rollbacks are automatically
45+
handled prior to release.
4446

4547
_Using `with` and `async with` implies blocking mode._
4648

@@ -74,7 +76,15 @@ try:
7476

7577
print("Acquired lock!")
7678

77-
# do something here
79+
try:
80+
# do something here
81+
pass
82+
83+
except Exception as exc:
84+
# handle_error() will rollback the transaction by default
85+
lock.handle_error(exc)
86+
87+
raise exc
7888
finally:
7989
# release lock (this is safe to run even if the lock has not been acquired)
8090
lock.release()
@@ -97,7 +107,15 @@ try:
97107

98108
print("Acquired lock!")
99109

100-
# do something here
110+
try:
111+
# do something here
112+
pass
113+
114+
except Exception as exc:
115+
# handle_error_async() will rollback the transaction by default
116+
await lock.handle_error_async(exc)
117+
118+
raise exc
101119
finally:
102120
# release lock (this is safe to run even if the lock has not been acquired)
103121
await lock.release_async()
@@ -114,19 +132,17 @@ conn = ...
114132
# create lock
115133
lock = Lock(conn, "shared-identifier")
116134

117-
try:
118-
# acquire lock
119-
if lock.acquire(block=False):
120-
print("Acquired lock!")
121-
122-
# do something here
135+
# acquire lock
136+
if lock.acquire(block=False):
137+
# do something here
138+
pass
123139

124-
else:
125-
print("Could not acquire lock!")
140+
else:
141+
# could not acquire lock
142+
pass
126143

127-
finally:
128-
# release lock (this is safe to run even if the lock has not been acquired)
129-
lock.release()
144+
# release lock (this is safe to run even if the lock has not been acquired)
145+
lock.release()
130146
```
131147

132148
### Specify the database interface manually
@@ -138,13 +154,29 @@ from postgres_lock import Lock
138154
conn = ...
139155

140156
# create and use lock
141-
with Lock(conn, "shared-identifier", interface="asyncpg"):
142-
print("Acquired lock!")
157+
lock = Lock(conn, "shared-identifier", interface="asyncpg")
143158

144-
# do something here
159+
# do things with the lock
160+
```
161+
162+
### Handle rollbacks manually
163+
164+
```python
165+
from postgres_lock import Lock
166+
167+
# setup connection
168+
conn = ...
169+
170+
# create and use lock
171+
lock = Lock(conn, "shared-identifier", rollback_on_error=False)
172+
173+
# do things with the lock
145174
```
146175

147176
### Changelog
148177

178+
- **0.1.2**
179+
- Add Lock.rollback_on_error (default true)
180+
- Add Lock.handle_error() & Lock.handle_error_async()
149181
- **0.1.1**
150182
- Key can be str or int

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "postgres-lock"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "Lock mechanism implemented with Postgres advisory locks."
55
license = "BSD-3-Clause"
66
authors = ["Sean Kerr <sean@code-box.org>"]

0 commit comments

Comments
 (0)