Re: newbie: deactivate query before freeing?
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 28 Sep 2005 19:43:16 +0200
"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1127919351.575409.175790@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Do you have to deactivate a query before you free it? Does this
> guarantee that the database server will close the connection or
> something like that?
Query components usually operate on connection components. If you
want to close the connection, close the connection component.
Any reasonable component should clean up after itself on being freed.
If you find one that doesn't, don't use it. There are plenty that do.
> What about the use of try..finally? Is this necessary?
Sometimes. Specifically, when an exception happens in the try block.
The point is that you never know when it will be necessary, so you
guard against it all the time.
> try
> myQuery.Create(nil);
> myQuery.sql.text := 'SELECT * from Blah';
> myQuery.active := true;
> ...
> finally
> if assigned(myQuery) then
> begin
> myQuery.active := false;
> freeAndNil(MyQuery);
> end;
While this works, it's not the usual idiom.
Create
try
Work
finally
Free
end;
You are explicitly _not_ guarding against an exception in Create.
Instead, you are making sure that *if* the Create succeeded, there
*will* be a Free.
Also, you don't need to check that the object is Assigned to call
Free on it. Free checks that; that's what it's for.
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: newbie: deactivate query before freeing?
- From: swansnow
- Re: newbie: deactivate query before freeing?
- References:
- newbie: deactivate query before freeing?
- From: swansnow
- newbie: deactivate query before freeing?
- Prev by Date: Re: how to determine drive type
- Next by Date: Re: newbie: deactivate query before freeing?
- Previous by thread: Re: newbie: deactivate query before freeing?
- Next by thread: Re: newbie: deactivate query before freeing?
- Index(es):
Relevant Pages
|
|