SQLConnection Leakage Issue (Timeout Expired due to Pooling - ASP.NET / SQL Server) - c#

I am developing a web application which is based on ASP.NET 4.0, jQuery, AJAX, Javascript using SQL Server 2008 database. Our application is an ERP application which has almost around 400 to 500 users using the application. We have made it live since a month ago.
Now, I'm facing a timeout issue and application crashed literally which is occurred after some hours of application usage.
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:
Line 36: param[1].Value = Password;
Line 37:
Line 38: return SqlHelper.ExecuteDataset(CDSGlobal.ConnectionString, CommandType.StoredProcedure, "ValidateLogin_sp", param);
Line 39: }
Line 40:
Source File: e:\abcd\App_Code\User.cs Line: 38
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) +6352273
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6352606
System.Data.SqlClient.SqlConnection.Open() +300
DBConn.SqlHelper.ExecuteDataset(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) +270
User.ValidateLogin(String Employee_Code, String Password) in e:\abcd\App_Code \User.cs:38
User_UserLogin.btnLogin_Click(Object sender, EventArgs e) in e:\abcd \User\UserLogin.ascx.cs:79
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
For temporary resolution, we are restarting the server and restarting the MSSQLSERVER service.
After searching for above, I came to know it is due to connection leakage. Some opened connections are never get closed.
I am using ready made Microsoft Data Access Application Block for .NET i.e. SQLHelper Class which in my application for data access using Dataset and all.
Note: I am NOT using SqlDataReader anywhere.
I have examined the class and its closing the connections and I have NEVER used manual connection code instead used SQLHelper everywhere.
I also have the following nested transactions structure in stored procedures as follows:
BEGIN TRY
BEGIN TRANSACTION
-- Every below stored procedure has the transation structure as this SP i.e. Parent SP. (BEGIN TRY, BEGIN TRANSACTION etc)
exec sp1
exec sp2
exec sp3
---
---
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF ##TRANCOUNT>0
ROLLBACK TRANSACTION
PRINT 'ERROR OCCURED In SP'
END CATCH
Now, The problem is why connection leakage is occurring.
Is above transaction structure is responsible i.e it is blocking the transactions and requests.
If request is in process and user has closed the browser, does the application closes the connection?
Any help will be greatly appreciated.

Solved !!
It was not due to Nested Transactions but it was due to Bad Indexes Strategy used. Used normal Indexes and Indexes with include to solve the performance issues.
Also ran SQL Profiler to examine the most read and writes on columns and accordingly applied indexes and review maintenance and execution plans. !!

You can also use this,
SqlConnection.ClearAllPools();
The above code then closes all of the pools upon execution. I had the same problem and cleared the pools each time and then it should work successfully due to the pools being cleared.

Related

How to resolve SQL timeout error in a window service application?

I have a web app which has been added as a reference to my window service app. The service will call a method in the web app which then will invoke the database to run a stored procedure.
I received an exception while calling one of my store proc. The stored procedure will approximately run for 45 seconds. Below is the error
System.Data.SqlClient.SqlException (0x80131904): Execution Timeout
Expired. The timeout period elapsed prior to completion of the
operation or the server is not responding. --->
System.ComponentModel.Win32Exception (0x80004005): The wait operation
timed out
I have tried to increase the service app timeout by appending ';Connection Timeout=3600' to the service app conn string in web.config but the exception still occurred after around 30 seconds of running.
Your help to resolve this issue will be greatly appreciated.
To elaborate on #Squirrel comment:
Your error message shows a System.Data.SqlClient.SqlException. So the timeout is on SQL Server side.
So if you want to increase the timeout, it must be done on the SQL Server side.
In the System.Data.SqlClient namespace you seem to use, the SqlCommand class that you probably use to call your stored procedure has a property CommandTimeout that let you set the timeout value.
See the documentation
CommandTimeout
Gets or sets the wait time (in seconds) before
terminating the attempt to execute a command and generating an error.

Oracle Error: Pooled connection request timed out

Im using Oracle12c with the application written in C# and using Oracle.ManagedDataAccess.dll to handle the DB Connection.
A product we have has started to occasionally throw this exception after running fine for years:
Oracle.ManagedDataAccess.Client.OracleException (0xFFFFFC0C): Pooled connection request timed out
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)
at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
I know the cause of this error. Looking at the code neither the OracleConnection or the OracleCommand objects are being disposed. So these connections are building up until it eventually throws this exception.
The fix is straight forward. Wrap these in Using statements. I dont need help with that.
However my interests me is why this problem has started now. This software was running for years without issue. They did some database maintenance, updated other software on the same server, then this problem started. I dont know what Db maintenance they did.
The connection string in the application does not specify any pool attributes.
Is there a oracle db setting which would cause a lower amount of simultaneous connections in the database that could have caused this to start occurring?
UPATE
I wrote a little test app to check the limit.
It just loops around and opens a connection, performs a basic query and doesnt dispose the connection.
On my test system it starts throwing this exception after 640ish loops. It varies give or take 10 loops each time i run it.
What is setting this limit?
I just had the same problem.
The reason, why you get that exception is, that the Oracle Pool Manager doesn't have a free connection anymore (per default you can have up to 100 connections). Often the reason for that problem are not closed connections (so 'using' was the right ways).
Even though you already found a solid solutions for that problem, I want to add those:
Try/catch and using is always a good idea
Adjust the Min Pool Size (or Max Pool Size, but default is 100 should be enough): So there's always a connection ready to use and the connection doesn't have to get established (see https://docs.oracle.com/en/database/oracle/oracle-database/21/jjucp/optimizing-ucp-behavior.html#GUID-FFCAB66D-45B3-4D7B-991B-40F1480630FD)
Update to Oracle.ManagedDataAcess >= 21.8.0 (
Bug 34322469 - CONNECTION POOL THROWS "CONNECTION REQUEST TIMED OUT" EXCEPTION DUE TO LOOPING WITHIN POOLMANAGER.GET() is fixed there)

C# Connection timeout to oracle database when application pool recycles

We have a web page which uses ODP.Net to connect to oracle database from ASP MVC page. During the heavy user load if application pool recycles for any reason, than we get few connection timeout errors from internal oracle class.
Inner Exception: StackTrace: at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
How can we handle this scenario ? what could be the reason for this error ?
If you are using a new connection string that differs per user or with some other variable parameter you can exceed your max connections. Pooling only works if you use the exact same string. Under the covers your "close" is ignored and the connection manager keeps the connection open. In SQL Server you can see a connection reset in the trace console every time an existing connection is reused.
Oracle probably has something similar.
Tracing your connections and possibly connection resets will most likely shed some light on what is creating so many connections.

Fatal error causing timeout -mysql

MySql.Data.MySqlClient.MySqlException:
Fatal error encountered during command
execution. --->
MySql.Data.MySqlClient.MySqlException:
Timeout expired. The timeout period
elapsed prior to completion of the
operation or the server is not
responding. at
MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior
behavior) --- End of inner exception
stack trace
What does this error mean? I have max connection pool =200 in the connection string? my app is web app in .net connecting mysql db.
Connection pooling isn't the answer to this problem. Connection pooling allows multiple connections to be made to the server and then recycled to avoid having to reestablish the connection. Establishing and reestablishing connections can be very expensive in time and computing resources.
What you're looking for is to increase the command timeout. This can be done one of two ways. Either in the connection string by specifying default command timeout or by assigning a value to the CommandTimeout property on the MySqlCommand.

Exception "The operation is not valid for the state of the transaction" using TransactionScope

We have a web service on server #1 and a database on server #2.
Web service uses transaction scope to produce distributed transaction. Everything is correct.
And we have another database on server #3. We had some problems with this server and we reinstalled operation system and software. We configured MSDTC and tried to use web service from server #1 to communicate with database on this server.
And now after first select statement within transaction scope we get: The operation is not valid for the state of the transaction. This exception falls in every web service request if it is using transaction scope.
Server #2 and Server #3 is almost similar. The difference can be only in settings.
.NET framework 3.5 SP1 installed and SQL Server SP3 on all servers.
Full stacktrace:
System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction
tx, IPromotableSinglePhaseNotification
promotableSinglePhaseNotification, Transaction atomicTransaction) в
System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification
promotableSinglePhaseNotification) в
System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction
t в System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction t в
System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction
transaction) в
System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction
transaction) в
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject) в
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection) в
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory) в
System.Data.SqlClient.SqlConnection.Open() в
NHibernate.Connection.DriverConnectionProvider.GetConnection() в
NHibernate.Impl.SessionFactoryImpl.OpenConnection()
I searched this message but didn't found any appropriate solution.
So what settings should I check and what exactly should I do to fix it?
Lanfear, I have come across the same error message and I found a solution. Your situation may be different, but I hope that the following knowledge will be useful to you.
System.Transactions.Transaction.Current.TransactionInformation.Status will return the status of the current transaction.
In each case where an exception is thrown with the message The operation is not valid for the state of the transaction, when I step thru with the debugger, I see that the status is "Aborted" before the exception is thrown.
In my case, the problem was caused by nesting two transactions inside each other and mistakenly aborting both when I wanted to abort only one. Apparently, if you use the TransactionScope default constructor New TransactionScope() with two nested transactions, aborting the inner transaction aborts the outer transaction as well. The solution is to use the Constructor New TransactionScope(TransactionScopeOption.RequiresNew) Using this constructor, the inner transaction will be a new transaction and aborting it will not abort the outer transaction.
This solved my issue.
Enabling the Distributed Transaction Coordinator solved the issue for me (this was done on both machines: the one that has the web service and the one that has the database. Not sure though if it is needed for the web service).
I followed the steps described here to enable the DTC and to add an exception to the Windows Firewall: Enable Network DTC Access for Windows Server 2008

Categories