Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How using query timeout for queries in loopback using DB2 database? #157

Open
rafaelchagasb opened this issue Dec 29, 2021 · 0 comments
Open
Labels

Comments

@rafaelchagasb
Copy link

When I use the ibmdb I can configure the query timeout in the statement.

How to do this using the Loopback Model? It's possible?

Example using only IBMDB

const ibmdb = require("ibm_db");
    const SQL_ATTR_QUERY_TIMEOUT = ibmdb.SQL_ATTR_QUERY_TIMEOUT;
    const DB2_QUERY_TIMEOUT = 3; // 3 seconds
    
    const init = async () => {
        let DB_CONNECTION = process.env.DB_CONNECTION || 'DATABASE=testdb;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=DB_USERNAME;PWD=DB_PASSWORD';
        let connection = ibmdb.openSync(DB_CONNECTION);
        let sql = "update SCHEMA.TABLE set COLUMN1 = 'VALUE' where ID = 1";
        let stmt = await prepareStatement(connection, sql);   
        executeStatement(stmt);
    }
    
    const prepareStatement = (conn, sql) => {
        return new Promise((resolve, reject) => {
            conn.prepare(sql, (err, stmt) => {
                if(err) reject(err);
                else resolve(stmt);
            })
        });
    }
    
    const executeStatement = (statement, values) => {
        statement.setAttr(SQL_ATTR_QUERY_TIMEOUT, DB2_QUERY_TIMEOUT, (err, result) => {
            statement.execute(values, function (err, result) {
                // return   
            });
        });      
    }
    
    init();

Example using Lookback Model

let DataSource = require('loopback-datasource-juggler').DataSource;
    let connectorDB2 = require('loopback-connector-db2');
    
    const configDB2 = () => ({
        connector: "db2", username: "user", password: "password", hostname: "localhost", port: "50000", database: "DATABASE", schema: "SCHEMA", maxPoolSize: 10, minPoolSize: 5, connectionTimeout: 10
    })
    let connection = new DataSource(connectorDB2, configDB2());
    const schema = { id:{ }, description:{ }};

    let Model = connection.define('TABLENAME', schema);
    Model.update({id: 1}, {description: 'new value'}); -- how configure query timeout ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant