Why did the transaction that contained DDL statement throw an exception?

Viewed 88

For example

$conn = new PDO("mysql:host=127.0.0.1;dbname=test", 'root', '123456');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
try {
    $sql = "CREATE TABLE IF NOT EXISTS test2 (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `text` varchar(32) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    $conn->exec($sql);
    $conn->commit();
} catch (\Exception $e) {
    $conn->rollBack();
}

It works fine in PHP 7.4, but it does not work in PHP 8.0 and throws an exception on line $conn->commit();:

PDOException: There is no active transaction

1 Answers
Related