Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions lib/private/DB/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,11 @@ public function __construct($conn) {
/**
* @param string $table name
*
* @return int id of last insert statement, 0 in case there was no INSERT before or it failed to get the ID
* @return int id of last insert statement
* @throws Exception
*/
public function lastInsertId($table, bool $allowRetry = true): int {
$return = $this->conn->realLastInsertId($table);
if ($return === 0 && $allowRetry) {
/**
* During a reconnect we are losing the connection and when the
* realLastInsertId call is the one triggering the reconnect, it
* does not return the ID. But inside the reconnect, we were able
* to save the last insert id, so calling it a second time is going
* to be successful.
* We can not return the result on the initial call, as we are already
* way deeper in the stack performing the actual database query on
* the doctrine driver.
*/
return $this->lastInsertId($table, false);
}
return $return;
public function lastInsertId($table) {
return (int)$this->conn->realLastInsertId($table);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/DB/AdapterOCI8.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace OC\DB;

class AdapterOCI8 extends Adapter {
public function lastInsertId($table, bool $allowRetry = true): int {
public function lastInsertId($table) {
if (is_null($table)) {
throw new \InvalidArgumentException('Oracle requires a table name to be passed into lastInsertId()');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/DB/AdapterPgSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class AdapterPgSql extends Adapter {

public function lastInsertId($table, bool $allowRetry = true): int {
public function lastInsertId($table) {
$result = $this->conn->executeQuery('SELECT lastval()');
$val = $result->fetchOne();
$result->free();
Expand Down
29 changes: 5 additions & 24 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ 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' => [
Expand Down Expand Up @@ -512,9 +510,9 @@ protected function logQueryToFile(string $sql, array $params): void {
* because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
* columns or sequences.
*
* @param ?string $name Name of the sequence object from which the ID should be returned.
* @param string $seqName Name of the sequence object from which the ID should be returned.
*
* @return int the last inserted ID, 0 in case there was no INSERT before or it failed to get the ID
* @return int the last inserted ID.
* @throws Exception
*/
public function lastInsertId($name = null): int {
Expand All @@ -528,13 +526,8 @@ public function lastInsertId($name = null): int {
* @internal
* @throws Exception
*/
public function realLastInsertId($seqName = null): int {
if ($this->lastInsertId !== 0) {
$lastInsertId = $this->lastInsertId;
$this->lastInsertId = 0;
return $lastInsertId;
}
return (int)parent::lastInsertId($seqName);
public function realLastInsertId($seqName = null) {
return parent::lastInsertId($seqName);
}

/**
Expand Down Expand Up @@ -903,23 +896,11 @@ private function reconnectIfNeeded(): void {
if (
!isset($this->lastConnectionCheck[$this->getConnectionName()]) ||
time() <= $this->lastConnectionCheck[$this->getConnectionName()] + 30 ||
$this->isTransactionActive() ||
$this->disableReconnect
$this->isTransactionActive()
) {
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();
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Comments/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ public static function providerTestReactionAddAndDelete(): array {
* @return array<string, IComment>
*/
private function proccessComments(array $data): array {
$this->connection->beginTransaction();
/** @var array<string, IComment> $comments */
$comments = [];
foreach ($data as $comment) {
Expand All @@ -1088,6 +1089,7 @@ private function proccessComments(array $data): array {
$comment = $this->testSave($message, $actorId, $verb, $parentId, $id);
$comments[$comment->getMessage() . '#' . $comment->getActorId()] = $comment;
}
$this->connection->commit();
return $comments;
}

Expand Down
Loading