views:

33

answers:

3

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.

+1  A: 

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.

David Neale
+1  A: 

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

Grz, Kris.

XIII
+1  A: 

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.

Stephen Cleary