Giter Club home page Giter Club logo

Comments (8)

dhmlau avatar dhmlau commented on August 18, 2024

@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.

metkuten avatar metkuten commented on August 18, 2024

@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

  1. export class PersonRepository extends DefaultTransactionalRepository

  2. controller i have below code.
    async createTransaction(): Promise {
    return this.personRepo.beginTransaction();
    }

const dbTransactionPromise = this.personsService.createTransaction();
dbTransaction = await dbTransactionPromise;

from loopback-connector-mssql.

dhmlau avatar dhmlau commented on August 18, 2024

@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.

metkuten avatar metkuten commented on August 18, 2024

@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.

dhmlau avatar dhmlau commented on August 18, 2024

@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.

stale avatar stale commented on August 18, 2024

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.

stale avatar stale commented on August 18, 2024

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.

heroca60 avatar heroca60 commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.