On Thu, Apr 6, 2017 at 8:26 AM, Simon Slavin <slav...@bigfraud.org> wrote:
> > On 6 Apr 2017, at 2:38pm, Bob Friesenhahn <bfrie...@simple.dallas.tx.us> > wrote: > > > Is calling sqlite3_close() prior to exiting a requirement in general for > sqlite? I was not aware of this requirement. > > Do you explicitly call sqlite3_shutdown() ? If not, when are you > expecting SQLite to close the file for you ? > > Do you also routinely omit close(), fclose(), and sync() ? Don’t you have > problems with unflushed buffers ? > > > It is not uncommon for programs to quit without explicitly releasing all > resources (e.g. calling sqlite3_close()) when a problem occurs. > > It depends on whether the software understands the problem, and on whether > that problem relates to SQLite. If your program crashed because of a > divide-by-zero error you probably still want it to close any databases it > was using. If it crashed because it ran out of disk space, you probably > don’t. > > SQLite does not just release resources when you close a file. Depending > on your settings it may make changes to journal files to so that when it > reopens the file it knows it closed it properly instead of crashing. This > means that when it reopens the file it knows whether it needs to check for > journal entries and perhaps recover from corruption caused by a crash or > power-failure. It may also recover unused space in its files, to release > some extra disk space. > > Deleting a journal file which SQLite thinks its using can cause database > corruption (see section 1.3 of <https://www.sqlite.org/howtoc > orrupt.html#_deleting_a_hot_journal>). But deleting the journal file of > a database which SQLite knows is correctly closed cannot cause corruption. > > It is uncommon for SQLite programs not to close files correctly. The only > time I see this done is code demonstrating a feature, where the file is > never going to be used again. What’s not common is for programmers to > issue sqlite3_initialize() or sqlite_shutdown(). Hardly anyone knows about > these calls, because if you close your database files properly SQLite does > everything needed without them. > > If you’re going to miss something out, miss out sqlite_shutdown(). Don’t > miss out closing your files. I played with the auto closing option for a little while and started to implement it on more databases; but the others are just infrequently accessed, and the re-open for a couple queries is more work to open the file than to just do the queries; I'd rather not have the initial open delay, so I implemented just doing a 'pragma checkpoint_wal' a short time after a statement that is not sqlite3_stmt_readonly() is done. (and after any pending commits if a transaction is in progress). This prevents having frames recovered; so in theory the .db itself is typically itself more up-to-date than previously. I scrapped the auto-close; but yes, then the -shm and -wal files remain. still, though, after having issued pragma wal_checkpoint's if the process is killed, frames are still 'recovered'. it's staying at 2 frames(and 1 frame in a second database) though because I now catch the error and issue a checkpoint if it's found on startup. the one database has 1 connection to it; but it still doesn't commit the checkpoint; (was thinking maybe because the 2 frame database has 2 connections to it that it wasn't actually checkpointing; but doesn't seem to matter) for a while it seemed that checkpoint after a commit was actually keeping recovered frames at 0; but there must have been an error in my test to start with(?) > > I do not see any admonishments about a requirement to call > sqlite3_close() on the documentation page at > https://sqlite.org/c3ref/close.html. > > One of those things you expect programmers to know. If you start > something finish it. > > Simon. > _______________________________________________ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users > _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users