Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclusive locking_mode in two tabs #54

Closed
steida opened this issue Jun 12, 2022 · 2 comments
Closed

exclusive locking_mode in two tabs #54

steida opened this issue Jun 12, 2022 · 2 comments

Comments

@steida
Copy link

steida commented Jun 12, 2022

I just tested my app with two tabs open, and on the second tab, DB access is always somehow locked. When I don't use exclusive locking, my app works as expected. Is it possible I am doing something wrong?

const asyncModule = await SQLiteAsyncESMFactory();
const sqlite3 = SQLite.Factory(asyncModule);
sqlite3.vfs_register(
  new IDBBatchAtomicVFS("evolu", { durability: "relaxed" })
);
const connection = await sqlite3.open_v2("app", undefined, "evolu");
// await sqlite3.exec(connection, "PRAGMA locking_mode=exclusive");
@rhashimoto
Copy link
Owner

rhashimoto commented Jun 13, 2022

I think the behavior you describe is working as expected. That's how exclusive locking works; it takes a lock and holds on to it:

When the locking-mode is set to EXCLUSIVE, the database connection never releases file-locks. The first time the database is read in EXCLUSIVE mode, a shared lock is obtained and held. The first time the database is written, an exclusive lock is obtained and held.

Note that while this says that when the database is read a shared lock is held, the stock IDBBatchAtomicVFS doesn't use shared locking, i.e. only one connection can hold the lock at a time even if only reading.

If you want to release the exclusive mode lock so another context can access the database, you have to either close the connection or change the mode and access the database:

Database locks obtained by a connection in EXCLUSIVE mode may be released either by closing the database connection, or by setting the locking-mode back to NORMAL using this pragma and then accessing the database file (for read or write). Simply setting the locking-mode to NORMAL is not enough - locks are not released until the next time the database file is accessed.

@steida
Copy link
Author

steida commented Jun 14, 2022

Thank you for your answer. If I understand it correctly, it means I have to implement some mechanism over wa-sqlite, if I want to use it with more than one tab. I have almost no idea, how that mechanism should work. Should I observe tab focus/blur? A simple example would be great.

Meanwhile, if I will not use EXCLUSIVE mode, do I have to use WebLock? Probably yes. Then I don't understand the point of the whole async API, because, with custom WebLock, the sync version should be OK. Am I correct? Thank you for your work.

Repository owner locked and limited conversation to collaborators Jun 14, 2022
@rhashimoto rhashimoto converted this issue into discussion #55 Jun 14, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants