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

getConnection always get timed out #2566

Open
blu2eh opened this issue Dec 2, 2023 · 0 comments
Open

getConnection always get timed out #2566

blu2eh opened this issue Dec 2, 2023 · 0 comments

Comments

@blu2eh
Copy link

blu2eh commented Dec 2, 2023

So in this code I wrote, I am trying to connect to my database through a ssh tunnel. And when I am only using oracledb.createPool(dbconfig), everything works fine. But when I wrote const connection = await oracledb.getConnection(dbConfig);, the process timed out. The SSH tunnel got connected successfully by the way.

const { Client } = require('ssh2');
const oracledb = require('oracledb');

const configs = require('./config')

const con = new Client();
console.log('Initializing:', configs);
console.log('Initializing:', configs.sshConfig.port);





con.on('ready', function() {
    console.log('Connection :: ready');
    con.shell(function(err, stream) {
        if (err) {
            console.log('err over ')
        } else {
            connectToDatabase();
            var buf = '';
            var bData = false;
            stream.on('data', function(data) {

                if (bData == false) {
                    bData = true;
                    stream.write('su\n');
                }
                if (data.indexOf('Password:') > -1) {
                    stream.write('Pxxxxxxxx')
                }
                console.log(data.toString())
                if (data.indexOf('# ') > -1) {
                    console.log('successful')
                    stream.end()
                }
            }).on('close', function() {
                console.log('success over ')
            }).stderr.on('data', function(data) {
                console.log('stderr: ' + data);
            });
        }
    });
});
con.on('error', function(err) {
    console.log('Connection :: error :: ' + err);
});
con.on('end', function() {
    console.log('Connection :: end');
});
con.on('close', function(had_error) {
    console.log('Connection :: close');
});


con.connect({
    host: configs.sshConfig.host,
    port: configs.sshConfig.port,
    username: configs.sshConfig.username,
    password: configs.sshConfig.password
})


// Connecting to remote database
async function connectToDatabase() {
    try {
        // Configuring remote 
        const dbConfig = {
            user: configs.mysql.user, // database username
            password: configs.mysql.password, //database password
            connectString: 'my connect string', // connect string

        // Creating pool
        const pool = await oracledb.createPool(dbConfig);
        console.log('Connected to the remote Oracle database successfully.');

        // Creating connection
        const connection = await oracledb.getConnection(dbConfig);

        //Closing pool and ssh tunnel
        pool.close((err) => {
            if (err) {
                console.error('Error closing the database connection:', err);
                return;
            }
            console.log('Database connection closed successfully.');
            con.end() // disconnect from ssh
        });
    } catch (err) {
        console.error('Error connecting to the remote Oracle database:', err);
    } finally {

    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant