yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

There are no more rows in the active result set

Open craiglondon opened this issue 4 years ago • 6 comments

What steps will reproduce the problem?

Using MSSQL driver (sqlsrv) Attempt to use $query->batch() or $query->each()

What is the expected result?

No errors

What do you get instead?

PDOException: SQLSTATE[IMSSP]: There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved. in /app/vendor/yiisoft/yii2/db/DataReader.php:112 Stack trace: #0 /app/vendor/yiisoft/yii2/db/DataReader.php(112): PDOStatement->fetch() #1 /app/vendor/yiisoft/yii2/db/BatchQueryResult.php(163): yii\db\DataReader->read() #2 /app/vendor/yiisoft/yii2/db/BatchQueryResult.php(147): yii\db\BatchQueryResult->getRows() #3 /app/vendor/yiisoft/yii2/db/BatchQueryResult.php(117): yii\db\BatchQueryResult->fetchData() #4 /app/frontend/controllers/MerchantInvoiceController.php(107): yii\db\BatchQueryResult->next() #5 [internal function]: frontend\controllers\MerchantInvoiceController->actionRun() #6 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array) #7 /app/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array) #8 /app/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('run', Array) #9 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('merchant-invoic...', Array) #10 /app/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request)) #11 /app/frontend/web/index.php(23): yii\base\Application->run() #12 {main}

Work around

Provide instance of \yii\db\Connection() as $db foreach ($query->batch(100, $db) as $orgs) {

Additional info

There seems to be an issue in BatchQueryResult->getDbDriverName $this->db should be set, but is not

Seems to be related to https://github.com/yiisoft/yii2/issues/17539

Q A
Yii version 2.0.38
PHP version 7.3.26
Operating system Ubuntu 16.04.7 LTS

craiglondon avatar Feb 19 '21 15:02 craiglondon

Any idea about how to fix it?

samdark avatar Feb 19 '21 17:02 samdark

@samdark Not off the top of my head... the comments in the batch() method say that * @param Connection $db the database connection. If not set, the "db" application component will be used. but in the code...

public function batch($batchSize = 100, $db = null)
    {
        return Yii::createObject([
            'class' => BatchQueryResult::className(),
            'query' => $this,
            'batchSize' => $batchSize,
            'db' => $db,
            'each' => false,
        ]);
    }

I don't see $db being set to something other than null if it is null, maybe something buried in the createObject() is supposed to automagically know to set $db.

craiglondon avatar Feb 19 '21 19:02 craiglondon

in db/Query, createCommand, there is this code to set the $db variable if it is null, seems like batch() needs it too...

    public function createCommand($db = null)
    {
        if ($db === null) {
            $db = Yii::$app->getDb();
        }

craiglondon avatar Feb 19 '21 19:02 craiglondon

Yep, seems like so.

samdark avatar Feb 19 '21 19:02 samdark

In 2.0.45 still the same:

PDOException: SQLSTATE[IMSSP]: There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved. in /var/www/vendor2045/yiisoft/yii2/db/DataReader.php:112 Stack trace: #0 /var/www/vendor2045/yiisoft/yii2/db/DataReader.php(112): PDOStatement->fetch() #1 /var/www/vendor2045/yiisoft/yii2/db/BatchQueryResult.php(179): yii\db\DataReader->read() #2 /var/www/vendor2045/yiisoft/yii2/db/BatchQueryResult.php(162): yii\db\BatchQueryResult->getRows() #3 /var/www/vendor2045/yiisoft/yii2/db/BatchQueryResult.php(132): yii\db\BatchQueryResult->fetchData()

pabloneruda1 avatar May 05 '22 08:05 pabloneruda1

workaround

       foreach ($queryQueue->batch(self::BATCH_LIMIT, Yii::$app->db) as $queue) {

lutek avatar Jul 22 '22 12:07 lutek