473,396 Members | 1,975 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,396 software developers and data experts.

Leaking Database Connections from ASP.NET 2 Web App

Problem:
Database connections are not being reused properly. SP_WHO2 shows upwards
of 200 connections being created per page request. Most connections exist for
60 seconds then close without being reused. A few connections are reused. SQL
System Profiler shows many “Audit Login” and many “RPC: Completed” records
for each page request – often involving the exact same SQL statement called
in exactly the same manner from within a single program loop.

System Description:
• SQL Server 2005 (SP1)
• ASP.NET 2 (2.0.50727)
• IIS 6
• Windows 2003 Server (64bit production, x86 development – both having the
same issue)
• All critical updates applied

Connection String:
• Globally called from web.config
• Never modified
• Provider=SQLNCLI;Data Source=localhost;Initial Catalog=XDATABASE;User
Id=XUSERNAME;Password=XPASSWORD;DataTypeCompatibil ity=80;

Connection Details:
• All database connections connect through a common DB Connection class.
• All database calls are in the form of stored procedures
• All connections are closed and disposed

Page Example:

Dim myOLE As New OLESQL()
myOLE.SetSQL("spStoredProc " & intSomeParam)
myOLE.SetTableName("TableName")
Dim dsResults As DataSet = myOLE.ExDataSet()
'Do somthing with the dataset ...

For Each x In xGroup
myOLE.SetSQL("spStoredProc2 " & x.ToString)
intReturn = myOLE.ExScalar()

myOLE.SetSQL("spStoredProc3 " & x.ToString)
myOLE.ExNonQuery()
Next

myOLE.Close()

Database Connection Class:

Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Imports System.Data
Imports System
Imports System.Web.UI.Page

Public Class OLESQL

'Class fields
Public strSQL As String
Public strTableName As String
Public cnMP As OleDbConnection
Public sConnectionString As String =
ConfigurationManager.AppSettings("MPSQL")

'Initializer
Public Sub New()

cnMP = New OleDbConnection(sConnectionString)
cnMP.Open()

End Sub

'Close the database connection
Public Sub Close()

If cnMP.State = ConnectionState.Open Then
cnMP.Close()
cnMP.Dispose()
cnMP = Nothing
End If

End Sub

'Set the SQL value
Public Sub SetSQL(ByVal inSQL)
strSQL = inSQL
End Sub

'Set the table name value
Public Sub SetTableName(ByVal inTableName)
strTableName = inTableName
End Sub

'Execute a non query
Public Sub ExNonQuery()
Dim cmdData As New OleDbCommand(strSQL, cnMP)
cmdData.ExecuteNonQuery()
cmdData.Dispose()
cmdData = Nothing
End Sub

'Execute a scalar function
Public Function ExScalar() As String
Dim cmdData As New OleDbCommand(strSQL, cnMP)
Dim strOutput As String
strOutput = cmdData.ExecuteScalar
Return strOutput
cmdData.Dispose()
cmdData = Nothing
End Function

'Execute and return a dataset function
Public Function ExDataSet() As DataSet
Dim daGetData As New OleDbDataAdapter(strSQL, sConnectionString)
Dim dsData As New DataSet()
daGetData.Fill(dsData, strTableName)
Return dsData
daGetData.Dispose()
daGetData = Nothing
End Function

End Class
Oct 17 '06 #1
4 1946
you have shouldn't open a connecton on your constructor. all routines need a
try/catch to close the connection on errors.

-- bruce (sqlwork.com)

"Sierra" <Si****@discussions.microsoft.comwrote in message
news:F7**********************************@microsof t.com...
Problem:
Database connections are not being reused properly. SP_WHO2 shows upwards
of 200 connections being created per page request. Most connections exist
for
60 seconds then close without being reused. A few connections are reused.
SQL
System Profiler shows many "Audit Login" and many "RPC: Completed" records
for each page request - often involving the exact same SQL statement
called
in exactly the same manner from within a single program loop.

System Description:
. SQL Server 2005 (SP1)
. ASP.NET 2 (2.0.50727)
. IIS 6
. Windows 2003 Server (64bit production, x86 development - both having the
same issue)
. All critical updates applied

Connection String:
. Globally called from web.config
. Never modified
. Provider=SQLNCLI;Data Source=localhost;Initial Catalog=XDATABASE;User
Id=XUSERNAME;Password=XPASSWORD;DataTypeCompatibil ity=80;

Connection Details:
. All database connections connect through a common DB Connection class.
. All database calls are in the form of stored procedures
. All connections are closed and disposed

Page Example:

Dim myOLE As New OLESQL()
myOLE.SetSQL("spStoredProc " & intSomeParam)
myOLE.SetTableName("TableName")
Dim dsResults As DataSet = myOLE.ExDataSet()
'Do somthing with the dataset ...

For Each x In xGroup
myOLE.SetSQL("spStoredProc2 " & x.ToString)
intReturn = myOLE.ExScalar()

myOLE.SetSQL("spStoredProc3 " & x.ToString)
myOLE.ExNonQuery()
Next

myOLE.Close()

Database Connection Class:

Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Imports System.Data
Imports System
Imports System.Web.UI.Page

Public Class OLESQL

'Class fields
Public strSQL As String
Public strTableName As String
Public cnMP As OleDbConnection
Public sConnectionString As String =
ConfigurationManager.AppSettings("MPSQL")

'Initializer
Public Sub New()

cnMP = New OleDbConnection(sConnectionString)
cnMP.Open()

End Sub

'Close the database connection
Public Sub Close()

If cnMP.State = ConnectionState.Open Then
cnMP.Close()
cnMP.Dispose()
cnMP = Nothing
End If

End Sub

'Set the SQL value
Public Sub SetSQL(ByVal inSQL)
strSQL = inSQL
End Sub

'Set the table name value
Public Sub SetTableName(ByVal inTableName)
strTableName = inTableName
End Sub

'Execute a non query
Public Sub ExNonQuery()
Dim cmdData As New OleDbCommand(strSQL, cnMP)
cmdData.ExecuteNonQuery()
cmdData.Dispose()
cmdData = Nothing
End Sub

'Execute a scalar function
Public Function ExScalar() As String
Dim cmdData As New OleDbCommand(strSQL, cnMP)
Dim strOutput As String
strOutput = cmdData.ExecuteScalar
Return strOutput
cmdData.Dispose()
cmdData = Nothing
End Function

'Execute and return a dataset function
Public Function ExDataSet() As DataSet
Dim daGetData As New OleDbDataAdapter(strSQL, sConnectionString)
Dim dsData As New DataSet()
daGetData.Fill(dsData, strTableName)
Return dsData
daGetData.Dispose()
daGetData = Nothing
End Function

End Class

Oct 17 '06 #2
Agreed.
Public Sub New()

cnMP = New OleDbConnection(sConnectionString)
cnMP.Open()

End Sub
store the connection string, and do not open the connection here
private m_connectionString as string = string.empty

public sub new( connectionString as string )

me.m_connectionString = connectionString

end sub

OPEN LATE , CLOSE EARLY.

"bruce barker (sqlwork.com)" <b_*************************@sqlwork.comwrote
in message news:Od**************@TK2MSFTNGP05.phx.gbl...
you have shouldn't open a connecton on your constructor. all routines need
a
try/catch to close the connection on errors.

-- bruce (sqlwork.com)

"Sierra" <Si****@discussions.microsoft.comwrote in message
news:F7**********************************@microsof t.com...
Problem:
Database connections are not being reused properly. SP_WHO2 shows
upwards
of 200 connections being created per page request. Most connections
exist
for
60 seconds then close without being reused. A few connections are
reused.
SQL
System Profiler shows many "Audit Login" and many "RPC: Completed"
records
for each page request - often involving the exact same SQL statement
called
in exactly the same manner from within a single program loop.

System Description:
. SQL Server 2005 (SP1)
. ASP.NET 2 (2.0.50727)
. IIS 6
. Windows 2003 Server (64bit production, x86 development - both having
the
same issue)
. All critical updates applied

Connection String:
. Globally called from web.config
. Never modified
. Provider=SQLNCLI;Data Source=localhost;Initial Catalog=XDATABASE;User
Id=XUSERNAME;Password=XPASSWORD;DataTypeCompatibil ity=80;

Connection Details:
. All database connections connect through a common DB Connection class.
. All database calls are in the form of stored procedures
. All connections are closed and disposed

Page Example:

Dim myOLE As New OLESQL()
myOLE.SetSQL("spStoredProc " & intSomeParam)
myOLE.SetTableName("TableName")
Dim dsResults As DataSet = myOLE.ExDataSet()
'Do somthing with the dataset ...

For Each x In xGroup
myOLE.SetSQL("spStoredProc2 " & x.ToString)
intReturn = myOLE.ExScalar()

myOLE.SetSQL("spStoredProc3 " & x.ToString)
myOLE.ExNonQuery()
Next

myOLE.Close()

Database Connection Class:

Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Imports System.Data
Imports System
Imports System.Web.UI.Page

Public Class OLESQL

'Class fields
Public strSQL As String
Public strTableName As String
Public cnMP As OleDbConnection
Public sConnectionString As String =
ConfigurationManager.AppSettings("MPSQL")

'Initializer
Public Sub New()

cnMP = New OleDbConnection(sConnectionString)
cnMP.Open()

End Sub

'Close the database connection
Public Sub Close()

If cnMP.State = ConnectionState.Open Then
cnMP.Close()
cnMP.Dispose()
cnMP = Nothing
End If

End Sub

'Set the SQL value
Public Sub SetSQL(ByVal inSQL)
strSQL = inSQL
End Sub

'Set the table name value
Public Sub SetTableName(ByVal inTableName)
strTableName = inTableName
End Sub

'Execute a non query
Public Sub ExNonQuery()
Dim cmdData As New OleDbCommand(strSQL, cnMP)
cmdData.ExecuteNonQuery()
cmdData.Dispose()
cmdData = Nothing
End Sub

'Execute a scalar function
Public Function ExScalar() As String
Dim cmdData As New OleDbCommand(strSQL, cnMP)
Dim strOutput As String
strOutput = cmdData.ExecuteScalar
Return strOutput
cmdData.Dispose()
cmdData = Nothing
End Function

'Execute and return a dataset function
Public Function ExDataSet() As DataSet
Dim daGetData As New OleDbDataAdapter(strSQL, sConnectionString)
Dim dsData As New DataSet()
daGetData.Fill(dsData, strTableName)
Return dsData
daGetData.Dispose()
daGetData = Nothing
End Function

End Class


Oct 17 '06 #3
1) The Microsoft Database Access Application Block ("MS DAAB") works
great and will solve all your db-connect and connection pooling
problems. Download it as part of the MS Enterprise Library. Highly
recommended. Give it a try. The latest version is a huge improvement
over previous versions.

2) In an asp.net app, you shouldn't be making that many round-trips to
the db anyway, regardless of whether your connection pooling is
working. Consider making a single round-trip to the db if possible.
Where you have the "for each x in xGroup" loop, my guess is that you
don't really need to keep coming back to the web server... You could
gather up all of your "x" strings/ints and pass them to the db at once.
Then do something will all of them at once on the db side.

Oct 18 '06 #4
Thanks all.

Bruce and sloans' suggestions cut down on the number of connections created,
but only partially. Installing and using MS DAAB knocked them out of the park
completely without the need for a massive code overhaul. As an added benefit
the DAAB class replicated most of the functionality I was trying to implement
with my DB access class in the first place, but did it better!

"GroupReader" wrote:
1) The Microsoft Database Access Application Block ("MS DAAB") works
great and will solve all your db-connect and connection pooling
problems. Download it as part of the MS Enterprise Library. Highly
recommended. Give it a try. The latest version is a huge improvement
over previous versions.

2) In an asp.net app, you shouldn't be making that many round-trips to
the db anyway, regardless of whether your connection pooling is
working. Consider making a single round-trip to the db if possible.
Where you have the "for each x in xGroup" loop, my guess is that you
don't really need to keep coming back to the web server... You could
gather up all of your "x" strings/ints and pass them to the db at once.
Then do something will all of them at once on the db side.

Oct 18 '06 #5

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

Similar topics

3
by: Mudge | last post by:
Hi, My hosting provider only allows me to use 50 connections to my MySQL database that my Web site will use. I don't know what this 50 connections means exactly. Does this mean that only 50...
4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
11
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
5
by: John | last post by:
I have an ASP.NET 2.0 application developed in VB.net, that accesses an Microsoft Access database. When the database is on the same IIS server all works just fine. BUT when it tried to access the...
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
8
by: BD | last post by:
I am developing C# win form app to work with remote database on SQL Server 2005. Problem scenario is as follows: 1. a form is open that has downloaded dataset to local cache 2. computer is...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.