Table locking issues with Laravel 5.1

I cannot lock MySQL tables with this query:

DB::statement('LOCK TABLES imports WRITE'); 

He gives the following exception:

 [Illuminate\Database\QueryException] 

SQLSTATE [HY000]: General Error: 2014 Unable to execute queries while other unbuffered queries are active. Consider using PDOStatement :: fetchAll (). Also, if your code is only ever to run with mysql, you can enable query buffering by setting the PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: LOCK TABLES imports WRITE)

The same thing happens when using PDO.

How should I use this?

+5
source share
2 answers
 // prevent rows from being modified until you're finished DB::table('imports')->where('total', '>', 10)->sharedLock()->get(); 

Use lockForUpdate() instead of lockForUpdate() rows from changing or changing them using another general lock.

+2
source

If you want to lock the entire table and you want to use LOCK TABLES table_name WRITE , this should be called like DB::unprepared('LOCK TABLES imports WRITE'); but if you want to apply lock for selected rows use sharedLock() or lockForUpdate() is the right way to do this so that it returns and locks only selected rows.

+5
source

All Articles