Click here to Skip to main content
15,881,852 members
Articles / Web Development / IIS

Find "Leaked" Database Connections in ASP.NET Web Applications

Rate me:
Please Sign up or sign in to vote.
4.62/5 (11 votes)
9 Dec 2005CPOL7 min read 122.8K   1.5K   45  
Presents a method for monitoring the amount of time database connections stay open in ASP.NET applications.
using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;

using PSI;

namespace ConnectionMontiorTest 
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start( Object sender, EventArgs e )
		{

		}
 
		protected void Session_Start( Object sender, EventArgs e )
		{

		}

		protected void Application_BeginRequest( Object sender, EventArgs e )
		{

		}

		protected void Application_EndRequest( Object sender, EventArgs e )
		{

		}

		protected void Application_AuthenticateRequest( Object sender, EventArgs e )
		{

		}

		protected void Application_Error( Object sender, EventArgs e )
		{

		}

		protected void Session_End( Object sender, EventArgs e )
		{

		}

        protected void Application_End( Object sender, EventArgs e )
        {
            ConnectionMonitor monitor = Application[ConnectionMonitor.CacheName] as ConnectionMonitor;

            if ( monitor != null )
            {    
                EventLog logger = monitor.Logger;

                if ( logger != null )
                {
                    monitor.ForceLogging();
                    logger.WriteEntry( "---Application Ending---", EventLogEntryType.Information );
                }
                
                //Uncomment out the following statement to close connections
                //that have been opened for longer than 180 seconds
                //monitor.Close( 180 );

            }
        }
			
		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

        }
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Todd has been a professional software developer for over 20 years, working in a variety of different languages including C, C++, C#, Basic, (and this really dates him) COBOL and RPG. He has been working with Microsoft operating systems since the early days of DOS.

He and his wife currently co-own a firm that provides continuing education for CPAs and financial professionals.

Comments and Discussions