Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:14:47+00:00 2026-05-15T20:14:47+00:00

After a fair amount of research and some errors , I modified my code

  • 0

After a fair amount of research and some errors, I modified my code so that it creates a new DataContext each time the database is queried or data is inserted. And the database is queried frequently – for each of 250k transactions that are processed, the database is queried to obtain a customer id, department id, and category before the transaction is inserted.

So now I’m trying to optimize the code as it was only processing around 15 transactions a second. I removed some extraneous queries and added some indexes and got it up to 30/sec. I then figured that even though everyone says a DataContext is lightweight, it’s got to cost something to create a new one 4 times per transaction, so I tried reusing the DataContext. I found, much to my surprise, that reusing the context caused performance to degrade to 10 transactions a second!

Why would this be the case? Is it because the DataContext caches the entities in memory and first searches through its in-memory list before querying the database? So that if, for example, I’m looking for the customer id (primary key) for the customer with name ‘MCS’ and the customer name column has a clustered index on it so that the database query is fast, the in-memory lookup will be slower?

And is it true that creating/disposing so many db connections could slow things down, or is this just another premature optimization? And if it is true, is there a way to reuse a DataContext but have it perform an actual database query for each linq-to-sql query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:14:48+00:00Added an answer on May 15, 2026 at 8:14 pm

    Here’s why re-using a DataContext is not a best practice, from the MSDN DataContext documentation:

    The DataContext is the source of all
    entities mapped over a database
    connection. It tracks changes that you
    made to all retrieved entities and
    maintains an “identity cache” that
    guarantees that entities retrieved
    more than one time are represented by
    using the same object instance.

    In general, a DataContext instance is
    designed to last for one “unit of
    work” however your application defines
    that term. A DataContext is
    lightweight and is not expensive to
    create. A typical LINQ to SQL
    application creates DataContext
    instances at method scope or as a
    member of short-lived classes that
    represent a logical set of related
    database operations.

    If you’re re-using a DataContext for a large number of queries, your performance will degrade for a couple of possible reasons:

    1. If DataContext’s in-memory identity cache becomes so large that it has to start writing to the pagefile then your performance will be bound to the HD’s read-head speed and effectively there won’t be a reason to use a cache at all.

    2. The more identity objects there are in memory, the longer each save operation takes.

    Essentially what you’re doing is violating the UoW principle for the DataContext class.

    Opening database connections does have some overhead associated with it, but keeping a connection open for a long period of time (which often also means locking a table) is less preferable than opening and closing them quickly.

    Another link which may or may not help you from MSDN:

    How to: Reuse a Connection Between an ADO.NET Command and a DataContext (LINQ to SQL)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After some time researching and trying different things I still cannot get my @ExceptionHandler
I spent a fair amount of time trying to determine why my view of
I have a class that has a fair amount of static variables, not all
I haven't found a clear answer to this after a fair amount of scouring
I've been playing around with HPricot, but after a fair amount of searching, I've
I have a page that has a fair amount of content, followed by a
I'm deserializing a fair amount of data through Boost.Serialization (one for each frame). However,
I'm building an app that will contain a fair amount of resources (video) and
I'm just learning objective-c after a fair amount of experience with C#. One of
After reading a fair few posts on Stack, and also some material recommended to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.