Solved

Should I reuse Npgsql connections in a C# application?

Posted on 2016-10-31
3
148 Views
Last Modified: 2016-11-01
Hi:

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.

Thanks,
JohnB
0
Comment
Question by:jxbma
3 Comments
 
LVL 51

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 total points
ID: 41867903
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
http://www.postgresqltutorial.com/introduction-to-postgresql-stored-procedures/
0
 
LVL 13

Accepted Solution

by:
Phil Phillips earned 250 total points
ID: 41867916
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.
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 41868500
I'm new to postgresql, so I just wanted to make sure standard DB strategies still apply.
Thx!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question