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:

Bildschirmfoto 2022-04-17 um 13 09 49

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

  1. clone repo: git@github.com:tobiasmuecksch/capacitor-sqlite-angular.git
  2. test web: npm run start
  3. test electron: npm run electron:start

Click buttons in this order (while looking at the js console):

  1. “Create Tables”
  2. “Insert some data”
  3. “print select all to console”
  4. “drop database”

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 44 (21 by maintainers)

Most upvoted comments

Android

On android, I cannot set the journal_mode to wal2, because the execute fails with this error:

ERROR Error: Uncaught (in promise): Error: Execute: unknown error: Queries cannot be performed using execSQL(), use query() instead.

=== [EDIT] ===

I’ve suspected that the error message indicates that something is wrong with the .execute()-method. Therefore I changed the code to:

Bildschirmfoto 2022-04-19 um 16 53 20

And now the exception has changed.

Bildschirmfoto 2022-04-19 um 16 51 12

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 wal works even though the exception is thrown. Setting it to wal2 does not work. Maybe wal2 is not available on android?