| 16 | const pool = await client.createPool(); |
| 17 | |
| 18 | async function transaction() { |
| 19 | try { |
| 20 | await pool.execute(async (isolatedClient) => { |
| 21 | await isolatedClient.watch('paymentId:1259'); |
| 22 | const multi = isolatedClient |
| 23 | .multi() |
| 24 | .set('paymentId:1259', 'Payment Successfully Completed!') |
| 25 | .set('paymentId:1260', 'Refund Processed Successfully!'); |
| 26 | await delay(5000); // Do some changes to the watched key during this time... |
| 27 | await multi.exec(); |
| 28 | console.log('Transaction completed Successfully!'); |
| 29 | client.quit(); |
| 30 | }); |
| 31 | } catch (error) { |
| 32 | if (error instanceof WatchError) { |
| 33 | console.log('Transaction Failed Due To Concurrent Modification!'); |
| 34 | fn(); |
| 35 | } else { |
| 36 | console.log(`Error: ${error}`); |
| 37 | client.quit(); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | transaction(); |