You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pages/features/transactions.mdx
+1-59Lines changed: 1 addition & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ PostgreSQL transactions ensure that a series of database operations either all s
8
8
9
9
## pg-transaction Module
10
10
11
-
The `pg-transaction` module provides a tiny level of abstraction for handling transactions, automatically managing`BEGIN`, `COMMIT`, and `ROLLBACK` operations while ensuring proper client lifecycle management.
11
+
The `pg-transaction` module provides a tiny level of abstraction for handling transactions, automatically running`BEGIN`, `COMMIT`, and/or`ROLLBACK`.
12
12
13
13
The motivation for this module was I pretty much write the same exact thing in every project I start. Sounds like a good thing to just publish widely.
14
14
@@ -166,18 +166,6 @@ try {
166
166
}
167
167
```
168
168
169
-
### Best Practices
170
-
171
-
1.**Use with Pools**: When possible, use the transaction function with a `Pool` rather than a `Client` for better resource management.
172
-
173
-
2.**Keep Transactions Short**: Minimize the time spent in transactions to reduce lock contention.
174
-
175
-
3.**Handle Errors Appropriately**: Let the transaction function handle rollbacks, but ensure your application logic handles the errors appropriately.
176
-
177
-
4.**Avoid Nested Transactions**: PostgreSQL doesn't support true nested transactions. Use savepoints if you need nested behavior.
178
-
179
-
5.**Return Values**: Use the callback's return value to pass data out of the transaction.
180
-
181
169
### Migration from Manual Transactions
182
170
183
171
If you're currently using manual transaction handling, migrating to `pg-transaction` is straightforward:
@@ -244,49 +232,3 @@ try {
244
232
client.release()
245
233
}
246
234
```
247
-
248
-
### When to Use Manual Transactions
249
-
250
-
Consider manual transaction handling when you need:
251
-
252
-
-**Savepoints**: Creating intermediate rollback points within a transaction
253
-
-**Custom Transaction Isolation Levels**: Setting specific isolation levels
254
-
-**Complex Transaction Logic**: Conditional commits or rollbacks based on business logic
255
-
-**Performance Optimization**: Fine-grained control over transaction boundaries
256
-
257
-
### Savepoints Example
258
-
259
-
```js
260
-
constclient=awaitpool.connect()
261
-
262
-
try {
263
-
awaitclient.query('BEGIN')
264
-
265
-
// First operation
266
-
awaitclient.query('INSERT INTO orders(user_id, total) VALUES($1, $2)', [userId, total])
267
-
268
-
// Create a savepoint
269
-
awaitclient.query('SAVEPOINT order_items')
270
-
271
-
try {
272
-
// Attempt to insert order items
273
-
for (constitemof items) {
274
-
awaitclient.query('INSERT INTO order_items(order_id, product_id, quantity) VALUES($1, $2, $3)',
275
-
[orderId, item.productId, item.quantity])
276
-
}
277
-
} catch (error) {
278
-
// Rollback to savepoint, keeping the order
279
-
awaitclient.query('ROLLBACK TO SAVEPOINT order_items')
280
-
console.log('Order items failed, but order preserved')
281
-
}
282
-
283
-
awaitclient.query('COMMIT')
284
-
} catch (error) {
285
-
awaitclient.query('ROLLBACK')
286
-
throw error
287
-
} finally {
288
-
client.release()
289
-
}
290
-
```
291
-
292
-
**Recommendation**: Use `pg-transaction` for most use cases, and fall back to manual transaction handling only when you need advanced features like savepoints or custom isolation levels. Note: the number of times I've done this in production apps is _nearly_ zero.
0 commit comments