Does closing a database connection in Dispose method is right?

后端 未结 2 1434
小鲜肉
小鲜肉 2020-12-17 03:52

I\'ve had a suspicion that a database connection used in one of our applications is not always closed. I went to see the code and I\'ve found a class DataProvider

2条回答
  •  遥遥无期
    2020-12-17 04:13

    conn.Dispose(); will also close the connection, so can't hurt changing it to follow the dispose pattern.

    But there functionally equivalent so there must be a problem else where.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

    If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is closed.

提交回复
热议问题