Reputation: 23
Looking for help to implement transaction management in loopback4. Using ms-sql database and loopback-connector-mssql as a connector.
i am extending repository with DefaultTransactionalRepository.
below is the code i am writing inside controller.
1. const addressTransaction = await this.addressRepo.beginTransaction();
2. const address = await this.addressRepo.create(addressObj);
3. addressTransaction.rollback()
Issue is, record which i created at line 2 is not getting reverted after line no 3, there is no error as well. also i am getting addressTransaction object back at line no 1. above 3lines are just an example. moto is i want to revert back data that inserted at line no2.
If i execute below code into DB its working perfectly fine. record is getting roll back.
BEGIN TRANSACTION
INSERT into addresses(ADDRESS_CITY,ADDRESS_STATE) values('123','1234')
ROLLBACK
but some how connector is not working properly.
i checked with DBA, look like we are not setting implicit_transactions on with above transaction. does anyone know how to set implicit_transactions on.
Any issue with the way i am implementing the transaction? kindly help
Upvotes: 1
Views: 250
Reputation: 226
To group operations in a transaction you need to explicitely assign the operation to an transaction.
Here is the official documentation of loopback4 transaction support:
To perform create, retrieve, update, and delete operations in the transaction, add the transaction object to the
Options
parameter of the standardcreate()
,update()
,deleteAll()
(and so on) methods.
In your example you need to do something like this:
const addressTransaction = await this.addressRepo.beginTransaction();
const address = await this.addressRepo.create(addressObj, { transaction: addressTransaction});
addressTransaction.rollback()
Upvotes: 2