Question: Connection Pools and Azure function
I'm not a 100% sure if the issue is my understand of the library or azure functions.
I have a project that has 20+ azure functions in it. All the functions reference one utility file. That creates a connection. The issue i'm getting is connection closed when concurrent calls are made from the client.
Each call is wrapped in function like such:
let connection = await sql.connect(config)
try {
const result = await next(context, req, { user, companyId, connection })
return normalize(result, req.method, req.params.id)
} catch (err) {
context.log.error(err)
return ErrorResult(err, 500)
} finally {
connection.close()
}
example of the function is as follows...
module.exports = Decorator(Roles.SuperAdmin)(async function (context, req, { user, companyId, connection }) {
result = await connection
.request()
.execute('usp_InvertersSelect')
return result
}
I also tried pools concept which worked locally but failed immediately when running in Azure .
Any thoughts or assistance would be greatly appreciated.
Software versions
- NodeJS: 10+
- node-mssql: 6.0.0
- SQL Server: Azure database
Hi @mgambill - I am using this library in Azure functions and am having a pretty good time with it. We use it across functions and it's pretty reliable.
There's a little quirk with Azure Functions: the node app is kept running and requests are passed into the app (as opposed to the node app being spun up, a request handled and it being thrown away), this means that the database pool is going to persist across connections. The fact you're calling connection.close means that you can be closing the pool for other requests because you're sharing a reference to the pool across all requests.
As such, the solution should be simple: never call connection.close() it is not required as you want the pool to be open for all your requests. As you are using v6.0.0 and the global pool, you can make repeat calls to connect() (though it's not needed).
An alternative approach is as so; creating a single database helper / utility file that holds a reference to your connected pool:
let connection
getConnection () {
// if there's a connection already, return it as a Promise
if (connection) {
return Promise.resolve(connection)
}
// no connection, so assign it the promise (this allows repeat calls during the `connecting` phase to
// still return a connection promise
connection = sql.connect(config).then(conn => {
// modify the connection so any calls to close will unset the local variable
const close = conn.close.bind(conn);
conn.close = () => {
connection = null
return close()
}
// once the pool is connected, replace the connection promise with the resolved pool
return connection = conn
}).catch(err => {
// if there's an error, unset the connection variable so we don't continually return an erroring connection
connection = null
return Promise.reject(err)
})
}
This means you can always call await getConnect() and you will get a connected pool. In this scenario you'd never call close() or connect() on the connection you get as the pool should always be in a connected state. However, if close() is called (eg: after test suites) then you will be able to call await getConnection() again and get a new connection.
I'm going to close this issue now as I think this will solve your problem and is an implementation issue, not a library bug