{"record":{"id":"167fcbb96765bee1","repo":"yiisoft/yii2","slug":"db-connection-is-not-active-167fcb","errorCode":null,"errorMessage":"DB Connection is not active.","messagePattern":"DB Connection is not active\\.","errorType":"exception","errorClass":"InvalidCallException","httpStatus":null,"severity":"error","filePath":"framework/db/oci/Schema.php","lineNumber":398,"sourceCode":"    /**\n     * @Overrides method in class 'Schema'\n     * @see https://www.php.net/manual/en/function.PDO-lastInsertId.php -> Oracle does not support this\n     *\n     * Returns the ID of the last inserted row or sequence value.\n     * @param string $sequenceName name of the sequence object (required by some DBMS)\n     * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object\n     * @throws InvalidCallException if the DB connection is not active\n     */\n    public function getLastInsertID($sequenceName = '')\n    {\n        if ($this->db->isActive) {\n            // get the last insert id from the master connection\n            $sequenceName = $this->quoteSimpleTableName($sequenceName);\n            return $this->db->useMaster(function (Connection $db) use ($sequenceName) {\n                return $db->createCommand(\"SELECT {$sequenceName}.CURRVAL FROM DUAL\")->queryScalar();\n            });\n        } else {\n            throw new InvalidCallException('DB Connection is not active.');\n        }\n    }\n\n    /**\n     * Creates ColumnSchema instance.\n     *\n     * @param array $column\n     * @return T\n     */\n    protected function createColumn($column)\n    {\n        $c = $this->createColumnSchema();\n        $c->name = $column['COLUMN_NAME'];\n        $c->allowNull = $column['NULLABLE'] === 'Y';\n        $c->comment = $column['COLUMN_COMMENT'] === null ? '' : $column['COLUMN_COMMENT'];\n        $c->isPrimaryKey = false;\n        $this->extractColumnType($c, $column['DATA_TYPE'], $column['DATA_PRECISION'], $column['DATA_SCALE'], $column['DATA_LENGTH']);\n        $this->extractColumnSize($c, $column['DATA_TYPE'], $column['DATA_PRECISION'], $column['DATA_SCALE'], $column['DATA_LENGTH']);","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/oci/Schema.php#L380-L416","documentation":"yii\\db\\oci\\Schema::getLastInsertID() evaluates \"SELECT <sequence>.CURRVAL FROM DUAL\" on the master connection, and CURRVAL only exists inside an established session. The method checks $this->db->isActive and throws InvalidCallException when the connection is closed - Yii opens connections lazily, so a connection with no statement executed yet is also 'not active'.","triggerScenarios":"Calling $db->getLastInsertID('seq') before any query has run on that connection (lazy open not triggered), after $db->close(), or in a long-running worker whose connection was dropped and closed.","commonSituations":"Calling getLastInsertID() at the start of a request instead of using the insert command's result; queue workers that close connections between jobs; code paths where the preceding INSERT failed before opening the connection; Oracle returning no auto-generated key because the connection object is fresh.","solutions":["Call $db->open() (or run the INSERT first) before getLastInsertID().","Prefer the ID returned by $command->insert() - the Oracle driver uses RETURNING and avoids CURRVAL entirely.","In reconnect logic, catch InvalidCallException, reopen the connection, and redo the insert.","For a fresh sequence value, use an explicit sequence query: SELECT seq.NEXTVAL FROM DUAL."],"exampleFix":"// before\n$id = $db->getLastInsertID('SEQ_CUSTOMER');\n\n// after\n$db->open();\n$id = $db->getLastInsertID('SEQ_CUSTOMER');\n\n// better: avoid CURRVAL entirely\n$id = $db->createCommand('SELECT SEQ_CUSTOMER.NEXTVAL FROM DUAL')->queryScalar();","handlingStrategy":"validation","validationCode":"// Ensure the session exists before CURRVAL\nif (!$db->isActive) {\n    $db->open();\n}\n$id = $db->getLastInsertID($sequenceName);","typeGuard":null,"tryCatchPattern":"try {\n    $id = $db->getLastInsertID($sequenceName);\n} catch (\\yii\\base\\InvalidCallException $e) {\n    $db->open();\n    // redo the INSERT - CURRVAL is only valid after a sequence use in this session\n}","preventionTips":["Use the return value of $command->insert() (RETURNING-based) instead of getLastInsertID() on Oracle.","Open the connection explicitly at the start of worker jobs that need CURRVAL/NEXTVAL.","After $db->close() or a dropped connection, rerun the insert before asking for the last ID."],"tags":["yii2","php","oracle","oci","database","connection","last-insert-id"],"backgroundTag":"db-connection-not-active","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}