On 2 Jul 2014, at 12:02pm, Grzegorz Sikorski <g.sikor...@kelvatek.com> wrote:

> My code works fine up to about 1020 connections to the database. After around 
> this number, I get an error "unable to open database file". [snip]

This should not happen.  But I don't know why it would happen.  I have seen 
individual apps  make more than 5000 connections to SQLite databases (not 
concurrently, but closing one before opening another) without problems.  Mind 
you, that was on a desktop computer, not under iOS.

Please make sure you are checking the results returned by /all/ sqlite3_ calls, 
including _close(), and reporting all errors.  Don't check just the results 
returned by operations which make changes to the database.  But if everything 
returns SQLITE_OK then you shouldn't be having those problems.  Please post 
more info, perhaps with the extended error code.

>  I am not sure if there is any reason why keeping opened connection in whole 
> application lifetime is really something I should do? I would prefer to open 
> database only when it is needed, to avoid risk of file corruption on power 
> loss.

If your app uses one database during one 'run', and need frequent access to it, 
there is no need to keep closing and reopening your database.  As long as you 
use transactions properly and do not disable proper journal handing, you will 
find that SQLite is rock solid and you do not need to keep closing a database 
to make sure it is updated.

Under iOS you should, of course, close the database when your app quits.  And 
you may also want to close it for the following notifications:

applicationWillResignActive:
applicationDidEnterBackground:

and reopen it for

applicationDidBecomeActive: 
applicationWillEnterForeground:

It's possible that your app might want to do processing while in the background 
so you might not want to close in response to 'applicationDidEnterBackground:'.

Alternatively instead of opening the database on 'start' and 'wake' you can 
keep track of its 'open' status in a boolean variable and check it before each 
database operation.  This would allow an application to open a database 
connection only when needed, then keep it open until the next suspend, 
background, or quit.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to