473,378 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Trap "connection pool" errors

We have implemented unhandled error trapping at the application level and
log these errors to our database. One error, however, the does not get
trapped is when the connection pool has exceeded the max number of
connections.

Obviously, we need to find the place(s) in our code where connections are
not closed correctly (espcially in loops), but I'm wondering if it's
possible to trap this error and to find out which part of our code (i.e.
stack trace) caused it. It seems logical that the application itself does
not produce the error since there is no error in the codebehind file. Is it
more of an IIS-level error?

Thanks in advance for your thoughts.

Sean
Nov 17 '05 #1
1 3194

"Sean Nolan" <sn****@harriton.com> wrote in message
news:Oa**************@tk2msftngp13.phx.gbl...
We have implemented unhandled error trapping at the application level and
log these errors to our database. One error, however, the does not get
trapped is when the connection pool has exceeded the max number of
connections.

Obviously, we need to find the place(s) in our code where connections are
not closed correctly (espcially in loops), but I'm wondering if it's
possible to trap this error and to find out which part of our code (i.e.
stack trace) caused it.


It it possible. The general idea is to have a object that will be Garbage
Collected in the same pass as your connection. Whenever a connection is
opened, store the stack trace of the opening method. And put a finalizer on
that object, and write out a trace entry if the finalizer runs and the
connection is still open.

One way to do this is to have a "wrapper object" for your connection.
But then your app code has to create the wrapper instead of the connection.
I think this is a good thing, since you can implement all the DAAB methods
as instance methods of your wrapper object. But that's another story.

Assuming you are using SQLServer (or some other connection that has a
StateChanged event), there may be an easier way.

Without a wrapper object, to get an object which will be finalized at the
same time as the connection we can use a "spy" object.

If we have a "spy" object which handles the StateChaned event of the
SQLConnection, and we give the spy object a reference to the connection we
will have what we want. If 2 objects mutually refer to each other, then
they will always be GC'd at the same time. The spy refers to the
SQLConnection and since the spy handles an event on teh SQLConnection, the
SQLConnection's delegate list contains a reference to the spy object. Voila!

There follows sample program to do this.

David

Imports System.Data.SqlClient

Class ConnectionFactory
Private Class ConnectionSpy
Private con As SqlConnection
Dim st As StackTrace
Public Sub New(ByVal con As SqlConnection, ByVal st As StackTrace)
Me.st = st

'latch on to the connection
Me.con = con
AddHandler con.StateChange, AddressOf StateChange
End Sub
Public Sub StateChange(ByVal sender As Object, ByVal args As
System.Data.StateChangeEventArgs)
If args.CurrentState = ConnectionState.Closed Then
'detach the spy object and let it float away into space
GC.SuppressFinalize(Me)
RemoveHandler con.StateChange, AddressOf StateChange
con = Nothing
st = Nothing
End If
End Sub
Protected Overrides Sub Finalize()
'if we got here then the connection was not closed.
Trace.WriteLine("WARNING: Open SQLConnection is being Garbage
Collected")
Trace.WriteLine("The connection was initially opened " & st.ToString)
End Sub
End Class

Public Shared Function OpenConnection(ByVal connect As String) As
SqlConnection
Dim con As New SqlConnection(connect)
con.Open()
Dim st As New StackTrace(True)
Dim sl As New ConnectionSpy(con, st)

Return con
End Function

End Class
Module Module1

Sub Main()
'pipe trace output to the console
'in your app this would go to a trace file
System.Diagnostics.Trace.Listeners.Add(New
TextWriterTraceListener(System.Console.Out))
Dim connect As String = "..."
Dim c As SqlConnection = ConnectionFactory.OpenConnection(connect)
c = Nothing '!!the connection was not closed

c = ConnectionFactory.OpenConnection(connect)
c.Close() 'this time it was closed
c = Nothing

GC.Collect(GC.MaxGeneration)
GC.WaitForPendingFinalizers()
'output will show 1 warning
End Sub

End Module

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leif Wessman | last post by:
I have a php-webpage that needs the database in the beginning and in the end of the script. In the middle there is a lot of processing that takes several seconds - during that time I don't use the...
0
by: Henry | last post by:
Hi, I have cases where I connect to the web site and MySQL is not working : Message : "Too many connection errors", Unblock with 'mysqladmin flush-hosts'. What I did is a script that check...
0
by: Bill | last post by:
Attempting to register (from a Win XP Pro machine) to my SQL Server 2000 machine (Win 2K Server), via Internet (http), error says "Errors occurred while connecting to 'mysqlserver'. Cannot open...
1
by: Muscha | last post by:
Hello, Every now and then my application throw this exception: "Unable to read data from the transport connection" And when I break into the Visual Studio, the thread where it failed has...
0
by: Philip Townsend | last post by:
I have a asp.net application that is periodically generating an error stating that the maximum number of connections has been exceeded. Is there a way to trap this error and reset the connection...
0
by: BGS | last post by:
I have a web site (www.on-the-matrix.com) that displays photos in a "slide show" format on ASPX pages that run in an inline frame on an ASP page (or in a separate pop-up window). The ASPX pages...
6
by: karim | last post by:
I have an asp.net page that stopped running properly giving the error below. The app uses a SQL Server 2000 on another server. Enterprise Manager and Query analyzer on the web server can connect to...
7
by: Henry | last post by:
I am writing a Windows forms VB.Net/MS SQL application via VS 2003 that utilizes Crystal Reports. I want to be able to dynamically set the report data source at run time. I'm trying to change...
2
by: one | last post by:
Hi, Was wondering if you could help me out with regards to a server that I'm trying to troubleshoot... Basically we have a windows 2003 load-balanced server that has got sharepoint and mcms...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.