Propagation will help to determine when the query should run in a transaction and what transaction to use.
REQUIRED (default)
In REQUIRED mode, the function will inspect the context for a transaction, use one if exists and create a new one if none exists.
class Service {
@Transactional(Propagation.REQUIRED)
async methodA() {
// methodA will create a transaction.
// methodB will use transaction created by methodA.
await this.methodB();
}
@Transactional(Propagation.REQUIRED)
async methodB() { ... }
}
REQUIRES_NEW
In REQUIRES_NEW mode, the function will always create and use a new transaction and assign it to the subsequent async context, even if one exists in the current context.
class Service {
@Transactional(Propagation.REQUIRED)
async methodA() {
// methodA will create a transaction.
// methodB will alwyas use a different transaction that methodA.
await this.methodB();
// methodB transaction will be commited here.
// queries here will use methodA transaction.
}
@Transactional(Propagation.REQUIRES_NEW)
async methodB() { ... }
}
MANDATORY
In Mandatory mode, the function will inspect the context for a transaction, if it finds a transaction it will use it, if it does not it will throw an error.
class Service {
@Transactional()
async methodA() {
await this.methodB();
}
@Transactional(Propagation.MANDATORY)
async methodB() { ... }
}
const service = new Service();
// works
await service.methodA();
// throws an error
await service.methodB();
SUPPORTS
In SUPPORTS mode, the function will inspect the context for a transaction, if it finds one it will use it, if not, it will run the queries without a transaction.
class Service {
@Transactional(Propagation.REQUIRED)
async methodA() {
// methodB queries will run inside method A transaction.
await this.methodB();
}
@Transactional(Propagation.SUPPORTS)
async methodB() { ... }
}
const service = new Service();
// methodB queries will run inside method A transaction.
await service.methodA();
// methodB queries will run outside a transaction.
await service.methodB();
NOT_SUPPORTED
In NOT_SUPPORTS mode, the function will always run outside the current context transaction.
class Service {
@Transactional(Propagation.REQUIRED)
async methodA() {
// methodB queries will run outside method A transaction.
await this.methodB();
}
@Transactional(Propagation.NOT_SUPPORTED)
async methodB() { ... }
}
NEVER
In NEVER mode, the function will run outside a transaction if non exists in the current context, and will throw an error if a transaction exists in the current context.
class Service {
@Transactional(Propagation.REQUIRED)
async methodA() {
// will throw an error since the context has a transaction.
await this.methodB();
}
@Transactional(Propagation.NEVER)
async methodB() { ... }
}
const service = new Service();
// throws an error.
await service.methodA();
// works.
await service.methodB();