473,396 Members | 1,891 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.

Closing SQL Connections

Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i'm getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don't know what to do. Can anyone advise.

Mar 21 '06 #1
10 1398
I don't know what you are closing, but it is not the connection your declare
in your Try block.

You need to declare the variable outside the Try block, but get the
connection in it. Something like:

Dim sqlconn as SqlConnection
Try
sqlconn = dbconnection()
Catch

Finally
If Not IsNothing(sqlconn) Then
sqlconn.Close()
End If
End Try
"bannaman" <jk****@f2s.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i'm getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don't know what to do. Can anyone advise.

Mar 21 '06 #2
if you call close you do not need to call dispose.

-- bruce (sqlwork.com)
"bannaman" <jk****@f2s.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i'm getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don't know what to do. Can anyone advise.

Mar 21 '06 #3
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?

Mar 21 '06 #4
As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?

Mar 21 '06 #5
First, I would dim your sqlconn outside the Try block like a poster
suggested.

Second, when you say db = dbconnection() you are not actually creating
a new connection object you are just getting a reference to the
conneciton object that was created inside dbconnection(). So, when you
say, db.close() you are actually closing the original connection that
you opened and there is no need to do dbconneciton.close().

Lastly, step through your code in debug mode to see if the connection
is closing.

Mar 21 '06 #6
re:
As Bruce said, close it. Don't dispose it.
Interesting point for discussion, Kevin.

There's a question as to the *unmanaged* resources which need freeing,
over and above the need for freeing the *managed* .net resources.

See Rocky Lhotka's article :

http://www.lhotka.net/WeBlog/Dispose...ndObjects.aspx

"However, it also turns out that some Command objects really do have non-managed
resources that need to be disposed. Some don't. How do you know which do and
which don't? You need to ask the dev that wrote the code.

It turns out that SqlCommand has no un-managed resources, which is why most of us
have gotten away with this so far. However, OleDbCommand and OdbcCommand
do have un-managed resources and must be disposed to be safe."
Comments ?

btw, I just finished a fix for a client's application, developed by someone else,
and for which I was brought in as trouble-shooter, which was having resource problems.

It had 117 undisposed of OleDbCommand objects.

I disposed all of them...and the problem vanished.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl... As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?


Mar 21 '06 #7
I'll take your word for it, Juan. I haven't done any OleDb stuff in a couple
of years. I would not be surprised if you were correct!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
re:
As Bruce said, close it. Don't dispose it.


Interesting point for discussion, Kevin.

There's a question as to the *unmanaged* resources which need freeing,
over and above the need for freeing the *managed* .net resources.

See Rocky Lhotka's article :

http://www.lhotka.net/WeBlog/Dispose...ndObjects.aspx

"However, it also turns out that some Command objects really do have
non-managed
resources that need to be disposed. Some don't. How do you know which do
and
which don't? You need to ask the dev that wrote the code.

It turns out that SqlCommand has no un-managed resources, which is why
most of us
have gotten away with this so far. However, OleDbCommand and OdbcCommand
do have un-managed resources and must be disposed to be safe."
Comments ?

btw, I just finished a fix for a client's application, developed by
someone else,
and for which I was brought in as trouble-shooter, which was having
resource problems.

It had 117 undisposed of OleDbCommand objects.

I disposed all of them...and the problem vanished.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?



Mar 21 '06 #8
oledbcommand doesn't have a close so you must call its dispose to release
unmanged resources.

with SqlConnection, it has a Close method, which you can call instead of
dispose (all dispose does is call Close), which can make the code more
readable. also you can call open again, while it not recommened to reinit
disposed objects.

if you are using c# (or vb.net 2.0), you probably shoudl switch to using
(which will call dispose even on an error)
using (SqlConnection conn = new SqlConnection())
{
// my sql code here
}
-- bruce (sqlwork.com)


"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
re:
As Bruce said, close it. Don't dispose it.


Interesting point for discussion, Kevin.

There's a question as to the *unmanaged* resources which need freeing,
over and above the need for freeing the *managed* .net resources.

See Rocky Lhotka's article :

http://www.lhotka.net/WeBlog/Dispose...ndObjects.aspx

"However, it also turns out that some Command objects really do have
non-managed
resources that need to be disposed. Some don't. How do you know which do
and
which don't? You need to ask the dev that wrote the code.

It turns out that SqlCommand has no un-managed resources, which is why
most of us
have gotten away with this so far. However, OleDbCommand and OdbcCommand
do have un-managed resources and must be disposed to be safe."
Comments ?

btw, I just finished a fix for a client's application, developed by
someone else,
and for which I was brought in as trouble-shooter, which was having
resource problems.

It had 117 undisposed of OleDbCommand objects.

I disposed all of them...and the problem vanished.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?



Mar 21 '06 #9
heh, heh...

I wouldn't be surprised if Rocky was correct!

I practically worship the ground he walks on.

;-)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...
I'll take your word for it, Juan. I haven't done any OleDb stuff in a couple of years. I would not
be surprised if you were correct!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
re:
As Bruce said, close it. Don't dispose it.


Interesting point for discussion, Kevin.

There's a question as to the *unmanaged* resources which need freeing,
over and above the need for freeing the *managed* .net resources.

See Rocky Lhotka's article :

http://www.lhotka.net/WeBlog/Dispose...ndObjects.aspx

"However, it also turns out that some Command objects really do have non-managed
resources that need to be disposed. Some don't. How do you know which do and
which don't? You need to ask the dev that wrote the code.

It turns out that SqlCommand has no un-managed resources, which is why most of us
have gotten away with this so far. However, OleDbCommand and OdbcCommand
do have un-managed resources and must be disposed to be safe."
Comments ?

btw, I just finished a fix for a client's application, developed by someone else,
and for which I was brought in as trouble-shooter, which was having resource problems.

It had 117 undisposed of OleDbCommand objects.

I disposed all of them...and the problem vanished.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?



Mar 21 '06 #10
re:
all dispose does is call Close
Yup...so calling either will work for SqlConnection objects.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Bruce Barker" <br******************@safeco.com> wrote in message
news:u1**************@TK2MSFTNGP14.phx.gbl... oledbcommand doesn't have a close so you must call its dispose to release unmanged resources.

with SqlConnection, it has a Close method, which you can call instead of dispose (all dispose does
is call Close), which can make the code more readable. also you can call open again, while it not
recommened to reinit disposed objects.

if you are using c# (or vb.net 2.0), you probably shoudl switch to using (which will call dispose
even on an error)
using (SqlConnection conn = new SqlConnection())
{
// my sql code here
}
-- bruce (sqlwork.com)


"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
re:
As Bruce said, close it. Don't dispose it.


Interesting point for discussion, Kevin.

There's a question as to the *unmanaged* resources which need freeing,
over and above the need for freeing the *managed* .net resources.

See Rocky Lhotka's article :

http://www.lhotka.net/WeBlog/Dispose...ndObjects.aspx

"However, it also turns out that some Command objects really do have non-managed
resources that need to be disposed. Some don't. How do you know which do and
which don't? You need to ask the dev that wrote the code.

It turns out that SqlCommand has no un-managed resources, which is why most of us
have gotten away with this so far. However, OleDbCommand and OdbcCommand
do have un-managed resources and must be disposed to be safe."
Comments ?

btw, I just finished a fix for a client's application, developed by someone else,
and for which I was brought in as trouble-shooter, which was having resource problems.

It had 117 undisposed of OleDbCommand objects.

I disposed all of them...and the problem vanished.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
As Bruce said, close it. Don't dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?



Mar 21 '06 #11

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

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
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...
3
by: Anthony | last post by:
Hey all, Here's a question for you, my hosts have told me that that one my pages, php, was causing their server to reboot because there were too many open connections and that they should be...
6
by: MAB71 | last post by:
There should be a way to copy an access database ( .mdb file ) without closing connections to it. Unfortunately the FileCopy statement in VB gives an error if users are connected to the db. But I...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
3
by: blakecaraway | last post by:
Hi there. I have a C# console application that extracts data to flat files. Some of my business partners begin consuming these extract files before I've had a chance to write the newest ones (I...
13
by: Simon Harvey | last post by:
Hi All, I have a colleague that I wprk with that develops using ASP. I develop using ASP.net. He seems to make sites much faster than me and I am wondering if its because of the two different...
7
by: darrel | last post by:
We're running into a problem on our new site. Once a week or so, our site goes down with an 'out of memory error'. Rebooting the web server fixes things. Googling the error doesn't return many...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
7
by: Martien van Wanrooij | last post by:
I have been faced a couple of times with the situation that I wanted to write a script and was worried about a too frequent opening and closing mysql connections. To give some examples: 1)I...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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 project—planning, coding, testing,...

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.