On Fri, Mar 18, 2005 at 03:00:47PM +1100, Lars Norved wrote:
> Lothar M?rkle <[EMAIL PROTECTED]> writes:
> >>> Assuming you have a cgi-like application with many processes 
> >>> that just looks up a row, displays and then exits. You can 
> >>> simple use the rename call to atomically replace the db file
> >>> (see man 2 rename for bordercases) with another and without 
> >>> locking.
> >>>
> >>> lothar
> >>
> >> but this will lose any update currently begin written by any 
> >> of the active cgi processes  - even if they are in the middle 
> >> of a transaction?
> >
> > yes, this will only work with a read-only db, e.g. all your data 
> > is pre-calculated once a day to a second db file, and then replace 
> > the primary read-only db with it.
> >
> > But with read-only, one should avoid transactions at all. There 
> > will be the risk of a -journal that doesn't correspond to the 
> > db-file. I think this will break if rolled back?
> >
> > lothar
> 
> I need to copy a 'live' master database file and overwrite a read 
> only file for pseudo replication.
> 
> How do I ensure the database is flushed and acquire an exclusive 
> lock on the master database?
> 
> Will something like the code below do it?
> 
> sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION", NULL, NULL, error);
> copyFiles();
> sqlite3_exec(db, "COMMIT", NULL, NULL, error);

i think you have to use sqlite3_close to be shure all
data is written. then copy. 

> Also are you sure it is completely safe to replace a read only 
> database while reads are currently being done?

I'm using this with cdb, which is a read-only constant-dbm like
thingy with put and get only(cr.yp.to/cdb.html)

on unix, this is save for read only one shot processes reading file a
and one updating process.
fd = open( a.tmp)
print( fd, "some new stuff")
close( fd )
rename( a.tmp, a)

no process reading file a will see a broken file and no locking is used, 
because rename is
atomic as long you stay in the same filesystem.

If you have only readers, i see no reason why an app using sqlite should
break. But the readers have to reopen the db.

lothar

Reply via email to