loopbackio / loopback-connector-mssql

LoopBack connector for Microsoft SQL Server
http://loopback.io/doc/en/lb3/SQL-Server-connector.html
Other
52 stars 84 forks source link

Transaction not working in Loopback 4. #215

Closed metkuten closed 4 years ago

metkuten commented 4 years ago

Facing issue in loopback 4. Transaction is not working. i am extending repository with DefaultTransactionalRepository.

i am writing below code 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 in background. also i am getting addressTransaction object back after 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 ms-sql DB its working perfectly fine. record is getting roll back.

BEGIN TRANSACTION INSERT into addresses(ADDRESS_CITY,ADDRESS_STATE) values('123','1234') ROLLBACK

i checked with DBA, look like we are not setting "implicit_transactions on" with above transaction. also connector is not calling beginTransaction or rollback queries back to DB.

select, insert queries are working perfectly fine.

Kindly advice.

raymondfeng commented 4 years ago

You need to explicitly pass the transaction into create.

const address = await this.addressRepo.create(addressObj, {transaction});

See https://loopback.io/doc/en/lb4/Using-database-transactions.html#perform-operations-in-a-transaction

metkuten commented 4 years ago

@raymondfeng : still facing issue, i have implemented transaction as mentioned above. but sometime its returning undefined after calling beginTransaction instead of Promise.

getting below error in console. Kindly help on this.

2019-11-15T21:00:52.747Z [founders-mw-dev-699fbb8489-z6k46] error: {"APPLICATION_NAME":"FOUNDERS_SVC","MESSAGE":"uncaughtException: cb is not a function\nTypeError: cb is not a function\n at /u01/foundersplatform/founders-mw/node_modules/loopback-connector/lib/transaction.js:119:5\n at /u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/lib/transaction.js:25:7\n at /u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/mssql/lib/base.js:813:9\n at /u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/mssql/lib/tedious.js:333:11\n at Request.userCallback (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/connection.js:1479:16)\n at Request._this.callback (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/request.js:61:27)\n at Connection.endOfMessageMarkerReceived (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/connection.js:1944:20)\n at Connection.dispatchEvent (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/connection.js:1012:38)\n at Parser. (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/connection.js:812:18)\n at Parser.emit (events.js:209:13)\n at Parser. (/u01/foundersplatform/founders-mw/node_modules/loopback-connector-mssql/node_modules/tedious/lib/token/token-stream-parser.js:54:15)\n at Parser.emit (events.js:209:13)\n at addChunk (/u01/foundersplatform/founders-mw/node_modules/readable-stream/lib/_stream_readable.js:291:12)\n at readableAddChunk (/u01/foundersplatform/founders-mw/node_modules/readable-stream/lib/_stream_readable.js:278:11)\n at Parser.Readable.push (/u01/foundersplatform/founders-mw/node_modules/readable-stream/lib/_stream_readable.js:245:10)\n at Parser.Transform.push (/u01/foundersplatform/founders-mw/node_modules/readable-stream/lib/_stream_transform.js:148:32)"}