yiisoft/yii2 · error · InvalidCallException

DB Connection is not active.

Error message

DB Connection is not active.

What it means

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'.

Source

Thrown at framework/db/oci/Schema.php:398

    /**
     * @Overrides method in class 'Schema'
     * @see https://www.php.net/manual/en/function.PDO-lastInsertId.php -> Oracle does not support this
     *
     * Returns the ID of the last inserted row or sequence value.
     * @param string $sequenceName name of the sequence object (required by some DBMS)
     * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object
     * @throws InvalidCallException if the DB connection is not active
     */
    public function getLastInsertID($sequenceName = '')
    {
        if ($this->db->isActive) {
            // get the last insert id from the master connection
            $sequenceName = $this->quoteSimpleTableName($sequenceName);
            return $this->db->useMaster(function (Connection $db) use ($sequenceName) {
                return $db->createCommand("SELECT {$sequenceName}.CURRVAL FROM DUAL")->queryScalar();
            });
        } else {
            throw new InvalidCallException('DB Connection is not active.');
        }
    }

    /**
     * Creates ColumnSchema instance.
     *
     * @param array $column
     * @return T
     */
    protected function createColumn($column)
    {
        $c = $this->createColumnSchema();
        $c->name = $column['COLUMN_NAME'];
        $c->allowNull = $column['NULLABLE'] === 'Y';
        $c->comment = $column['COLUMN_COMMENT'] === null ? '' : $column['COLUMN_COMMENT'];
        $c->isPrimaryKey = false;
        $this->extractColumnType($c, $column['DATA_TYPE'], $column['DATA_PRECISION'], $column['DATA_SCALE'], $column['DATA_LENGTH']);
        $this->extractColumnSize($c, $column['DATA_TYPE'], $column['DATA_PRECISION'], $column['DATA_SCALE'], $column['DATA_LENGTH']);

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Call $db->open() (or run the INSERT first) before getLastInsertID().
  2. Prefer the ID returned by $command->insert() - the Oracle driver uses RETURNING and avoids CURRVAL entirely.
  3. In reconnect logic, catch InvalidCallException, reopen the connection, and redo the insert.
  4. For a fresh sequence value, use an explicit sequence query: SELECT seq.NEXTVAL FROM DUAL.

Example fix

// before
$id = $db->getLastInsertID('SEQ_CUSTOMER');

// after
$db->open();
$id = $db->getLastInsertID('SEQ_CUSTOMER');

// better: avoid CURRVAL entirely
$id = $db->createCommand('SELECT SEQ_CUSTOMER.NEXTVAL FROM DUAL')->queryScalar();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the session exists before CURRVAL
if (!$db->isActive) {
    $db->open();
}
$id = $db->getLastInsertID($sequenceName);

Try / catch

try {
    $id = $db->getLastInsertID($sequenceName);
} catch (\yii\base\InvalidCallException $e) {
    $db->open();
    // redo the INSERT - CURRVAL is only valid after a sequence use in this session
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/167fcbb96765bee1. Report an issue: GitHub.