PC Review


Reply
Thread Tools Rate Thread

Calling DataAdapter.Update() on disposed DataSet?

 
 
Carlo Razzeto
Guest
Posts: n/a
 
      25th Jan 2008
Hey,

Some one gave a new developer of ours a project to make sure our core
library funcitons are properly cleaning up resources (that's a good thing!),
but I had consernse about one thing the developer was doing which sparked a
debate with my co-worker.

Some of these functions were returning DataSet objects, and the developer
was calling the Dispose() function on the datasets inside the finally
portion of a try block. For me, this kind of raised a red flag, because it
seems to me the funciton is then returning an object which it has already
"cleaned up". In some cases an "end programmer" might even want to take this
dataset, modify datarow values and call a DataAdapter.Update() on it. It
just doesn't seem like a very safe practise to me.

Am I being overly cautious here? I guess I just have a problem with
returning "cleaned up" object in general, to me it seems like no good can
come from that. Thanks for your thoughts,

Carlo

 
Reply With Quote
 
 
 
 
Miha Markic
Guest
Posts: n/a
 
      25th Jan 2008
Dispose should be called only when object isn't needed anymore.
In the case of DataSet, Dispose probably doesn't do anything and thus it
still works. However, this is a dangerous practice and should be avoided.
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Carlo Razzeto" <(E-Mail Removed)> wrote in message
news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
> Hey,
>
> Some one gave a new developer of ours a project to make sure our core
> library funcitons are properly cleaning up resources (that's a good
> thing!), but I had consernse about one thing the developer was doing which
> sparked a debate with my co-worker.
>
> Some of these functions were returning DataSet objects, and the developer
> was calling the Dispose() function on the datasets inside the finally
> portion of a try block. For me, this kind of raised a red flag, because it
> seems to me the funciton is then returning an object which it has already
> "cleaned up". In some cases an "end programmer" might even want to take
> this dataset, modify datarow values and call a DataAdapter.Update() on it.
> It just doesn't seem like a very safe practise to me.
>
> Am I being overly cautious here? I guess I just have a problem with
> returning "cleaned up" object in general, to me it seems like no good can
> come from that. Thanks for your thoughts,
>
> Carlo


 
Reply With Quote
 
Carlo Razzeto
Guest
Posts: n/a
 
      25th Jan 2008
These are my feelings exactly, thank you so much for confirming. Even if it
works in this case I'm afraid someone will see this working and decide it
will make a great standard practice.


"Miha Markic" <miha at rthand com> wrote in message
news:(E-Mail Removed)...
> Dispose should be called only when object isn't needed anymore.
> In the case of DataSet, Dispose probably doesn't do anything and thus it
> still works. However, this is a dangerous practice and should be avoided.
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & development www.rthand.com
> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>
> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
> news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
>> Hey,
>>
>> Some one gave a new developer of ours a project to make sure our core
>> library funcitons are properly cleaning up resources (that's a good
>> thing!), but I had consernse about one thing the developer was doing
>> which sparked a debate with my co-worker.
>>
>> Some of these functions were returning DataSet objects, and the developer
>> was calling the Dispose() function on the datasets inside the finally
>> portion of a try block. For me, this kind of raised a red flag, because
>> it seems to me the funciton is then returning an object which it has
>> already "cleaned up". In some cases an "end programmer" might even want
>> to take this dataset, modify datarow values and call a
>> DataAdapter.Update() on it. It just doesn't seem like a very safe
>> practise to me.
>>
>> Am I being overly cautious here? I guess I just have a problem with
>> returning "cleaned up" object in general, to me it seems like no good can
>> come from that. Thanks for your thoughts,
>>
>> Carlo

>


 
Reply With Quote
 
Sylvain Lafontaine
Guest
Posts: n/a
 
      25th Jan 2008
Excerpt for some exception, this is the right thing to do. The finally
portion of a try block has been called; this mean that an error occurred
earlier and that the object is now in an unknown, unstable or unsafe state
and should not be used anymore. Every object who has the IDispose interface
should be disposed of in the finally portion of a try block and the Dataset
object is no exception to this rule.

Of course, there are always some exceptions to any rule; for example when
you know that some portions of the object are still valid and that you want
to continue to operate on these portions but these are exceptions and you
should take some serious precautions when dealing with these.

In your case, the end programmer who might want to call the
DataAdapter.Update() is making an error because he's trying to operate on an
object for which there has been an error and therefore is in an unknown and
unstable state. Even if the Dispose() method would have not been called on
this object; trying to update the database through this object could lead to
some serious damages to the database; because essentially you are shooting
in the dark.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


"Carlo Razzeto" <(E-Mail Removed)> wrote in message
news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
> Hey,
>
> Some one gave a new developer of ours a project to make sure our core
> library funcitons are properly cleaning up resources (that's a good
> thing!), but I had consernse about one thing the developer was doing which
> sparked a debate with my co-worker.
>
> Some of these functions were returning DataSet objects, and the developer
> was calling the Dispose() function on the datasets inside the finally
> portion of a try block. For me, this kind of raised a red flag, because it
> seems to me the funciton is then returning an object which it has already
> "cleaned up". In some cases an "end programmer" might even want to take
> this dataset, modify datarow values and call a DataAdapter.Update() on it.
> It just doesn't seem like a very safe practise to me.
>
> Am I being overly cautious here? I guess I just have a problem with
> returning "cleaned up" object in general, to me it seems like no good can
> come from that. Thanks for your thoughts,
>
> Carlo



 
Reply With Quote
 
Sylvain Lafontaine
Guest
Posts: n/a
 
      25th Jan 2008
Sorry, forget about my last comment as I've misread the original post.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
wrote in message news:(E-Mail Removed)...
> Excerpt for some exception, this is the right thing to do. The finally
> portion of a try block has been called; this mean that an error occurred
> earlier and that the object is now in an unknown, unstable or unsafe state
> and should not be used anymore. Every object who has the IDispose
> interface should be disposed of in the finally portion of a try block and
> the Dataset object is no exception to this rule.
>
> Of course, there are always some exceptions to any rule; for example when
> you know that some portions of the object are still valid and that you
> want to continue to operate on these portions but these are exceptions and
> you should take some serious precautions when dealing with these.
>
> In your case, the end programmer who might want to call the
> DataAdapter.Update() is making an error because he's trying to operate on
> an object for which there has been an error and therefore is in an unknown
> and unstable state. Even if the Dispose() method would have not been
> called on this object; trying to update the database through this object
> could lead to some serious damages to the database; because essentially
> you are shooting in the dark.
>
> --
> Sylvain Lafontaine, ing.
> MVP - Technologies Virtual-PC
> E-mail: sylvain aei ca (fill the blanks, no spam please)
>
>
> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
> news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
>> Hey,
>>
>> Some one gave a new developer of ours a project to make sure our core
>> library funcitons are properly cleaning up resources (that's a good
>> thing!), but I had consernse about one thing the developer was doing
>> which sparked a debate with my co-worker.
>>
>> Some of these functions were returning DataSet objects, and the developer
>> was calling the Dispose() function on the datasets inside the finally
>> portion of a try block. For me, this kind of raised a red flag, because
>> it seems to me the funciton is then returning an object which it has
>> already "cleaned up". In some cases an "end programmer" might even want
>> to take this dataset, modify datarow values and call a
>> DataAdapter.Update() on it. It just doesn't seem like a very safe
>> practise to me.
>>
>> Am I being overly cautious here? I guess I just have a problem with
>> returning "cleaned up" object in general, to me it seems like no good can
>> come from that. Thanks for your thoughts,
>>
>> Carlo

>
>



 
Reply With Quote
 
Carlo Razzeto
Guest
Posts: n/a
 
      26th Jan 2008
Ok, lol... After reading what you wrote I was going to have some questions,
just wondering if there was something I haddn't considered. This post tells
me that this isn't the case.

Carlo

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
wrote in message news:(E-Mail Removed)...
> Sorry, forget about my last comment as I've misread the original post.
>
> --
> Sylvain Lafontaine, ing.
> MVP - Technologies Virtual-PC
> E-mail: sylvain aei ca (fill the blanks, no spam please)
>
>
> "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
> wrote in message news:(E-Mail Removed)...
>> Excerpt for some exception, this is the right thing to do. The finally
>> portion of a try block has been called; this mean that an error occurred
>> earlier and that the object is now in an unknown, unstable or unsafe
>> state and should not be used anymore. Every object who has the IDispose
>> interface should be disposed of in the finally portion of a try block and
>> the Dataset object is no exception to this rule.
>>
>> Of course, there are always some exceptions to any rule; for example when
>> you know that some portions of the object are still valid and that you
>> want to continue to operate on these portions but these are exceptions
>> and you should take some serious precautions when dealing with these.
>>
>> In your case, the end programmer who might want to call the
>> DataAdapter.Update() is making an error because he's trying to operate on
>> an object for which there has been an error and therefore is in an
>> unknown and unstable state. Even if the Dispose() method would have not
>> been called on this object; trying to update the database through this
>> object could lead to some serious damages to the database; because
>> essentially you are shooting in the dark.
>>
>> --
>> Sylvain Lafontaine, ing.
>> MVP - Technologies Virtual-PC
>> E-mail: sylvain aei ca (fill the blanks, no spam please)
>>
>>
>> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
>> news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
>>> Hey,
>>>
>>> Some one gave a new developer of ours a project to make sure our core
>>> library funcitons are properly cleaning up resources (that's a good
>>> thing!), but I had consernse about one thing the developer was doing
>>> which sparked a debate with my co-worker.
>>>
>>> Some of these functions were returning DataSet objects, and the
>>> developer was calling the Dispose() function on the datasets inside the
>>> finally portion of a try block. For me, this kind of raised a red flag,
>>> because it seems to me the funciton is then returning an object which it
>>> has already "cleaned up". In some cases an "end programmer" might even
>>> want to take this dataset, modify datarow values and call a
>>> DataAdapter.Update() on it. It just doesn't seem like a very safe
>>> practise to me.
>>>
>>> Am I being overly cautious here? I guess I just have a problem with
>>> returning "cleaned up" object in general, to me it seems like no good
>>> can come from that. Thanks for your thoughts,
>>>
>>> Carlo

>>
>>

>
>


 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      26th Jan 2008
Carlo,

AFAIK does the dispose of a DataSet nothing as long as there is a reference
to the DataSet or that the DataSet has a reference (which mostly is because
it has some tables).

I have tried this some years ago and I thought that it was the behaviour.

But there seems to be some maniaks who think that the dispose in .Net is the
same as deconstructing.

Cor

"Carlo Razzeto" <(E-Mail Removed)> schreef in bericht
news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
> Hey,
>
> Some one gave a new developer of ours a project to make sure our core
> library funcitons are properly cleaning up resources (that's a good
> thing!), but I had consernse about one thing the developer was doing which
> sparked a debate with my co-worker.
>
> Some of these functions were returning DataSet objects, and the developer
> was calling the Dispose() function on the datasets inside the finally
> portion of a try block. For me, this kind of raised a red flag, because it
> seems to me the funciton is then returning an object which it has already
> "cleaned up". In some cases an "end programmer" might even want to take
> this dataset, modify datarow values and call a DataAdapter.Update() on it.
> It just doesn't seem like a very safe practise to me.
>
> Am I being overly cautious here? I guess I just have a problem with
> returning "cleaned up" object in general, to me it seems like no good can
> come from that. Thanks for your thoughts,
>
> Carlo


 
Reply With Quote
 
Sylvain Lafontaine
Guest
Posts: n/a
 
      26th Jan 2008
No, it's just surprising how fast you can forget some basic things about a
language when it has been some time since the last time you have used it
(and especially but not necessarily when you are doing something else like
waiting for something to finish).

Maybe I should stop trying to make two things at the same time but on the
other hand, a lot of potentially useful posts would be lost; so I suppose
that's probably better to make some basic error from time to time in favor
of the greater good.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


"Carlo Razzeto" <(E-Mail Removed)> wrote in message
news:0F759BC2-67BD-49FA-9DEB-(E-Mail Removed)...
> Ok, lol... After reading what you wrote I was going to have some
> questions, just wondering if there was something I haddn't considered.
> This post tells me that this isn't the case.
>
> Carlo
>
> "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
> wrote in message news:(E-Mail Removed)...
>> Sorry, forget about my last comment as I've misread the original post.
>>
>> --
>> Sylvain Lafontaine, ing.
>> MVP - Technologies Virtual-PC
>> E-mail: sylvain aei ca (fill the blanks, no spam please)
>>
>>
>> "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
>> wrote in message news:(E-Mail Removed)...
>>> Excerpt for some exception, this is the right thing to do. The finally
>>> portion of a try block has been called; this mean that an error occurred
>>> earlier and that the object is now in an unknown, unstable or unsafe
>>> state and should not be used anymore. Every object who has the IDispose
>>> interface should be disposed of in the finally portion of a try block
>>> and the Dataset object is no exception to this rule.
>>>
>>> Of course, there are always some exceptions to any rule; for example
>>> when you know that some portions of the object are still valid and that
>>> you want to continue to operate on these portions but these are
>>> exceptions and you should take some serious precautions when dealing
>>> with these.
>>>
>>> In your case, the end programmer who might want to call the
>>> DataAdapter.Update() is making an error because he's trying to operate
>>> on an object for which there has been an error and therefore is in an
>>> unknown and unstable state. Even if the Dispose() method would have not
>>> been called on this object; trying to update the database through this
>>> object could lead to some serious damages to the database; because
>>> essentially you are shooting in the dark.
>>>
>>> --
>>> Sylvain Lafontaine, ing.
>>> MVP - Technologies Virtual-PC
>>> E-mail: sylvain aei ca (fill the blanks, no spam please)
>>>
>>>
>>> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
>>> news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
>>>> Hey,
>>>>
>>>> Some one gave a new developer of ours a project to make sure our core
>>>> library funcitons are properly cleaning up resources (that's a good
>>>> thing!), but I had consernse about one thing the developer was doing
>>>> which sparked a debate with my co-worker.
>>>>
>>>> Some of these functions were returning DataSet objects, and the
>>>> developer was calling the Dispose() function on the datasets inside the
>>>> finally portion of a try block. For me, this kind of raised a red flag,
>>>> because it seems to me the funciton is then returning an object which
>>>> it has already "cleaned up". In some cases an "end programmer" might
>>>> even want to take this dataset, modify datarow values and call a
>>>> DataAdapter.Update() on it. It just doesn't seem like a very safe
>>>> practise to me.
>>>>
>>>> Am I being overly cautious here? I guess I just have a problem with
>>>> returning "cleaned up" object in general, to me it seems like no good
>>>> can come from that. Thanks for your thoughts,
>>>>
>>>> Carlo
>>>
>>>

>>
>>

>



 
Reply With Quote
 
Miha Markic
Guest
Posts: n/a
 
      26th Jan 2008

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
wrote in message news:(E-Mail Removed)...
> Excerpt for some exception, this is the right thing to do. The finally
> portion of a try block has been called; this mean that an error occurred
> earlier and that the object is now in an unknown, unstable or unsafe state
> and should not be used anymore.


Ehm, not exactly. finally block is always run regardless whether error
occured or not - that's why it is called finally.
If the error occurs (aka Exception is thrown) then except block is executed
first (here you know that exception occured, but the object might be in safe
state - depends on the various factors) and finally block after it.

Every object who has the IDispose interface
> should be disposed of in the finally portion of a try block and the
> Dataset object is no exception to this rule.


You have to dispose something only if you don't need it anymore. If you want
to return an object to a caller you won't dispose it, will you?
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

 
Reply With Quote
 
Carlo Razzeto
Guest
Posts: n/a
 
      27th Jan 2008
Yes, but still I just didn't see it as being particularly safe. While it may
be safe in the dataset, I'm just afraid that down the line some developer
will see this happening in our lower level library, think it's a great short
cut to skirt having to dispose of your objects properly (i.e. at the right
time) and start copying the behaviour with impunity. Which is why I kind of
wanted to get all of that kind of code out of our lower level libraries even
if in the end it makes absolutly no difference.

Besides, knowing microsoft you're putting your self at risk that in some
future revision of the framework prematurly calling dispose on a dataset
will all of the sudden become unsafe, and we'd really be in a bind then.

Carlo

"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
newsD2C9259-55FD-4378-908E-(E-Mail Removed)...
> Carlo,
>
> AFAIK does the dispose of a DataSet nothing as long as there is a
> reference to the DataSet or that the DataSet has a reference (which mostly
> is because it has some tables).
>
> I have tried this some years ago and I thought that it was the behaviour.
>
> But there seems to be some maniaks who think that the dispose in .Net is
> the same as deconstructing.
>
> Cor
>
> "Carlo Razzeto" <(E-Mail Removed)> schreef in bericht
> news:0A76CA20-B5AF-4330-9EE1-(E-Mail Removed)...
>> Hey,
>>
>> Some one gave a new developer of ours a project to make sure our core
>> library funcitons are properly cleaning up resources (that's a good
>> thing!), but I had consernse about one thing the developer was doing
>> which sparked a debate with my co-worker.
>>
>> Some of these functions were returning DataSet objects, and the developer
>> was calling the Dispose() function on the datasets inside the finally
>> portion of a try block. For me, this kind of raised a red flag, because
>> it seems to me the funciton is then returning an object which it has
>> already "cleaned up". In some cases an "end programmer" might even want
>> to take this dataset, modify datarow values and call a
>> DataAdapter.Update() on it. It just doesn't seem like a very safe
>> practise to me.
>>
>> Am I being overly cautious here? I guess I just have a problem with
>> returning "cleaned up" object in general, to me it seems like no good can
>> come from that. Thanks for your thoughts,
>>
>> Carlo

>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How post changes before calling DataAdapter.Update? Bill Microsoft ADO .NET 1 16th Jul 2005 10:02 PM
Dataadapter won't update Dataset in VB.net =?Utf-8?B?U2hhcmlx?= Microsoft Dot NET Framework Forms 0 23rd Jun 2005 04:22 PM
Effective use of DataAdapter.Update(DataSet) Ken Allen Microsoft ADO .NET 5 4th Jun 2004 12:24 PM
DataAdapter.Update(DataSet) dm_dal Microsoft ADO .NET 2 3rd Jun 2004 06:16 PM
calling a dataAdapter Update in DataTable.DataRowChanged Event pplppp Microsoft ADO .NET 1 20th Nov 2003 12:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.