tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 465 forks source link

mssql ISSUE:- My issue is everytime I opens a connection it should not kill the previous query and start a new connection. #1486

Closed Kedar782 closed 1 year ago

Kedar782 commented 1 year ago

Here is my code :- I'm using the mssql library in node js

const sql = require('mssql')

async () => {
    try {
         await sql.connect('Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true')
        const result = await sql.query`select * from mytable where id = ${value}`
    } catch (err) {
        // ... error checks
    } finally{
        await sql.close()
    }
}

I'm simultaneously executing lots of other queries in my application. My goal is to create a new connection every time using the above code and it should not kill the previous connection or query. Right now It is not killing the previous query but it is using the previous connection which is not closed. How should I disable this? Means I have to ignore the previous connection and make a new one.

I will really appreciate your help on this.

Kedar782 commented 1 year ago

@dhensby Do you have any idea about this? I have stuck on this for a long time.

dhensby commented 1 year ago

I recommend reading the connection pooling part of the docs: https://github.com/tediousjs/node-mssql#connection-pools

Kedar782 commented 1 year ago

@dhensby Can I do it without using the pool? I have added my code in the above? Can I make some changes in the connection string so it will create a new connection instead of using the old one?

dhensby commented 1 year ago

There is no way to create a single connection.

If you want to change the connection string you need to create a new pool and not use the global one, again it's covered in the docs.

Kedar782 commented 1 year ago

@dhensby I went through the documentation and I did not find it. Can you please give me example here how can i achieve it? I don't want to kill previous queries and I don't want to use the existing connection when config is different. I will really appreciate it if you post a code here for this.

dhensby commented 1 year ago

The part about using different pools for different connections is in the section I linked to, sub section advanced pool management - it provides example code for managing multiple pools.