title: Good technique for connections with PostgreSQL
body: <p>I am using <code>Npgsql</code> to access PostgreSQL via .NET. I am concerned about the right way to perform connections to the database, since in my opinion this is an expensive operation to open a connection and then close it every single time I want to perform some transaction.</p>
  
  <p>So here is the general idea:</p>
  
  <pre><code>public class PostgreSQL
  {
      private NpgsqlConnection conn; // &lt;- one connection for this object, open all the time
  
      public PostgreSQL(string connString)
      {
          conn = new NpgsqlConnection(connString);
          conn.Open();
      }
  
      // ...here making some queries...
  
      public void Close() =&gt; conn.Close(); // &lt;- use this in the very end of the program
  }
  </code></pre>
  
  <p>As you can see above, I have one connection for an instance of <code>PostgreSQL</code> class.</p>
  
  <p><strong>My question</strong>:</p>
  
  <p>Is this approach right? Or should I open and close connection every single time I want to make a transaction - open as late as possible and close as soon as possible?</p>
  
  <p>If I should open and close connections every single time - should I write a queue that would limit the amount of concurrent connections? Or PostgreSQL will handle it itself - and, theoretically, I can open 200 connections and it will be alright.</p>
  
  <p>Please share your experience with me ^^</p>
  
  <p><strong>EDIT:</strong>
  I will run 100-200 queries a second.</p>