flutter sqlite transaction usage error warning database has been locked for

不想你离开。 提交于 2020-12-15 05:19:49

问题


I am developing a mobile application that use sqlite and transaction.

Below code is that getting order information from user using by using sqlite transaction saving model to multiple db tables.

Future<AppResult<int>> save(Order order) async {
    Database db;
    var dbReturn = BaseService();
    try {
      db = await databaseOpen();
      int dbSaveResult = 0;
      await db.transaction((txn) async {
        var batch = txn.batch();
        final List<Map<String, dynamic>> exist = await db.query(tableName, where: "id =?", whereArgs: [order.id]);
        if (exist.length == 0) {
          await _insert(order, txn, batch);
        } else {
          await _update(order, txn, batch);
        }
        await batch.commit(continueOnError: true).then((value) {
          if (value.length > 0) dbSaveResult = value.length;
        });
      });
      return dbReturn.success<int>(dbSaveResult, "Kaydedildi", 1, 1);
    } on Exception catch (e) {
      return dbReturn.failed<int>(-1, "OrderDbService -> save", 0, 0, e);
    } finally {
      if (db != null && db.isOpen) {
        await db.close();
      }
    }
  }



 Future<void> _insert(Order order, Transaction txn, Batch batch) async {
    try {
      order = setEntityValue(order, false);
      order = _setOrder(order, order.lines);

      batch.insert(tableName, order.toMap(), conflictAlgorithm: ConflictAlgorithm.replace);

      await _saveLines(order.lines, txn, batch);
      await _saveDescriptions(order.descriptions, txn, batch);
      await _saveDetail(order.detail, txn, batch);
      await _saveDiscounts(order.discounts, txn, batch);
      await _saveExchangeRates(order.exchangeRates, txn, batch);
    } on Exception catch (e) {
      throw e;
    }
  }

after calling the method sqlite throw a message as below

D/Sqflite ( 8938): [57,main(2)] starting threadThread[Sqflite,5,main] priority 10
D/Sqflite ( 8938): [57,main(2)] opened 57 /data/user/0/com.tttt.mobileApp/databases/tttt.db
D/Sqflite ( 8938): [57,Sqflite(484)] opened 57 /data/user/0/com.tttt.mobileApp/databases/tttt.db
D/Sqflite ( 8938): [57,Sqflite(484)] BEGIN IMMEDIATE
I/flutter ( 8938): Warning database has been locked for 0:00:10.000000. Make sure you always use the transaction object for database operations during a transaction

回答1:


SOLVED: be sure you don't use db instance inside the transaction as below

await db.query(tableName, where: "id =?", whereArgs: [order.id]);

instead of use as below

await txn.query(tableName, where: "id =?", whereArgs: [order.id]);

flutter does not warn you about any transaction usage error.



来源:https://stackoverflow.com/questions/64834014/flutter-sqlite-transaction-usage-error-warning-database-has-been-locked-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!