2

I have a program that queries a queue table every 2 seconds looking for new entries to process (queue table never has more than a handful of records in it at anytime). I've read that leaving SqlConnections open is typically poor form but most of the references are talking about code that isn't hitting the database on such a rapid pace.

Question:

In a loop where a database is polled on a short duration (every 2 seconds for example), would it be better to leave the database connection open or re-open it every query.

b.pell
  • 3,873
  • 2
  • 28
  • 39
  • 3
    I think this link will answer your question: http://programmers.stackexchange.com/questions/142065/creating-database-connections-do-it-once-or-for-each-query – Ulric May 25 '16 at 13:44
  • 2
    Best practice is to close connections as soon as soon the connection is no longer needed irrespective of the time interval between requests. – nobody May 25 '16 at 13:49
  • 1
    http://stackoverflow.com/questions/5288434/how-to-monitor-sql-server-table-changes-by-using-c – Dmitry Bychenko May 25 '16 at 14:14

1 Answers1

1

Best practice is to re-open every query.

Once every two seconds isn't particularly fast. A website with significant load such as StackOverflow will open and close connections far more frequently.

Joe
  • 122,218
  • 32
  • 205
  • 338