We are using .Net Entity Framework to do our database related work. Our database is Sybase SQL Anywhere.
using (AndeDBEntities db = new AndeDBEntities(decryptConnStr()))
{
}
We use a lots of above statements to access database. My questions are do we need to close the connection each time after the access is done and how to do that?
At one time I saw “Database server connection limit exceeded” error. I am wondering there must be something wrong in our database connection code.
The connection should be closed automatically. It’s possible that there is a resource leak in the Sybase EF supporting classes.
See Managing Connections for more information. Note that (by default) EF will open and dispose a database connection for each query or
SaveChangescall. If Sybase’s supporting classes do not handle this well (e.g., with a connection pool), then a resource leak may become noticeable when it would otherwise not be.So actually the
usingstatement does not close the EF connection (unless you’ve manually opened it). It should have already been disposed (released to the connection pool or closed) before reaching the end of theusingstatement.