strongloop / strong-oracle

Deprecated: Node.js Driver for Oracle databases (Use https://github.com/oracle/node-oracledb instead)
Other
45 stars 18 forks source link

ORA-12537: TNS:connection closed - Error when running multiple queries in succession #49

Closed CollinEstes closed 9 years ago

CollinEstes commented 9 years ago

We are trying to setup an automated routine which performs several successive queries using strong-oracle.

We are using a datareader currently with strong-oracle like this:

/**
*
* mdt_oracle/src/oracleQuery.js
*
**/
"use strict";
var _ = require('lodash')
  , path = require('path')
  , oracle = require('strong-oracle')({})
  ;

var Query = require('mdt_utils').database.query.Query
  , connectionData = require('../config/config.js');

class OracleQuery extends Query {
  constructor(query, params) {
    super(params);

    if (query !== undefined) {
      this.query = query;
    }

    this.dbConfig = connectionData();

    if (_.isArray(params)) {
      this.queryInputParameters = _.pluck(params, 'value');
    } else {
      this.queryInputParameters = [];
    }

  }

  _setupReader (err, connection) {
    if (err) {
      return this.cb(err);
    }
    // execute query
    this.connection = connection;
    this.connection.setPrefetchRowCount(2000);
    this.reader = connection.reader(this.query, this.queryInputParameters);
    this.results = [];
    this._doRead(this.cb);
  }

  _execSp (connection, done) {
    connection.execute("call dbad.sp(:1)"
      , [new oracle.OutParam(oracle.OCCICURSOR)]
      , done);
  }

  _doRead () {
    try {
        this.reader.nextRow(this._processRow.bind(this));
    } catch (e) {
        return done(e);
    }
  }

  _processRow (err, row) {
    if (err) {
      return this.cb(err);
    };

    if (!row) {
      return this.cb(null, this.results);
    };

    if (row) {
      this.results.push(row);
      return this._doRead();
    };
  }

  execute (cb) {
    var me = this;
    super.execute(cb);

    this.cb = cb;

    if (!this.query) {
      throw new Error('No query was set');
    }

    // connect to oracle db
    oracle.connect(this.dbConfig, this._setupReader.bind(this));
  }

}

module.exports = OracleQuery;

Should I be setting a connection pool? Everything works fine when running single requests but aggregation requests clustered together cause this error.

CollinEstes commented 9 years ago

Turns out I was exceeding the maximum process limit on my Oracle DB Server. I believe the problem was that I was not closing my connection after I complete a query. The connection would eventually timeout but in that time I was spinning up enough concurrently (using async parallel over a collection) that I would eventually hit the limit of max processes setup for our staging server.