'Multiple mssql connections in Node.js

I want to have two MSSQL connections to two different databases in the same Node.js process. For some reason, only the first connection is successful for querying.

const sql = require('mssql');

(async () => {
    try {
        // First connection (success)
        const connection = await sql.connect('mssql://user:[email protected]')
        // First connection (success)
        const connection2 = await sql.connect('mssql://user2:[email protected]')

        // Query in the first database -- success
        console.log(await connection.query(`
            select TOP(10) *
            from dbname.dbo.column
            where 1 = 1
        `))

        // Query in the second database
        //   Fails: Invalid object name 'Product
        //   Only if the first db connection is made.
        //   Succeeds if the first connection would be to this database
        console.log(await connection2.query(`
            SELECT TOP(10) * FROM Product
        `))
    } catch (err) {
        console.log(err)
    }
})()

Why is this? How can we solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source