Reuse database connections

Hello, n8n community,
Let me start off by saying that n8n is an amazing tool. It just simplifies development to a great extent.
However, it has a lot way to go and requires a lot of polishing.

So something from my side :slight_smile:

I have noticed that if in an n8n workflow, if you are using 5 database nodes, 5 separate/new database connections are established with the database.

This is resource-intensive and will result in slower execution.

Is there any way we can re-use previously established database connection?

Maybe we can create an internal array map where we map connections with credentials, and if the connection for that credential is already established, we can reuse it.
Else we can establish a new one.

I believe this will speed up n8n workflows drastically.

I like this idea and it could apply to more than just the database nodes it would also be useful for the FTP node. The way I have seen it implemeted before is you can output a “session id” and if you are using that the connection will be kept open for the life of the workflow and you can then pass that id instead of using credentials.

In theory we could automatically handle that but I suspect it wouldn’t be an easy or quick feature to add.

I went through the source code of a couple of nodes.
It won’t be a straightforward implementation for sure.

Coz you will need something on the lines of a singleton object or a global object which is available everywhere and you can store "session-id"s there.

Once you have the global object ready, it will be very easy from there.
Something as simple as an if condition.

1 Like