yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

There are no more rows in the active result set #18521

Open craiglondon opened 3 years ago

craiglondon commented 3 years ago

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
samdark commented 3 years ago

Any idea about how to fix it?

craiglondon commented 3 years ago

@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 commented 3 years ago

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();
        }
samdark commented 3 years ago

Yep, seems like so.

pabloneruda1 commented 2 years ago

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()

lutek commented 2 years ago

workaround

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