Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning database has been locked for 0:00:10.000000. Make sure you always use the transaction object for database operations during a transaction

I am trying to get images from a remote server. I keep getting this error:

  Warning database has been locked for 0:00:10.000000. 
  Make sure you always use the transaction object for database operations during a transaction

And afterwards my application crash.


Widget carousel = new Container(
      height: 200,
      child: FutureBuilder<List<String>>(
          future: getPropImgs(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return new Carousel(
                
                boxFit: BoxFit.cover,
                images: snapshot.data.map((f) {
                  return new CachedNetworkImage(
                    imageUrl: f.toString(),
                    imageBuilder: (context, imageProvider) => Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                            image: imageProvider,
                            fit: BoxFit.cover,
                            colorFilter:
                                ColorFilter.mode(Colors.blue, BlendMode.dstIn)),
                      ),
                    ),
                    placeholder: (context, url) => Center(
                        child: Container(
                            height: 30,
                            width: 30,
                            child: CircularProgressIndicator())),
                    errorWidget: (context, url, error) =>
                        Image.asset("assets/images/icon.png"),
                  );
                }).toList(),
                autoplay: true,
                animationCurve: Curves.fastOutSlowIn,
                animationDuration: Duration(milliseconds: 1000),
                indicatorBgPadding: 1.0,
                dotColor: Colors.black,
                
              );


like image 385
samuel ojekunle Avatar asked Oct 26 '20 14:10

samuel ojekunle


1 Answers

I guess you are trying to perform huge amount of SQL queries. Once slow solution is to make sure you await every calls. In the second line here you are missing an await:

 await db.transaction((txn) async {

However here it still create one transaction per insert so that will be super slow. One solution is to use batches (https://github.com/tekartik/sqflite/tree/master/sqflite#batch-support) and queue the insert statement (1000 by 1000 for example);

var batch = db.batch();
batch.rawInsert(statement1);
batch.rawInsert(statement2);
batch.rawInsert(statement3);
...
await db.commit(noResult: true);
like image 120
Ömer Karaca Avatar answered Sep 18 '22 04:09

Ömer Karaca