using sqlConnection RRS feed

  • Question

  • Hi,

    Regarding sqlConnection, is it recommended to keep it open all across the application or open it every time I want to use it then close it? Which method is better for the memory?


    Monday, December 29, 2014 12:15 PM

All replies

  • Hello Jassim,

    Personally, I would choice to open/close the connection for every call. Always close connections as soon as you are done with them, so they underlying database connection can go back into the pool and be available for other callers. With the connection pool, Closing/opening DbConnection doesn't affect your performance, but makes your code clean and stable - connection leaks are impossible in this case and it would not take much memory.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 30, 2014 2:06 AM
  • In additional to above, note that if your query has error that leave your connection in error state, you'll need to close and reopen it anyway. And "open only when you use it" model works better if your target platform supports hibernation / hybrid sleep, which means most systems on Win7+.
    Wednesday, December 31, 2014 4:35 AM