1
votes

I'm building a prototype of an app where persistent store uses SQLite via AFP on a "server" machine (the same LAN). However, I can't connect to the store from 2 different instances of my app.

I set SQLite pragma setting (related to locking) on persistent store coordinator like this:

    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];

NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
[pragmaOptions setObject:@"NORMAL" forKey:@"locking_mode"];
NSDictionary *storeOptions = [NSDictionary dictionaryWithObject:pragmaOptions forKey:NSSQLitePragmasOption];

if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:storeOptions error:&error]) {
    [[NSApplication sharedApplication] presentError:error];
    return nil;
}

and error message I'm getting when trying to connect to the store from 2nd client (i.e. when the 1st one is already successfully connected) is:

ERROR: sqlite database is locked because it is in use by another host that holds a host-exclusive lock on .../TestDBApp.storedata; this host UID... cannot override the host-exclusive lock until the other host UID... releases its locks on .../.TestDBApp.storedata-conch

Am I doing something wrong?

Is accessing the same store from 2 clients possible with Core Data and SQLite?

Is this a bug in Core Data and/or SQLite API?

2
Did you ever find a way to access sqlite from 2 different processes? If there is an exclusive lock from one connection, I guess the 2nd connection won't wait until it's committed and you have to implement your won retry logic. - paiego

2 Answers

0
votes

SQLite does not allow a database file to have multiple write locks. Write locks are taken for any transaction that changes data, or when the applications explicitly requests one.

Apparently, a Core Data connection holds a write transaction or a write lock open.

0
votes

As far as I could remember, Core data will use the exclusive lock mode for save method. I suggest you to pass argument "-com.apple.CoreData.SQLDebug 1" to your app . If you find "BEGIN EXCLUSIVE" in console, I guess your setting is ignored by Core Data.