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
You can configure the `TransactionModule` with the following options:
41
+
-`autoUseMasterNodeForChangeRequest`: If set to `true`, the module will automatically use the master node for change requests (except GET). This is useful for ensuring that write operations are always directed to the correct database node.
23
42
24
43
### How to use
25
44
26
-
##### your-service.service.ts
45
+
#### Transactional
46
+
47
+
You can use the `@Transactional` decorator to mark a method as transactional. This means that all database operations within this method and child methods will be executed within a transaction context. If any operation fails, the entire transaction will be rolled back, ensuring data integrity.
48
+
49
+
In any service, you can use the `@Transactional` decorator to ensure that the method is executed within a transaction context:
50
+
51
+
> **Note**: We suggest using the `@Transactional` decorator on Controller/Task/Cron jobs methods, as it will automatically handle the transaction lifecycle for you. However, you can also use it in services if needed.
You also use the `@Transactional` decorator with options to control the transaction behavior, such as isolation level. For example:
80
+
81
+
```typescript
82
+
@Transactional({ isolationLevel: 'SERIALIZABLE'})
83
+
asyncmyTransactionalMethod() {
84
+
// This method will run in a transaction with SERIALIZABLE isolation level
85
+
awaitthis.myRepository.save(myEntity);
86
+
}
87
+
```
88
+
89
+
#### Replication
90
+
You can use the `@UseMasterNode`/`@UseSlaveNode` decorator to ensure that a method is executed on the master/slave node. This is useful for write operations that need to be directed to the master/slave database node.
You can configure services to be excluded from transactions by specifying them in `transactionConfig` and importing it
73
-
into `AppModule`
111
+
You can use the `@RunAfterTransactionCommit` decorator to run a method after a transaction has been successfully committed. This is useful for performing actions that should only occur if the transaction was successful.
0 commit comments