@@ -32,15 +32,17 @@ pip install postgres-lock
32
32
33
33
### Default operation
34
34
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.
37
38
38
39
### Usage
39
40
40
41
All work revolves around the ` Lock ` class.
41
42
42
43
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.
44
46
45
47
_ Using ` with ` and ` async with ` implies blocking mode._
46
48
74
76
75
77
print (" Acquired lock!" )
76
78
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
78
88
finally :
79
89
# release lock (this is safe to run even if the lock has not been acquired)
80
90
lock.release()
97
107
98
108
print (" Acquired lock!" )
99
109
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
101
119
finally :
102
120
# release lock (this is safe to run even if the lock has not been acquired)
103
121
await lock.release_async()
@@ -114,19 +132,17 @@ conn = ...
114
132
# create lock
115
133
lock = Lock(conn, " shared-identifier" )
116
134
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
123
139
124
- else :
125
- print (" Could not acquire lock!" )
140
+ else :
141
+ # could not acquire lock
142
+ pass
126
143
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()
130
146
```
131
147
132
148
### Specify the database interface manually
@@ -138,13 +154,29 @@ from postgres_lock import Lock
138
154
conn = ...
139
155
140
156
# create and use lock
141
- with Lock(conn, " shared-identifier" , interface = " asyncpg" ):
142
- print (" Acquired lock!" )
157
+ lock = Lock(conn, " shared-identifier" , interface = " asyncpg" )
143
158
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
145
174
```
146
175
147
176
### Changelog
148
177
178
+ - ** 0.1.2**
179
+ - Add Lock.rollback_on_error (default true)
180
+ - Add Lock.handle_error() & Lock.handle_error_async()
149
181
- ** 0.1.1**
150
182
- Key can be str or int
0 commit comments