Re: Question about IDispose
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/25/04
- Next message: Jon Skeet [C# MVP]: "Re: StackOverFlow Exception while trying to update the dataset back to the database."
- Previous message: kalantre: "Silly Windows Forms Question"
- In reply to: Alvin Bruney [MVP]: "Re: Question about IDispose"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: Question about IDispose"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 25 Feb 2004 08:43:32 -0000
<"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 - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Jon Skeet [C# MVP]: "Re: StackOverFlow Exception while trying to update the dataset back to the database."
- Previous message: kalantre: "Silly Windows Forms Question"
- In reply to: Alvin Bruney [MVP]: "Re: Question about IDispose"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: Question about IDispose"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|