[转载]ASP.NET Error – Adding the specified count to the semaphore would cause it to exceed its maximum count

http://jwcooney.com/2012/08/13/asp-net-error-adding-the-specified-count-to-the-semaphore-would-cause-it-to-exceed-its-maximum-count/

Justin Cooney / 2012/08/13

If you are working with ASP.NET in Visual Studio, then you may be mystified when you see an error code : Adding the specified count to the semaphore would cause it to exceed its maximum count.

This error had me wondering what exactly is going on…

Identifying the Problem

At first when I saw this error, I thought it might be due to the syntax of the query I was running. However after starting up Query Profiler I was able to run the query directly in SSMS without any issues. The problem clearly was somewhere in the application and not with the SQL query.

After investigating this, it appears that this error is related to ASP.NET’s ADO.NET connection pool manager. When you put together a connection string in your web.config file, you probably won’t be thinking about connection pooling, but by default this will be enabled. The connection pool manager will attempt to find a free connection, but if none are available it will throw the error we saw.

What is a Semaphore in SQL Server?

The first question I asked myself was what a semaphore is since I had not heard this term used before. Well, it seems that the definition of semaphore is just a locking mechanism used by SQL Server behind the scenes to prevent more than one task from accessing a data structure that is currently in use. In particular SQL Server protects user log caches using semaphores.

What is the cause of this problem?

So that was pretty interesting information. Basically with the error being returned, it appears that some sort of a lock was being retained and not released when my VB.NET application was communicating with SQL Server. It was definitely not a problem with long running queries or forgetting to clean up the connection object after execution was complete.

I’m still not able to pin down exactly what was causing this error to happen. People online speculate that similar problems appear to be caused by network issues. However, when I encountered the problem there were no noticeable network problems though, so I doubt that the network was causing the problem in my case.

Bottom line is that the semaphore locking issue appears to be  related to ASP.NET’s connection pooling. Resources were being locked in the pool and then were not being released, so I would see the semaphore error when my application was trying to access the application pool and no free resources were available.

Why Are Connection Pools Used By Default?

So since ADO.NET connection pools seem to be a possible point of failure, the question remains: why are connection pools enabled by default? The answer is performance.

There is a high performance hit involved with establishing a connection with a database, so ADO.NET tries to increase performance by not destroying connections after a call has happened to a database. Rather, ADO.NET puts the released connection into a pool which it holds for the next time that a request to the database is made. In general this ends up returning database results much faster than if connection pooling is disabled.

Of course we have also seen the down-side to connection pooling, which is the semaphore error where ADO.NET tries to access a connection in the pool, and finds that it can’t.

In the case of my application I decided that the possible performance improvement gained by using connection pooling was outweighed by the possibility of getting ugly connection errors such as the semaphore error, so next I will explain how to ‘fix’ the semaphore error by disabling ADO.NET connection pooling.

Fixing the Problem

The simplest way to fix the ADO.NET semaphore error is to disable connection pooling in the connection string of your web.config file.

Here is an example of what a default connection string might look like. Although it doesn’t specify a connection pooling option, this is enabled by default:

<add name="testConnection" connectionString="Data Source=MyDBServer;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=testUserId;Password=TestPassword;"
 providerName="System.Data.SqlClient" />

Now to disable pooling and get rid of the error message we were seeing, we simply append the directive Pooling=False to the end of our connection parameters as follows:

<add name="testConnection" connectionString="Data Source=MyDBServer;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=testUserId;Password=TestPassword;Pooling=False;"
 providerName="System.Data.SqlClient" />

Pretty simple, right?

Sometimes finding the reason for a problem is more difficult than fixing the problem. Now that I know what was causing the error I will find it easier to diagnose similar problems in future!

时间: 2024-10-23 07:03:44

[转载]ASP.NET Error – Adding the specified count to the semaphore would cause it to exceed its maximum count的相关文章

error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status

Windows服务器Azure云编译安装MariaDB教程 www.111cn.net 编辑:future 来源:转载 安装MariaDB数据库最多用于linux系统中了,下文给各位介绍在Windows服务器Azure云编译安装MariaDB教程,希望本文能帮助到各位. 试用1元Windows Azure,带宽都是杠杠的.下面演示下Windows Azure下编译安装LNMP环境,系统环境是Ubuntu 14.04 TLS版 软件版本: 数据库:mariadb-10.0.13 Stable PH

[转载]ASP.NET伪静态页面的实现和伪静态在IIS7.0中的配置

ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置 最近在写一个CMS的内容管理系统,需要用到一个伪静态方面的方法,在网上找了一下这方面的资料,现将这方面的资源记录一下. 本机环境: Window server 2008 R2 Enterprise 操作系统+SQL Server 2008 R2 Enterprise企业版数据+Visual Studio 2010 SP1 普及一下伪静态方面的知识, 1.伪静态简单一点儿来说,其实就是重写了URL,最大的好处便于搜索引擎抓取,因

/usr/local/ssl/lib/libssl.a: error adding symbols: Bad value

一.背景 编译第三方库A的时候提示依赖openssl库. 二.编译openssl库 去官网下载最新版本的openssl-1.0.2l.tar.gztar -zxvf openssl-1.0.2l.tar.gzcd openssl-1.0.2l ./config make make install 完成编译,默认openssl的库安装在/usr/local/ssl目录下. 三.引用openssl库 第三方库[A]编译引用openssl的时候(make的时候),提示这个错误: /usr/local/

[转载]ASP.NET对路径&quot;xxxxx&quot;的访问被拒绝的解决方法小结

异常详细信息: System.UnauthorizedAccessException: 对路径“D:/temp1/MyTest.txt”的访问被拒绝 在windows 2003下,在运行web应用程序的时候出现一下错误: 服务器无法处理请求,-->对路径“C:/temp/mytest.txt”的访问拒绝 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.UnauthorizedAccess

GLFW3出error adding symbols: DSO missing from command line解决

背景:使用OpenGL的GLFW3.1库的时候,使用其中一些代码 报error adding symbols: DSO missing from command line 因为使用的是Qcreator,解决方法是在.pro文件里加一些库 LIBS +=-lpthread LIBS +=-lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor 找具体缺少的库使用 pkg-config Linux 命令行运行 pkg-config --print-requ

ASP.NET Error

Error Handler http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling ASP.NET Error

转载 ASP.NET MVC中使用ASP.NET Identity - 新西兰程序员 - 博客园

转载原地址: http://blog.jobbole.com/90695/ 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP.NET Identity的建立和使用,包括基础类的搭建和用户管理功能的实现-- http://myusermanagement.azurewebsites.net/Account/Login?ReturnUrl=%2F 点此

(转载)ASP.NET Quiz Answers: Does Page.Cache leak memory?

原文地址:http://blogs.msdn.com/b/tess/archive/2006/08/11/695268.aspx "We use Page.Cache to store temporary data, but we have recently discovered that it causes high memory consumption. The bad thing is that the memory never goes down even though the cach

[转载] ASP.NET MVC (一)——深入理解ASP.NET MVC

个人认为写得比较透彻得Asp.net mvc 文章,所以转载过来,原文链接在最后: ASP.NET vs MVC vs WebForms 许多ASP.NET开发人员开始接触MVC认为MVC与ASP.NET完全没有关系,是一个全新的Web开发,事实上ASP.NET是创建WEB应用的框架而MVC是能够用更好的方法来组织并管理代码的一种更高级架构体系,所以可以称之为ASP.NET MVC. 我们可将原来的ASP.NET称为 ASP.NET Webforms,新的MVC 称为ASP.NET MVC. A