Does DatabaseQuery Release Previous Query's Resources?

Just starting out? Need help? Post your questions and find answers here.
swhite
Addict
Addict
Posts: 805
Joined: Thu May 21, 2009 6:56 pm

Does DatabaseQuery Release Previous Query's Resources?

Post by swhite »

Hi

The documentation mentions that FinishDatabaseQuery() must be called to free the query resources. However, what happens in the following case with multiple queries?

Code: Select all

    DatabaseQuery(2,"select * from mytable1")
    
    ... process the data
    
    DatabaseQuery(2,"select * from mytable2")
    
    ... process the data
    
    DatabaseQuery(2,"select * from mytable3")
    
    ... process the data
    
   FinishDatabaseQuery(2)    
In the above pseudo code do the subsequent queries release the resources of the previous query? Does the FinishDatabaseQuery(2) clear all the resources from all queries or should a FinishDatabaseQuery(2) be called before the subsequent queries?

Thanks,
Simon
Simon White
dCipher Computing
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Does DatabaseQuery Release Previous Query's Resources?

Post by spikey »

I'm reasonably confident in saying the scenario you describe will leak and you should 'Finish' every query (but I haven't tested it).
SQLite Documentation wrote: The application must finalize every prepared statement in order to avoid resource leaks.
See https://www.sqlite.org/c3ref/finalize.html. PostgreSQL is even more emphatic in https://www.postgresql.org/docs/current/libpq-exec.html. I don't have references to hand for the other databases but I don't imagine they say something very different.

A query will result in a memory allocation of an indeterminate (to PB) size to handle the results; the size being dependent on the backend database and the particulars of the query. PB, therefore, can't garbage collect it successfully on its own and must rely on the database library to do so.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Does DatabaseQuery Release Previous Query's Resources?

Post by infratec »

You have to use FinishDatabaseQuery() for every query and before you start an other one.
This has nothing to do with the used database.

If you mean you have to do more queries after an other ,you have 2 possibilies:
1. Use an other DB handle
2. Pack your selects into one. (Hint: With ...)
swhite
Addict
Addict
Posts: 805
Joined: Thu May 21, 2009 6:56 pm

Re: Does DatabaseQuery Release Previous Query's Resources?

Post by swhite »

Hi

I was assuming that I would need to use the FinishDatabaseQuery() but was not absolutely sure. So thank-you for the clarification.

Simon
Simon White
dCipher Computing
Post Reply