473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about IDispose

with the using construct, exit from scope calls dispose on said object. But
what happens in a connection pooling scenario? Is the run-time smart enough
to realize that Object Pooling is being used so it will not call dispose and
instead flag the object similar to what close() does when pooling is in
effect. Or does it really go ahead and shut down the object? If it does,
then isn't it circumventing the object pooling? Does anybody know?

I ask because I often use the using construct instead of calling close, but
I wonder if I am introducing expense into the equation if the dispose is not
behaving efficiently like I assumed.

??

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Nov 15 '05 #1
9 2011

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
with the using construct, exit from scope calls dispose on said object.
But
what happens in a connection pooling scenario? Is the run-time smart
enough
to realize that Object Pooling is being used so it will not call dispose
and
instead flag the object similar to what close() does when pooling is in
effect. Or does it really go ahead and shut down the object? If it does,
then isn't it circumventing the object pooling? Does anybody know?

I ask because I often use the using construct instead of calling close,
but
I wonder if I am introducing expense into the equation if the dispose is
not
behaving efficiently like I assumed.

Using should blindly call Dispose. It would be up to the object implementer
to write a Dispose method that works properly with the pooling system. You
will have to analyze the behaviour of the class in question.

??

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b

Nov 15 '05 #2
so what exactly is the point of this in a web page (windows forms doesn't
care about pooling for the most part because it is not inherently
expensive).
using(MySQLConnection cn = new MySQLConnection (Global._connString3))

{

}

I am not in agreement with this implementation at all. Because it is
inherently costly at the expense of being safe. Is it an either safety or
performance cake? That totally negates the benefit of connection pooling. It
should come with a disclaimer as well because I always assumed it worked
with some sort of intelligence.

Give me a good reason why dispose should not have some built-in intelligence
with regard to object pooling. I need this intelligence for web development
because object creation is inherently expensive. It doesn't matter for
windows program. I suspuct web program was an after thought. This is wrong
in my limited view of what is happening.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Oo******************@tk2msftngp13.phx.gbl...

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
with the using construct, exit from scope calls dispose on said object.
But
what happens in a connection pooling scenario? Is the run-time smart
enough
to realize that Object Pooling is being used so it will not call dispose
and
instead flag the object similar to what close() does when pooling is in
effect. Or does it really go ahead and shut down the object? If it does,
then isn't it circumventing the object pooling? Does anybody know?

I ask because I often use the using construct instead of calling close,
but
I wonder if I am introducing expense into the equation if the dispose is
not
behaving efficiently like I assumed.

Using should blindly call Dispose. It would be up to the object

implementer to write a Dispose method that works properly with the pooling system. You
will have to analyze the behaviour of the class in question.

??

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b


Nov 15 '05 #3
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
so what exactly is the point of this in a web page (windows forms doesn't
care about pooling for the most part because it is not inherently
expensive).
using(MySQLConnection cn = new MySQLConnection (Global._connString3))

{

}

I am not in agreement with this implementation at all. Because it is
inherently costly at the expense of being safe. Is it an either safety or
performance cake? That totally negates the benefit of connection pooling. It
should come with a disclaimer as well because I always assumed it worked
with some sort of intelligence.

Give me a good reason why dispose should not have some built-in intelligence
with regard to object pooling.
Because IDisposable is a very general interface, and shouldn't refer to
anything specific. It's a horrible, horrible idea for something as
general as IDisposable to know about databases - it's putting knowledge
in entirely the wrong place.

The *right* place for the "smarts" is in MySQLConnection, which can
implement IDisposable however it wishes.
I need this intelligence for web development
because object creation is inherently expensive. It doesn't matter for
windows program. I suspuct web program was an after thought. This is wrong
in my limited view of what is happening.


Are you *sure* that using the above pattern is actually ignoring
connection pooling? Don't assume that there's a direct correlation
between MySQLConnection instances and actual connections to the
database.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:Of****************@TK2MSFTNGP09.phx.gbl...
so what exactly is the point of this in a web page (windows forms doesn't
care about pooling for the most part because it is not inherently
expensive).
using(MySQLConnection cn = new MySQLConnection (Global._connString3))

{

}

I am not in agreement with this implementation at all. Because it is
inherently costly at the expense of being safe. Is it an either safety or
performance cake? That totally negates the benefit of connection pooling.
It
should come with a disclaimer as well because I always assumed it worked
with some sort of intelligence.

Why would you assume that? connection\object pooling is a feature of a
library that should(usually) be transparent to user and language. There is
*nothing* in the language that supports or detracts from the feature.

Give me a good reason why dispose should not have some built-in
intelligence
with regard to object pooling. I need this intelligence for web
development
because object creation is inherently expensive. It doesn't matter for
windows program. I suspuct web program was an after thought. This is wrong
in my limited view of what is happening. The primary reason is that there is no reason to be. It is the duty of the
implementer of the object to implement pooling in their class. All versions
of the MS provided connection objects appear to achieve the same basic
results no matter if dispose or close is called. How the pooling of the
actual connection works internally shouldn't be destroyed with the dispose
call(perhaps using reference counts or something, thats irrelevent).

Can you give a good reason why intelligence should be there? Why the
changes, for example additional attributes being defined, additional logic
in using(emitting code to check for the attributes on overloads, etc) would
make sense? Considering the actual amount of code needed and the fact it
would probably be performance impairing(calling reflection to check for the
attribute to determine if it participates in a pool, determining how to
dispose the object, etc). Simply writing Dispose so that it allows pooling
to operate makes far more sense. One would hope the MySqlConnection class
actually does this, if not its an issue with the library you are using, not
the language itself.


--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Oo******************@tk2msftngp13.phx.gbl...

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
> with the using construct, exit from scope calls dispose on said object.
> But
> what happens in a connection pooling scenario? Is the run-time smart
> enough
> to realize that Object Pooling is being used so it will not call
> dispose
> and
> instead flag the object similar to what close() does when pooling is in
> effect. Or does it really go ahead and shut down the object? If it
> does,
> then isn't it circumventing the object pooling? Does anybody know?
>
> I ask because I often use the using construct instead of calling close,
> but
> I wonder if I am introducing expense into the equation if the dispose
> is
> not
> behaving efficiently like I assumed.
>


Using should blindly call Dispose. It would be up to the object

implementer
to write a Dispose method that works properly with the pooling system.
You
will have to analyze the behaviour of the class in question.

> ??
>
> --
> Regards,
> Alvin Bruney [ASP.NET MVP]
> Got tidbits? Get it here...
> http://tinyurl.com/3he3b
>
>



Nov 15 '05 #5
Probably I was misunderstood. I'll restate for clarity.

Implementing the using construct disposes of the object in question. The
connection pool is specifically setup to prevent object destruction because
it is inherently expensive to create this object. It is much more efficient
to reuse the object instead of destroying it.

This behavior poses no problem in a windows environment because connections
are cheap, however, the reverse is true in a web environment.

Should the dispose pattern be smart enough to detect pooling? After thinking
about it, Jon is right. Because of how the finalizer works, it is faulty to
place dependency in the overridden dispose function. So it cannot check for
a connection pool flag before destroying the object, at least based on
microsoft's best practices. So the *fix cannot come from the Dispose layer.

Should the fix be implemented at a higher level such as the connection
object layer? After thinking about it, I don't think so for the same reason;
it forces the connection object into knowing more than it is supposed to
about the environment in which it is run. But this doesn't solve the
problem. The problem still remains that the benefits of the pool are being
negated. Why is this important? Consider a high volume website fielding
20000 hits per day. The benefits of pooling here make or break the website.
Negating these benefits is simply not acceptable and must be avoided. But
how can I avoid it if I don't know the negative effects?

What I am suggesting is that Microsoft append a qualifying comment to the
dispose pattern literature indicating that the using construct which
implements the IDispose pattern not be used in combination with object
pooling for web applications because the benefits of pooling may be
negatively affected. With that recommendation in the literature, I can
safely avoid the using construct at a high level and choose to manually
close my objects guaranteeing that the connection pool benefits remain in
effect since calling close() only flags the object for reuse anyway.
Are you *sure* that using the above pattern is actually ignoring connection pooling?

No, I am not 100%sure. But based on the literature, I make an educated guess
that it ignores pooling because the dispose implementation must make no
assumptions about the environment.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
so what exactly is the point of this in a web page (windows forms doesn't care about pooling for the most part because it is not inherently
expensive).
using(MySQLConnection cn = new MySQLConnection (Global._connString3))

{

}

I am not in agreement with this implementation at all. Because it is
inherently costly at the expense of being safe. Is it an either safety or performance cake? That totally negates the benefit of connection pooling. It should come with a disclaimer as well because I always assumed it worked
with some sort of intelligence.

Give me a good reason why dispose should not have some built-in intelligence with regard to object pooling.


Because IDisposable is a very general interface, and shouldn't refer to
anything specific. It's a horrible, horrible idea for something as
general as IDisposable to know about databases - it's putting knowledge
in entirely the wrong place.

The *right* place for the "smarts" is in MySQLConnection, which can
implement IDisposable however it wishes.
I need this intelligence for web development
because object creation is inherently expensive. It doesn't matter for
windows program. I suspuct web program was an after thought. This is wrong in my limited view of what is happening.


Are you *sure* that using the above pattern is actually ignoring
connection pooling? Don't assume that there's a direct correlation
between MySQLConnection instances and actual connections to the
database.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Alvin Bruney [MVP] wrote:
Probably I was misunderstood. I'll restate for clarity.

Implementing the using construct disposes of the object in question. The
connection pool is specifically setup to prevent object destruction because
it is inherently expensive to create this object. It is much more efficient
to reuse the object instead of destroying it.

This behavior poses no problem in a windows environment because connections
are cheap, however, the reverse is true in a web environment.

Should the dispose pattern be smart enough to detect pooling? After thinking
about it, Jon is right. Because of how the finalizer works, it is faulty to
place dependency in the overridden dispose function. So it cannot check for
a connection pool flag before destroying the object, at least based on
microsoft's best practices. So the *fix cannot come from the Dispose layer.

Should the fix be implemented at a higher level such as the connection
object layer? After thinking about it, I don't think so for the same reason;
it forces the connection object into knowing more than it is supposed to
about the environment in which it is run. But this doesn't solve the
problem. The problem still remains that the benefits of the pool are being
negated. Why is this important? Consider a high volume website fielding
20000 hits per day. The benefits of pooling here make or break the website.
Negating these benefits is simply not acceptable and must be avoided. But
how can I avoid it if I don't know the negative effects?

What I am suggesting is that Microsoft append a qualifying comment to the
dispose pattern literature indicating that the using construct which
implements the IDispose pattern not be used in combination with object
pooling for web applications because the benefits of pooling may be
negatively affected. With that recommendation in the literature, I can
safely avoid the using construct at a high level and choose to manually
close my objects guaranteeing that the connection pool benefits remain in
effect since calling close() only flags the object for reuse anyway.

Are you *sure* that using the above pattern is actually ignoring


connection pooling?

No, I am not 100%sure. But based on the literature, I make an educated guess
that it ignores pooling because the dispose implementation must make no
assumptions about the environment.


The Dispose() implementation is provided by the class - so it knows the
environment intimately.

For SqlConnection, IDisposable.Dispose() simply calls
SqlConnection.Close() if the connection is not already closed. So it
handles pooling just fine (at least as well as Close() does).
--
mikeb
Nov 15 '05 #7

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:Of****************@TK2MSFTNGP09.phx.gbl...
so what exactly is the point of this in a web page (windows forms doesn't
care about pooling for the most part because it is not inherently
expensive).
using(MySQLConnection cn = new MySQLConnection (Global._connString3))

{

}

I am not in agreement with this implementation at all. Because it is
inherently costly at the expense of being safe. Is it an either safety or
performance cake? That totally negates the benefit of connection pooling. It should come with a disclaimer as well because I always assumed it worked
with some sort of intelligence.


SqlConnection.Dispose calls SqlConnection.Close which puts the physical
connection back to the connection pool. The physical connection is *not*
closed or destroyed at this point. AFAIK the same applies to MySqlConnection
class with the ByteFX provider.

Likewise it is relatively cheap to call 'new SqlConnection' because only the
first call (per connection string) actually creates a physical connection.
Allocating SqlConnections with the 'using' pattern is the best way as it
plays well together with the connection pooling.

Sami
Nov 15 '05 #8
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
Probably I was misunderstood. I'll restate for clarity.
I think you misunderstood our response :)
Implementing the using construct disposes of the object in question. The
connection pool is specifically setup to prevent object destruction because
it is inherently expensive to create this object. It is much more efficient
to reuse the object instead of destroying it.
Yes. However, that object isn't necessarily visible at all.
This behavior poses no problem in a windows environment because connections
are cheap, however, the reverse is true in a web environment.
Well, some connections are cheap. Database connections aren't, in
general.
Should the dispose pattern be smart enough to detect pooling? After thinking
about it, Jon is right. Because of how the finalizer works, it is faulty to
place dependency in the overridden dispose function. So it cannot check for
a connection pool flag before destroying the object, at least based on
microsoft's best practices. So the *fix cannot come from the Dispose layer.
I don't believe there's any fix required...
Should the fix be implemented at a higher level such as the connection
object layer? After thinking about it, I don't think so for the same reason;
it forces the connection object into knowing more than it is supposed to
about the environment in which it is run. But this doesn't solve the
problem. The problem still remains that the benefits of the pool are being
negated. Why is this important? Consider a high volume website fielding
20000 hits per day. The benefits of pooling here make or break the website.
Negating these benefits is simply not acceptable and must be avoided. But
how can I avoid it if I don't know the negative effects?
The connection layer hides it from you entirely - and it's entirely the
*right* place to put the knowledge, as only the connection layer knows
about what is really cheap and what is really expensive.
What I am suggesting is that Microsoft append a qualifying comment to the
dispose pattern literature indicating that the using construct which
implements the IDispose pattern not be used in combination with object
pooling for web applications because the benefits of pooling may be
negatively affected. With that recommendation in the literature, I can
safely avoid the using construct at a high level and choose to manually
close my objects guaranteeing that the connection pool benefits remain in
effect since calling close() only flags the object for reuse anyway.


No, you shouldn't avoid using the "using" construct - because chances
are that's exactly what *returns* the connection to the pool.
Are you *sure* that using the above pattern is actually ignoring

connection pooling?

No, I am not 100%sure. But based on the literature, I make an educated guess
that it ignores pooling because the dispose implementation must make no
assumptions about the environment.


Your crucial assumption is that the object you create is the one which
is expensive. That's unlikely to be true. What *is* likely to be true
is that creating the object (or more likely a call to Open) acquires an
expensive resource in some way. Now, calling Dispose (or Close) is
likely to return that expensive resource to the pool. It doesn't mean
the resource is destroyed so that it has to be recreated. Effectively,
the MySQLConnection object in your example is likely to be a wrapper to
some extent - when it's opened, it acquires an open connection from the
pool of *real* connections, and when it's closed, that open connection
is returned to the pool - which no doubt has its own policies for when
the real connections are closed (timeouts etc).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
> For SqlConnection, IDisposable.Dispose() simply calls
SqlConnection.Close() if the connection is not already closed. So it
handles pooling just fine (at least as well as Close() does).
well that's really what i was looking for. if that's true, the problem is
resolved then. and i assume mysqlconnection would follow that same pattern.
i was thrown off by this response which seemed to indicate to me at least
that this wasn't happening
Using should blindly call Dispose. It would be up to the object implementer
to write a Dispose method that works properly with the pooling system
phew. ok all is well now. you guys can go back to work.

thanks

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"mikeb" <ma************@mailnull.com> wrote in message
news:ug****************@TK2MSFTNGP11.phx.gbl... Alvin Bruney [MVP] wrote:
Probably I was misunderstood. I'll restate for clarity.

Implementing the using construct disposes of the object in question. The
connection pool is specifically setup to prevent object destruction because it is inherently expensive to create this object. It is much more efficient to reuse the object instead of destroying it.

This behavior poses no problem in a windows environment because connections are cheap, however, the reverse is true in a web environment.

Should the dispose pattern be smart enough to detect pooling? After thinking about it, Jon is right. Because of how the finalizer works, it is faulty to place dependency in the overridden dispose function. So it cannot check for a connection pool flag before destroying the object, at least based on
microsoft's best practices. So the *fix cannot come from the Dispose layer.
Should the fix be implemented at a higher level such as the connection
object layer? After thinking about it, I don't think so for the same reason; it forces the connection object into knowing more than it is supposed to
about the environment in which it is run. But this doesn't solve the
problem. The problem still remains that the benefits of the pool are being negated. Why is this important? Consider a high volume website fielding
20000 hits per day. The benefits of pooling here make or break the website. Negating these benefits is simply not acceptable and must be avoided. But how can I avoid it if I don't know the negative effects?

What I am suggesting is that Microsoft append a qualifying comment to the dispose pattern literature indicating that the using construct which
implements the IDispose pattern not be used in combination with object
pooling for web applications because the benefits of pooling may be
negatively affected. With that recommendation in the literature, I can
safely avoid the using construct at a high level and choose to manually
close my objects guaranteeing that the connection pool benefits remain in effect since calling close() only flags the object for reuse anyway.

Are you *sure* that using the above pattern is actually ignoring


connection pooling?

No, I am not 100%sure. But based on the literature, I make an educated guess that it ignores pooling because the dispose implementation must make no
assumptions about the environment.


The Dispose() implementation is provided by the class - so it knows the
environment intimately.

For SqlConnection, IDisposable.Dispose() simply calls
SqlConnection.Close() if the connection is not already closed. So it
handles pooling just fine (at least as well as Close() does).
--
mikeb

Nov 15 '05 #10

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

Similar topics

2
by: Kevin Phifer | last post by:
If you are not creating a finializer in your class, but you are implementing IDisposable, is it still a good idea to call GC.SupressFinialize inside the dispose method? Does this somehow make...
3
by: JP | last post by:
Ok, Im at my wits end with trying to dispose of this class. Everything I try either doesnt dispose the class or I get build error. Im using C#. What Im I doing wrong? XHTML_Library...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
2
by: Keith Smith | last post by:
I was reading this link which mentions destructors... http://www.csharp-station.com/Tutorials/Lesson07.aspx Could someone give a simple example of practical useful destructor code? Would you...
2
by: Silent Ocean | last post by:
Hi All I have following questions regarding C# Assembly and Threading. Let me know the precise answer or lead me to the proper materials. 1. Is memory leakeage possible in .Net Manager...
10
by: Steph. | last post by:
I have an Object " MyEmploye " : Class MyEmploye { private string Name;
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
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...
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...
1
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 project—planning, 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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.