Question

I was debugging Jaroslaw Kowalski provider wrappers. While debugging it I observed that the Entity Framework closes the connection after every query. On every interaction with database, during the lifetime of application, a database connection is opened and after performing the query the connection is closed

My questions are:

  • Whether the underlying provider do connection pooling by default.
  • If I provide my own pooling then how will I know when to close the connection. At the moment I am registering with Thread.GetDomain().ProcessExit event and closing the connection when the event fires. Is this approach good?

The thing that is bothering me is that Entity Framework itself is closing the connection. So I am a little hesitant to pool the connection or it may cause problems in exceptional scenarios.

Was it helpful?

Solution

FYI:

The EF only closes connections it opens.

So if you do something like this manually:

((ctx.Connection as EntityConnection).StoreConnection as SqlConnection).Open();

subsequents queries and SaveChanges() shouldn't close that connection.

Hope this helps

Alex

OTHER TIPS

The default connectionpool = 1 connection. So don't worry.

The whole idea here is that we have 1 programming model (disconnected) and that configuration & tuning is external.

In .Net 4, to quote: Managing Connections and Transactions

The following considerations apply when managing connections:

The object context will open the connection if it is not already open before an operation. If the object context opens the connection during an operation, it will always close the connection when the operation is complete.

If you manually open the connection, the object context will not close it. Calling Close or Dispose will close the connection.

If the object context creates the connection, the connection will always be disposed when the context is disposed.

In a long-running object context, you must ensure that the context is disposed when it is no longer required.

If you supply an open EntityConnection for the object context, you must ensure that it is disposed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top