• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Should I reuse Npgsql connections in a C# application?


I've got a C# method that, during it's course of execution, is running several queries using npgsql.
I have several queries to gather data from different sources (tables), and then a query to insert
the results into a separate table.

Is it best in this scenario to reuse the NpgsqlConnection for all the queries, or use a
separate NpgsqlConnection for each query.

I'm new to npgsql and not sure what the best practice would be in terms of
performance and reliability.

2 Solutions
Ryan ChongCommented:
what the best practice would be in terms of performance and reliability
that would mean that we try to reduce the opened connection to your database, and we certainly hope that in one connection request, we finished all the tasks as possible as we can. and we probably can do it via database's stored procedures. (meaning do your stuff at database end)

Check the advantages and disadvantages from this article:

Introduction to PostgreSQL Stored Procedures
Phil PhillipsDirector of DevOps & Quality AssuranceCommented:
It's better to reuse the same connection.  There's overhead associated with opening a new connection.  If it's already open, might as well use it.
jxbmaSoftware ConsultantAuthor Commented:
I'm new to postgresql, so I just wanted to make sure standard DB strategies still apply.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now