@ -92,6 +92,8 @@ class Connection extends PrimaryReadReplicaConnection {
protected ShardConnectionManager $shardConnectionManager;
protected AutoIncrementHandler $autoIncrementHandler;
protected bool $isShardingEnabled;
protected bool $disableReconnect = false;
protected int $lastInsertId = 0;
public const SHARD_PRESETS = [
'filecache' => [
@ -510,9 +512,9 @@ class Connection extends PrimaryReadReplicaConnection {
* because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
* columns or sequences.
*
* @param string $seqN ame Name of the sequence object from which the ID should be returned.
* @param ?string $n ame Name of the sequence object from which the ID should be returned.
*
* @return int the last inserted ID.
* @return int the last inserted ID, 0 in case there was no INSERT before or it failed to get the ID
* @throws Exception
*/
public function lastInsertId($name = null): int {
@ -526,8 +528,13 @@ class Connection extends PrimaryReadReplicaConnection {
* @internal
* @throws Exception
*/
public function realLastInsertId($seqName = null) {
return parent::lastInsertId($seqName);
public function realLastInsertId($seqName = null): int {
if ($this->lastInsertId !== 0) {
$lastInsertId = $this->lastInsertId;
$this->lastInsertId = 0;
return $lastInsertId;
}
return (int)parent::lastInsertId($seqName);
}
/**
@ -896,11 +903,23 @@ class Connection extends PrimaryReadReplicaConnection {
if (
!isset($this->lastConnectionCheck[$this->getConnectionName()]) ||
time() < = $this->lastConnectionCheck[$this->getConnectionName()] + 30 ||
$this->isTransactionActive()
$this->isTransactionActive() ||
$this->disableReconnect
) {
return;
}
if ($this->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) {
/**
* Before reconnecting we save the lastInsertId, so that if the reconnect
* happens between the INSERT executeStatement() and the getLastInsertId call
* we are able to return the correct result after all.
*/
$this->disableReconnect = true;
$this->lastInsertId = (int)parent::lastInsertId();
$this->disableReconnect = false;
}
try {
$this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL());
$this->lastConnectionCheck[$this->getConnectionName()] = time();