.NET, the SqlConnection object, and multi-threading

前端 未结 2 787
旧巷少年郎
旧巷少年郎 2020-12-11 02:29

We have an application which uses an SQL Server 2008 R2 database. Within the application, calls to the database are made using a SqlConnection object.

T

2条回答
  •  感动是毒
    2020-12-11 02:38

    The obvious solution is to just re-create the SqlConnection object every time a database call requires one - in this case, it would never be shared. Is there any reason not to do this?

    On the contrary, that's absolutely what you should do. That's the behaviour SqlConnection was designed for. You should use a Using statement to automatically close the connection at the end of the block you're using it for, and the connection pool mechanism will automatically handle the real underlying connections to the database.

提交回复
热议问题