开发者

Entity Framework database connection question

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 s开发者_如何学Gotatements 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 SaveChanges call. 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 using statement 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 the using statement.


The using statement will make sure that db will get disposed and the connection closed.

Grz, Kris.


No, you are wrapping the AndeDBEntities object in a using block with means its Dispose() method will be called when it goes out of scope (as it implements IDisposable). This method will clear all of the unmanaged resources aquired by the object (assuming it has been developed without a leak - which, I think, is a fair presumption).

I don't believe this is the route of your connection limit error. Do you have the developer's edition? This is only licensed for 3 connections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜