I am using Drupal behind a reverse proxy/cache layer (e.g. Cloud Front/ Akamai) and sometimes the service goes quite slow (so I get a Gateway timeout, for reasons such as too many people are using the servers) or a something bad happens in the server farm (docker micro-architecture) and so I get a 502 Bad Gateway.
Do we know if a database transaction will roll back in such cases? This is especially relevant when doing 800+ entity updates via the Batch API.
E.g. (based off mock code: https://www.drupal.org/docs/drupal-apis/database-api/database-transactions)
$transaction = $connection->startTransaction(); try { // Do some thing that writes to the database. $entity = create_some_entity(); $entity->save(); // Pretend a 502 Bad Gateway or Gateway timeout happened here. // Do another database write that depends upon the first. $dependent_entity = update_dependent_entity($entity->id()); $dependent_entity->save(); } catch (Exception $e) { // There was an error in writing to the database, so the database is rolled back // to the state when the transaction was started. // Not sure if catching an exception will do anything here. // (since no exception is expected) $transaction->rollBack(); } // Commit the transaction by unsetting the $transaction variable. unset($transaction);