Closed kareemalkoul closed 5 months ago
Yes, that is true. Currently, each @Transactional
decorator creates a new transaction.
I have a plan to add nesting options soon, but for now it's a limitation of the library, so please stay tuned.
I propose the following APIs for the propagation control, inspired by the Spring framework:
An enum
called Propagation
with possible values:
Required
- (default) Reuse the existing transaction or create a new one if none existsMandatory
- Reuse an existing transaction, throw an exception otherwiseNever
- Throw an exception if an existing transaction exists, otherwise create a new oneNotSupported
- Run the method without a transaction even if one exists (essentially calling TransactionHost#withoutTransaction
)RequiresNew
- Create a new transaction even if one already existsIntroducing the default behavior or
Propagation.Required
wold be a BREAKING CHANGE since the default (and only mode) is nowPropagation.RequiresNew
And then use it in one of the following ways (TO BE DECIDED) to specify it:
1) Pass it as the first parameter to the @Transactional
or withTransaction
calls
@Transactional(Propagation.RequiresNew, { someAdapterOption: ... })
async doSomething(){
// ...
}
async doSomething(){
return this.txHost.withTransaction(Propagation.RequiresNew, { someAdapterOption: ... }, async () => {
// ...
})
}
2) ~Mix it into the adapter options~
propagation
property)@Transactional({
propagation: Propagation.RequiresNew,
someAdapterOption: ...
})
async doSomething(){
// ...
}
async doSomething(){
return this.txHost.withTransaction({
propagation: Propagation.RequiresNew,
someAdapterOption: ...
}, async () => {
// ...
})
}
3) ~Move adapter options into a nested field instead~
@Transactional({
propagation: Propagation.RequiresNew,
adapterOptions: { someAdapterOption: ... }
})
async doSomething(){
// ...
}
async doSomething(){ return this.txHost.withTransaction({ propagation: Propagation.RequiresNew, adapterOptions: { someAdapterOption: ... } }, async () => { // ... }) }
@kareemalkoul An update with a support for propagation modes was just published, please update @nestjs-cls/transactional
to v2
and your adapter to latest and see the new documentation.
npm i @nestjs-cls/transactional@latest @nestjs-cls/transactional-adapter-<youradapter>@latest
i use function used @Transactional inside this function calling to function using aleady @Transactional, so i noticed the parent @Transactional is ovveride dont use it so it is act like not @Transactional in parent function ex:
in the example above if some error happen in fun2 the operation in fun1 not rollback