sqlite: bug: Simultaneous write operations broken on `web` and `electron`
Describe the bug I’m working on a chat app. That means, that my app receives events asynchronously. For example such an event could be a new message.
It happens pretty often, that multiple messages are received at the same time, which means that the app tries to add multiple messages at the same time to the database. On Android that works perfectly fine. But on iOS web and electron it doesn’t work as expected at all.
On iOS and web only one of the multiple messages are saved. The other datasets are discarded without any error.
On electron the following exception is thrown:
To Reproduce The following code does not work as expected (on web and electron):
async addDataToTestDB() {
// Since I'm not using await here, these operations are called simultaneously.
this.insertData('id1', 'Bob');
this.insertData('id2', 'Lina');
this.insertData('id3', 'Maria');
}
async insertData(id: string, name: string) {
const dbConnection = await this.sqlite.openDB('test-db');
const result = await dbConnection.run('REPLACE INTO test(id, name) VALUES(?,?)', [id, name]);
// The following line is only necessary on web platform
await this.sqlite.saveToStore('test-db');
}
But if I actively take care that there are no simultaneous db queries, the bug does not appear.
async addDataToTestDB() {
await this.insertData('id1', 'Bob');
await this.insertData('id2', 'Lina');
await this.insertData('id3', 'Maria');
}
Expected behavior Simultaneous database write actions should work.
Desktop (please complete the following information):
- OS: macOS 12.3.1
- Browser: Safari
- Version: 15.4
Demo code
- clone repo: git@github.com:tobiasmuecksch/capacitor-sqlite-angular.git
- test
web:npm run start - test
electron:npm run electron:start
Click buttons in this order (while looking at the js console):
- “Create Tables”
- “Insert some data”
- “print select all to console”
- “drop database”
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 44 (21 by maintainers)
Sample code: https://github.com/jepiqueau/capacitor-sqlite-angular/blob/main/src/app/services/sqlite.service.ts#L103-L113
Android
On
android, I cannot set thejournal_modetowal2, because theexecutefails with this error:=== [EDIT] ===
I’ve suspected that the error message indicates that something is wrong with the
.execute()-method. Therefore I changed the code to:And now the exception has changed.
I don’t understand what this error means.
I did some research, and if I understand this post correctly, it could be that the plugin misinterprets the response of sqlite as an error, although no error has occurred.
In order to verify this suspicion I ignored the exception by wrapping the pragma code with a try-catch block. I found out, that setting
walworks even though the exception is thrown. Setting it towal2does not work. Maybewal2is not available on android?