Entity Framework Long Living Data Context Vs Short Living Data Context
Solution 1:
Open and close as quickly as possible. Let ADO.Net
connection pooling take care of minimizing overhead of actually connecting to the database over and over. As long as you use the same connection string, closing a connection does not actually close it. It just releases it back to ADO.Net connection pool. The longer you keep it open, (and not in the pool), the more actual connections the pool may need to create to service the database requests.
Solution 2:
Mainly depends on what kind of application you're designing, but it's great idea to close them as soon as possible (but not necessarily right after performing a single query, just to re-open it five lines of code later - reopening connection takes time too and on high-ping connections this might become an unbearable user experience).
Just be sure to close the connection whenever you end a task that requires an open connection (for example gathering data from DB) and start another task (for example processing data) or await user input. Remember you can pass the connection as parameter to methods you might be calling too, to conserve the connection pool.
Post a Comment for "Entity Framework Long Living Data Context Vs Short Living Data Context"