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

  • Home
  • SEARCH
  • 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 7937233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:28:26+00:00 2026-06-03T22:28:26+00:00

If I open a dbContext as a global variable, as in the code below,

  • 0

If I open a dbContext as a global variable, as in the code below, can it lead to connection pool problems compared to using a new datacontext in each function and wrapping it in a using block?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LoopinDeals.Helper;

namespace LoopinDeals.Model
{
    public class DealUsersRepository
    {
        private DataClassesDataContext DbContext = new DataClassesDataContext();

        public void AddUser(string Email, string Password)
        {
            DealUser deal = new DealUser()
            {
                Email = Email,
                Password = Password,
                EmailVerified = true,
                LastLogin = DateTime.Now,
                AccountCreated = DateTime.Now,
                IsActive = true,
                RegistrationMethod = RegistrationType.Normal.ToString(),
                Subscribe = "true"
            };

            DbContext.DealUsers.InsertOnSubmit(deal);
            DbContext.SubmitChanges();
        }

        public void AddSignUpUser(string Email, string City)
        {
            try
            {
                DealUser UserData = new DealUser()
                    {
                        Email = Email,
                        City = City,
                        IsActive = false,
                        LastLogin = DateTime.Now
                    };
                DbContext.DealUsers.InsertOnSubmit(UserData);
            }
            catch (Exception ex)
            {               

            }
        }

        public void UpdateSignUpUser(string Email, string FirstName, string LastName, string Password, string Mobile, string City)
        {
            try
            {
                DealUser UserData = DbContext.DealUsers.Single(UD => UD.Email == Email);
                UserData.FirstName = FirstName;
                UserData.LastName = LastName;
                UserData.Password = Password;
                UserData.Mobile = Mobile;
                UserData.City = City;
                UserData.IsActive = true;
                UserData.LastLogin = DateTime.Now;
                DbContext.SubmitChanges();
            }
            catch (Exception ex)
            {

            }
        }


    }
}

Description

Please note that I did not write this code, it was outsourced. It crashes with the following error message and I am trying to figure out how to fix it.

Server Error in ‘/’ Application. Timeout expired. The timeout period
elapsed prior to obtaining a connection from the pool. This may have
occurred because all pooled connections were in use and max pool size
was reached. Description: An unhandled exception occurred during the
execution of the current web request. Please review the stack trace
for more information about the error and where it originated in the
code.

Exception Details: System.InvalidOperationException: Timeout expired.
The timeout period elapsed prior to obtaining a connection from the
pool. This may have occurred because all pooled connections were in
use and max pool size was reached.

Source Error:

[No relevant source lines]

Source File: c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary
ASP.NET Files\root\f44daa26\cce00cbd\App_Web_b21g2v5x.5.cs Line: 0

Stack Trace:

[InvalidOperationException: Timeout expired. The timeout period
elapsed prior to obtaining a connection from the pool. This may have
occurred because all pooled connections were in use and max pool size
was reached.]
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection) +6264689
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory) +6265031
System.Data.SqlClient.SqlConnection.Open() +258
System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser
user) +65 System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe() +33
System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode() +32
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression
query) +63
System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
+45 LoopinDeals.Model.SiteDetails..ctor() +253 LoopinDeals.Admin.Default..ctor() +76
ASP.admin_default_aspx..ctor() in
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET
Files\root\f44daa26\cce00cbd\App_Web_b21g2v5x.5.cs:0
__ASP.FastObjectFactory_app_web_b21g2v5x.Create_ASP_admin_default_aspx()
in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET
Files\root\f44daa26\cce00cbd\App_Web_b21g2v5x.22.cs:0
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath
virtualPath, Type requiredBaseType, HttpContext context, Boolean
allowCrossApp, Boolean noAssert) +138
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context,
String requestType, VirtualPath virtualPath, String physicalPath) +50
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+425 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +263

I have added the SiteDetailsConstructor here, is it possible that this lead to connection pool problems ?

    public class SiteDetails
    {
        public DataClassesDataContext DbContext = new DataClassesDataContext();

        public SiteDetails()
        {
            var Details = from dtl in DbContext.SiteSettings
                          select dtl;

            foreach (var item in Details)
            {
                MetaIndexKeyword = item.MetaIndexKeyword;
                MetaIndexDesc = item.MetaIndexDesc;
                MetaIndexTitle = item.MetaIndexTitle;
                MetaGetPrefrenceKeyword = item.MetaGetPrefrenceKeyword;
                MetaGetPrefrenceDesc = item.MetaGetPrefrenceDesc;
                Logo = item.Logo;
                Favicon = item.Favicon;
                NoImage = item.NoImage;
                SiteName = item.SiteName;
                SiteTitle = item.SiteTitle;
                SiteUrl = item.SiteUrl;
                FbAndTwitterShareMessage = item.FbAndTwitterShareMessage;
                CharacterSet = item.CharacterSet;
                SiteMaintanance = item.SiteMaintanance;
                PasswordChar = item.PasswordChar;
                HtmlMetaKeyword = item.HtmlMetaKeyword;
                HtmlMetaDescription = item.HtmlMetaDescription;
                MetaDataGoogleSiteMap = item.MetaDataGoogleSiteMap;
                WebMasterEmail = item.WebMasterEmail;
                SupportEmail = item.SupportEmail;
                NoReplyName = item.NoReplyName;
                NoReplyEmail = item.NoReplyEmail;
                DeleteExpireDeals = item.DeleteExpireDeals;
                DealsPerPageBeforeLogin = item.DealsPerPageBeforeLogin;
                DealsPerPageAfterLogin = item.DealsPerPageAfterLogin;
                RecentViewDeals = item.RecentViewDeals;
                BoughtDeals = item.BoughtDeals;
                FbFanPage = item.FacebookFanPage;
                FbApplicationId = item.FbApplicationId;
                FbSecret = item.FbSecret;
                FbApiSharingDeals = item.FbApiSharingDeals;
                GoogleApiKey = item.GoogleApiKey;
                TwitterScreenName = item.TwitterScreenName;
                TwitterConsumerKey = item.TwitterConsumerKey;
                TwitterConsumerSecret = item.TwitterConsumerSecret;
                SharingAppId = item.SharingAppId;
                SharingAppSecret = item.SharingAppSecret;
                SharingCanvasURL = item.SharingCanvasURL;
                InviteMessage = item.InviteMessage;
                SharingMsgLink = item.SharingMsgLink;
                ShareMsgPicture = item.ShareMsgPicture;
                ShareMsgName = item.ShareMsgName;
                ShareMsgCaption = item.ShareMsgCaption;
                ShareMsgDesc = item.ShareMsgDesc;
            }

        }

        public static string MetaIndexKeyword { get; set; }
        public static string MetaIndexDesc { get; set; }
        public static string MetaIndexTitle { get; set; }
        public static string MetaGetPrefrenceKeyword { get; set; }
        public static string MetaGetPrefrenceDesc { get; set; }
        public static string Logo { get; set; }
        public static string Favicon { get; set; }
        public static string NoImage { get; set; }
        public static string SiteName { get; set; }
        public static string SiteTitle { get; set; }
        public static string SiteUrl { get; set; }
        public static string FbAndTwitterShareMessage { get; set; }
        public static string CharacterSet { get; set; }
        public static string SiteMaintanance { get; set; }
        public static string PasswordChar { get; set; }
        public static string HtmlMetaKeyword { get; set; }
        public static string HtmlMetaDescription { get; set; }
        public static string MetaDataGoogleSiteMap { get; set; }
        public static string WebMasterEmail { get; set; }
        public static string SupportEmail { get; set; }
        public static string NoReplyName { get; set; }
        public static string NoReplyEmail { get; set; }
        public static bool? DeleteExpireDeals { get; set; }
        public static int? DealsPerPageBeforeLogin { get; set; }
        public static int? DealsPerPageAfterLogin { get; set; }
        public static int? RecentViewDeals { get; set; }
        public static int? BoughtDeals { get; set; }
        public static string FbFanPage { get; set; }
        public static string FbApplicationId { get; set; }
        public static string FbSecret { get; set; }
        public static string FbApiSharingDeals { get; set; }
        public static string GoogleApiKey { get; set; }
        public static string TwitterScreenName { get; set; }
        public static string TwitterConsumerKey { get; set; }
        public static string TwitterConsumerSecret { get; set; }

        public static string SharingAppId { get; set; }
        public static string SharingAppSecret { get; set; }
        public static string SharingCanvasURL { get; set; }
        public static string InviteMessage { get; set; }
        public static string SharingMsgLink { get; set; }
        public static string ShareMsgPicture { get; set; }
        public static string ShareMsgName { get; set; }
        public static string ShareMsgCaption { get; set; }
        public static string ShareMsgDesc { get; set; }

    }
}
  • 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-06-03T22:28:29+00:00Added an answer on June 3, 2026 at 10:28 pm

    Having the context managed globally will not lead to connection pool problems. However, the context contains the DBSets with all of the data accessed so the context will grow until you run out of memory (assuming your DB is larger than your available memory).

    But the context is cheap to create, why would you want to manage it globally?

    — Response to new information

    I don’t think the code fragment you are showing is behind the issue at all. Rather, from looking at the code trace, the issue appears to be happening in the constuctor for SiteDetails. is this constructor loading a lot of data? if os, you could simply be overwhelming the database because too many threads are trying to load too much data.

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

Sidebar

Related Questions

I open a connection like this: Using conn as New OdbcConnection(connectionString) conn.Open() //do stuff
Open Ankh seems to be using Subversion 1.6.2 and Tortoise uses Subversion 1.6.0. After
open message box and long click on a message , as you can see
Open up Apple's Calendar app. When you name a new appointment, it automatically capitalizes
I'm trying to use mysql_real_escape_string() to secure a log in form. Using this code:
I'm wondering what the difference between... using (var db = new PteDotNetContext()) { var
I am using the Generic Repository pattern to abstract my EF 4.1 DbContext. I
I'm trying to reuse an existing database connection so that I can do multiple
I'm creating a multi-tenant Asp.Net MVC 3 Web app, and using EF4.1 code first
# Open new file to write file = None try: file = open(filePath, 'w')

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.