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
@@ -472,6 +477,140 @@ const token = statement.asyncQueryToken; // can only be fetched for async query
472
477
awaitconnection.cancelAsyncQuery(token);
473
478
```
474
479
480
+
<aid="transaction-management"></a>
481
+
## Transaction management
482
+
483
+
Firebolt's Node.js SDK supports database transactions, allowing you to group multiple operations into atomic units of work. Transactions ensure data consistency and provide the ability to rollback changes if needed.
484
+
485
+
<aid="transaction-methods"></a>
486
+
### Transaction methods
487
+
488
+
The SDK provides three main methods for transaction management:
489
+
490
+
```typescript
491
+
awaitconnection.begin(); // Start a new transaction
492
+
awaitconnection.commit(); // Commit the current transaction
493
+
awaitconnection.rollback(); // Rollback the current transaction
494
+
```
495
+
496
+
<aid="basic-transaction-usage"></a>
497
+
### Basic transaction usage
498
+
499
+
The following example demonstrates a basic transaction that inserts data and commits the changes:
console.error('Transaction rolled back due to error:', error);
535
+
}
536
+
```
537
+
538
+
#### Transaction with prepared statements
539
+
540
+
Transactions work seamlessly with prepared statements:
541
+
542
+
```typescript
543
+
awaitconnection.begin();
544
+
545
+
try {
546
+
// Use prepared statements within transactions
547
+
awaitconnection.execute(
548
+
'INSERT INTO users (id, name, age) VALUES (?, ?, ?)',
549
+
{ parameters: [4, 'Diana', 28] }
550
+
);
551
+
552
+
awaitconnection.execute(
553
+
'UPDATE users SET age = ? WHERE id = ?',
554
+
{ parameters: [29, 4] }
555
+
);
556
+
557
+
awaitconnection.commit();
558
+
} catch (error) {
559
+
awaitconnection.rollback();
560
+
throwerror;
561
+
}
562
+
```
563
+
564
+
<aid="transaction-error-handling"></a>
565
+
### Error handling
566
+
567
+
#### Transaction state errors
568
+
569
+
The SDK will throw errors for invalid transaction operations:
570
+
571
+
```typescript
572
+
try {
573
+
// This will throw an error if no transaction is active
574
+
awaitconnection.commit();
575
+
} catch (error) {
576
+
console.error('Cannot commit: no transaction in progress');
577
+
}
578
+
579
+
try {
580
+
awaitconnection.begin();
581
+
// This will throw an error if a transaction is already active
582
+
awaitconnection.begin();
583
+
} catch (error) {
584
+
console.error('Cannot begin: transaction already in progress');
585
+
}
586
+
```
587
+
588
+
<aid="transaction-isolation"></a>
589
+
### Transaction isolation
590
+
591
+
Transactions in Firebolt provide isolation between concurrent operations. Changes made within a transaction are not visible to other connections until the transaction is committed:
592
+
593
+
```typescript
594
+
// Connection 1 - Start transaction and insert data
0 commit comments