Fix deadlock in -instanceWithPrimaryKey
Some codepaths through instanceWithPrimaryKey can result in a database request, which will
be serialized through g_databaseQueue. If a separate thread has already acquired the g_databaseQueue
lock and is waiting for g_instancesReadLock, then a mutual deadlock will occur. This fix releases
the g_instancesReadLock before any database calls are made.