Comments (8)
@metkuten, to help us to reproduce the problem, could you please provide a small application and steps to reproduce? Thanks. https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-4x-bugs
from loopback-connector-mssql.
@dhmlau : below is the code snapshot, unfortunately i work for a bank and its hard to provide more code details. but i am sure, i am following details provided on loopback4 tutorials(model, transaction repository, passing transaction object into repository call(create),etc).
all other insert, get quires works perfectly fine.
code snapshot
-
export class PersonRepository extends DefaultTransactionalRepository
-
controller i have below code.
async createTransaction(): Promise {
return this.personRepo.beginTransaction();
}
const dbTransactionPromise = this.personsService.createTransaction();
dbTransaction = await dbTransactionPromise;
from loopback-connector-mssql.
@metkuten, I have the following test code and it seems to be working:
/**
* Test endpoint
* @param id
*/
@get('/customers/create-find/{id}', {
responses: {
'200': {
description: 'Customer model instance',
content: {'application/json': {schema: getModelSchemaRef(Customer)}},
},
},
})
async createAndFind(@param.path.string('id') id: string): Promise<Customer> {
let tx: Transaction;
tx = await this.customerRepository.beginTransaction(
IsolationLevel.READ_COMMITTED,
);
const newCustomer = await this.customerRepository.create(
{
name: 'Bob',
city: 'Toronto',
},
{transaction: tx},
);
await tx.commit();
return this.customerRepository.findById(id);
}
I'm using https://github.com/strongloop/loopback-next/blob/master/packages/repository-tests/src/crud/transactions.suite.ts as reference. Hope it helps.
from loopback-connector-mssql.
@dhmlau : as i mentioned above , i am exactly implementing like this. and it's throwing an exception. look like call back "cb" is not working in the code.
below is the transaction.js code on server
// Node module: loopback-connector
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('loopback:connector:transaction');
var uuid = require('uuid');
module.exports = Transaction;
/**
* Create a new Transaction object
* @param {Connector} connector The connector instance
* @param {*} connection A connection to the DB
* @constructor
*/
function Transaction(connector, connection) {
this.connector = connector;
this.connection = connection;
EventEmitter.call(this);
}
util.inherits(Transaction, EventEmitter);
// Isolation levels
Transaction.SERIALIZABLE = 'SERIALIZABLE';
Transaction.REPEATABLE_READ = 'REPEATABLE READ';
Transaction.READ_COMMITTED = 'READ COMMITTED';
Transaction.READ_UNCOMMITTED = 'READ UNCOMMITTED';
Transaction.hookTypes = {
BEFORE_COMMIT: 'before commit',
AFTER_COMMIT: 'after commit',
BEFORE_ROLLBACK: 'before rollback',
AFTER_ROLLBACK: 'after rollback',
TIMEOUT: 'timeout',
};
/**
* Commit a transaction and release it back to the pool
* @param cb
* @returns {*}
*/
Transaction.prototype.commit = function(cb) {
return this.connector.commit(this.connection, cb);
};
/**
* Rollback a transaction and release it back to the pool
* @param cb
* @returns {*|boolean}
*/
Transaction.prototype.rollback = function(cb) {
return this.connector.rollback(this.connection, cb);
};
/**
* Begin a new transaction
* @param {Connector} connector The connector instance
* @param {Object} [options] Options {isolationLevel: '...', timeout: 1000}
* @param cb
*/
Transaction.begin = function(connector, options, cb) {
if (typeof options === 'function' && cb === undefined) {
cb = options;
options = {};
}
if (typeof options === 'string') {
options = {isolationLevel: options};
}
var isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
assert(isolationLevel === Transaction.SERIALIZABLE ||
isolationLevel === Transaction.REPEATABLE_READ ||
isolationLevel === Transaction.READ_COMMITTED ||
isolationLevel === Transaction.READ_UNCOMMITTED, 'Invalid isolationLevel');
debug('Starting a transaction with options: %j', options);
assert(typeof connector.beginTransaction === 'function',
'beginTransaction must be function implemented by the connector');
connector.beginTransaction(isolationLevel, function(err, connection) {
if (err) {
return cb(err);
}
var tx = connection;
// When the connector and juggler node module have different version of this module as a dependency,
// the transaction is not an instanceof Transaction.
// i.e. (connection instanceof Transaction) == false
// Check for existence of required functions and properties, instead of prototype inheritance.
if (connection.connector == undefined || connection.connection == undefined ||
connection.commit == undefined || connection.rollback == undefined) {
tx = new Transaction(connector, connection);
}
// Set an informational transaction id
tx.id = uuid.v1();
// NOTE(lehni) Handling of transaction timeouts here only works with recent
// versions of `loopback-datasource-juggler` which make its own handling of
// timeouts conditional based on the absence of an already set `tx.timeout`,
// see: https://github.com/strongloop/loopback-datasource-juggler/pull/1484
if (options.timeout) {
tx.timeout = setTimeout(function() {
var context = {
transaction: tx,
operation: 'timeout',
};
tx.notifyObserversOf('timeout', context, function(err) {
if (!err) {
tx.rollback(function() {
debug('Transaction %s is rolled back due to timeout', tx.id);
});
}
});
}, options.timeout);
}
cb(err, tx);
});
};```
from loopback-connector-mssql.
@strongloop/loopback-maintainers, might need your help here. It seems like @metkuten and I are doing the same thing to call beginTransaction
(my test code is here), but I wasn't able to reproduce the problem.
from loopback-connector-mssql.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from loopback-connector-mssql.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS
file at the top-level of this repository.
from loopback-connector-mssql.
Why loopback4 in the transactions retains and does not release the auto incremental id despite using await tx.rollback () when the transaction fails.
My repositories inherit from Default Transactional Repository as indicated in the official documentation
from loopback-connector-mssql.
Related Issues (20)
- tedious type deprecation messages in console HOT 2
- query the table using rowversion HOT 2
- AutoUpdate needs to have order when executing index related operations HOT 2
- Loopback 4 relations compatibility HOT 15
- applyPagination is missing the ORDER BY clause when supportsOffsetFetch HOT 2
- loopback 4 model options not working for mssql HOT 5
- MsSQL not exported in loopback-connector-mssql HOT 7
- Incorrect Table Relations Discovery HOT 5
- Relations not working in loopback 4 using mysql database How to solve it HOT 3
- Run shared tests from both v3 and v4 of juggler
- Transaction not working in Loopback 4. HOT 2
- New release? HOT 2
- EPERM on version 3.7.0
- EPERM in version 3.7.0, error related to adal node package HOT 22
- executeSql should return mssql results HOT 5
- Loopback 4 can not parsing javascript Date Object to native SQL statement which require Date parameter HOT 1
- migrate script not supports custom column names
- Having CVE in the current version. can you please update to latest version of mssql and loopback-connector HOT 1
- Like operator not working when trying to fetch stringify array of objects or object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loopback-connector-mssql.