1

I have a small C# app that executes queries every 10 seconds. Should I open and close the connection every time I execute a query (method #2) or keep it open until I close the app (method #1)?

Method #1:

class A
{
     A()
     {
           connection.open()
     }

     public void fct()
     {
           //do stuff
     }

     ~A()
     {
           connection.close()
     }
}

Method #2:

class A
{
     A()
     {
     }

     public void fct()
     {
           connection.open();
           //do stuff
           connection.close();
     }

     ~A()
     {
     }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Simon
  • 71
  • 2
  • 9
  • 1
    open and close the connection every time – Mitch Wheat Oct 23 '18 at 08:10
  • 1
    also, remember that you can use the connection pool to leverage the overall costs of physically opening and closing a connection to the your DBMS. When you open and close a connection with a connection pool, you'll logically close the connection but leaving the physical connection open to be reused until it expires. This is indeed useful in these scenarios. – Yennefer Oct 23 '18 at 15:24
  • Thanks a lot. Both answers were useful. – Simon Oct 23 '18 at 15:28

0 Answers0