Re: newbie: deactivate query before freeing?



"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


.



Relevant Pages

  • Re: Lost connection
    ... The network can be OK again at the moment I want to execute query. ... But ADO only raises an exception when it actually uses the connection, ...
    (borland.public.delphi.database.ado)
  • Re: But a fool with a tool is still a fool
    ... 'layered design' solve? ... Basically, the db connection components should not assume anything about the DB they're connecting to, and the connection string should be "filled" by another layer in your application that decides, usually at startup, which information to use. ...
    (borland.public.delphi.non-technical)
  • get your potentially resuming element on board my squad
    ... exclusive era. ... require a transmission! ... it eases a contest too positive in connection with her ...
    (sci.crypt)
  • Re: ASP - FROM statement slows down connection to database
    ... Open your database in Access, switch to the Queries tab, create a new query ... in Design View without choosing a table, swtich to SQL View, paste the sql ... connection string rather than using an ... recordset open statements. ...
    (microsoft.public.inetserver.asp.db)
  • Re: Update checking
    ... My thought was to use a simple query with a "username" param ... concurrency exceptions happen if the data you queried has ... > was and basically let the user work in a similar offline mode. ... Where does connection pooling come ...
    (microsoft.public.dotnet.framework.adonet)