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.

Topics: Administration, Core, General, Troubleshooting
Sep 4, 2013 at 8:49 PM
I am getting the following error on startup of my orchard site. Anyone know what could be causing this issue. From what I can tell the site has been up for a few hours, with minimal trafffic (maybe 10 visits or less) and when I now try to go to it I get this error.

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.



[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.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +6711993
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) +6712511
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) +152
System.Data.SqlClient.SqlConnection.Open() +229
NHibernate.Connection.DriverConnectionProvider.GetConnection() +238
NHibernate.AdoNet.ConnectionManager.GetConnection() +95
NHibernate.AdoNet.AbstractBatcher.Prepare(IDbCommand cmd) +43

[ADOException: While preparing SELECT TOP (@p0) this_.Id as Id94_2_, this_.Number as Number94_2_, this_.Published as Published94_2_, this_.Latest as Latest94_2_, this_.Data as Data94_2_, this_.ContentItemRecord_id as ContentI6_94_2_, contentite1_.Id as Id93_0_, contentite1_.Data as Data93_0_, contentite1_.ContentType_id as ContentT3_93_0_, contenttyp4_.Id as Id95_1_, contenttyp4_.Name as Name95_1_ FROM Orchard_Framework_ContentItemVersionRecord this_ inner join Orchard_Framework_ContentItemRecord contentite1_ on this_.ContentItemRecord_id=contentite1_.Id left outer join Orchard_Framework_ContentTypeRecord contenttyp4_ on contentite1_.ContentType_id=contenttyp4_.Id WHERE contentite1_.Id = @p1 and this_.Published = @p2 an error occurred]
NHibernate.AdoNet.AbstractBatcher.Prepare(IDbCommand cmd) +390
NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) +84
NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session) +600
NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +273
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +205
NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +410
NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet
1 querySpaces, IType[] resultTypes) +236
NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) +60
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +1055
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +79
Orchard.ContentManagement.DefaultContentManager.GetManyImplementation(QueryHints hints, Action2 predicate) +1963
Orchard.ContentManagement.DefaultContentManager.Get(Int32 id, VersionOptions options, QueryHints hints) +187
Orchard.Security.Providers.FormsAuthenticationService.GetAuthenticatedUser() +296
Orchard.Security.CurrentUserWorkContext.<Get>b__0(WorkContext ctx) +29
Orchard.Environment.<>c__DisplayClass7
1.<FindResolverForState>b__5() +19
Orchard.Environment.WorkContextImplementation.GetState(String name) +119
Orchard.Security.Authorizer.Authorize(Permission permission, IContent content, LocalizedString message) +106
Orchard.Security.SecurityFilter.OnAuthorization(AuthorizationContext filterContext) +416
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +156
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +720
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__19() +40
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) +42
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
Coordinator
Sep 4, 2013 at 8:51 PM
Pretty hard to say with so little information. What additional modules do you have? Did you try to disable those one by one?
Developer
Sep 4, 2013 at 9:02 PM
Maybe connection pooling? (I am just throwing ideas out there)
Sep 4, 2013 at 9:48 PM
We have a bunch of custom modules for our site (around 15). There is one background task that runs every minute to send orders to a web service.

If you look at the inner error it is timing out trying to run this command.

SELECT TOP (@p0) this_.Id as Id94_2_, this_.Number as Number94_2_, this_.Published as Published94_2_, this_.Latest as Latest94_2_, this_.Data as Data94_2_, this_.ContentItemRecord_id as ContentI6_94_2_, contentite1_.Id as Id93_0_, contentite1_.Data as Data93_0_, contentite1_.ContentType_id as ContentT3_93_0_, contenttyp4_.Id as Id95_1_, contenttyp4_.Name as Name95_1_ FROM Orchard_Framework_ContentItemVersionRecord this_ inner join Orchard_Framework_ContentItemRecord contentite1_ on this_.ContentItemRecord_id=contentite1_.Id left outer join Orchard_Framework_ContentTypeRecord contenttyp4_ on contentite1_.ContentType_id=contenttyp4_.Id WHERE contentite1_.Id = @p1 and this_.Published = @p2

It is likely one of the first queries ran I'm guessing as this query does not look like anything we have written.
Coordinator
Sep 4, 2013 at 9:51 PM
One of those modules is probably not releasing its connection. That thing that talks to a web service in a background task is a prime suspect, yes.
Developer
Sep 5, 2013 at 1:04 AM
Edited Sep 5, 2013 at 1:10 AM
It doesn't matter which query you see here - it's not the one causing this exception. It's just the first that happened after reaching the max pool size.

There's definitely some connection leaking happening somewhere. Default max. connection pool size is 100 for SQL Server iirc. Normally, it's enough even when you hammer the Orchard app to its limits.

Try monitoring the number of active connections - there is a performance counter for this. This may give you some hints.
Don't know how you access your database in those background tasks, but make sure to properly dispose the session and close the connection.
Sep 29, 2013 at 7:44 PM
Do we have to do anything special in case we access the database thru the content manager in background tasks? Do we need to handle exceptions or will orchard handle them and close/rollback session correctly?
Coordinator
Sep 29, 2013 at 10:23 PM
In principle no, but something is not quite working as it should here, obviously. What you need to do is monitor it as Piotr suggests, then maybe use the SQL profiler to identify what queries and what bits of code are keeping the connections open (provided that's what's happening).