Cannot access transaction id on database transaction

Created on 7 August 2024, 5 months ago
Updated 25 August 2024, 4 months ago

Problem/Motivation

Currently using transaction to replace the legacy popTransaction method ~~ we have to use unpile method with two parameter i.e transaction name and id
However class Transaction is having name() to retrieve the transaction name but there is no method to retrieve the transaction ID.

Steps to reproduce

Run the database transaction with usage of unpile method.

Proposed resolution

Include public function id() to handle the $this->id (To handle the protected property)

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

💬 Support request
Status

Fixed

Version

11.0 🔥

Component
Database  →

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇮🇳India ankitv18

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ankitv18
  • Pipeline finished with Success
    5 months ago
    #246751
  • Issue was unassigned.
  • Status changed to Needs review 5 months ago
  • 🇮🇳India ankitv18

    Please review MR!9113

  • Status changed to Needs work 5 months ago
  • 🇺🇸United States smustgrave

    Believe will need test coverage to show the issue

  • 🇮🇹Italy mondrake 🇮🇹

    Run the database transaction with usage of unpile method.

    Why? There should be no need to call methods from the TransactionManager in normal code.

    Just get a Transaction object from the Connection, then either roll it back or destroy it.

    The ID is an implementation detail, and not exposed on purpose.

    Maybe if you share your use case we can discuss it.

  • 🇮🇳India ankitv18

    #6 Make sense of not exposing transaction ID at first place.

    Call to deprecated method popTransaction() of class      
             Drupal\Core\Database\Connection:                         
             in drupal:10.2.0 and is removed from drupal:11.0.0. Use  
               TransactionManagerInterface methods instead. 

    Could you help me to fix this deprecation? I was using this $this->database->popTransaction($transaction->name()); and to replace it I've used $this->database->transactionManager()->unpile($transaction->name(), $transaction->id());

  • 🇮🇹Italy mondrake 🇮🇹

    Can you share the code that is triggering the deprecation? From the point where you assign $transaction to the point where you try and pop it? popTransaction() should not have been used either, likely

  • 🇮🇳India ankitv18

    @mondrake below is the method where I tried to replace it with unpile method.

    /**
       * Rename the cloned tables to the original tables.
       */
      protected function rename() {
        $transaction = $this->database->startTransaction('gdpr_rename_table');
    
        foreach (array_keys($this->tablesToAnonymize) as $table) {
          $gdprTable = self::GDPR_TABLE_PREFIX . $table;
          $this->database->schema()->dropTable($table);
          $this->database->schema()->renameTable($gdprTable, $table);
        }
    
        $this->database->popTransaction($transaction->name());
        
      }
    
    
  • Status changed to Needs review 4 months ago
  • 🇮🇹Italy mondrake 🇮🇹

    First of all, you are doing tables drops and renames here, which are DDL (Data Definition Language) operations. Not all DBs support transactions with DDL - most notably, MySql performs an autocommit in DDL, which vanifies the transaction.

    Since you are not doing any data manipulation here, you may simply avoid to wrap your code into a transaction.

    If you want to stick with it, the correct way (currently) to commit the transaction is by unsetting it

    unset($transaction);

    instead of calling popTransaction() or unpile(). See also the docs:

    Transaction;
    /**
    * Removes a Drupal transaction from the stack.
    *
    * The unpiled item does not necessarily need to be the last on the stack.
    * This method should only be called by a Transaction object going out of
    * scope.
    *
    * This method should only be called internally by a database driver.

    *
    * @param string $name
    * The name of the transaction.
    * @param string $id
    * The id of the transaction.
    *
    * @throws \Drupal\Core\Database\TransactionOutOfOrderException
    * If a Drupal Transaction with the specified name does not exist.
    * @throws \Drupal\Core\Database\TransactionCommitFailedException
    * If the commit of the root transaction failed.
    */
    public function unpile(string $name, string $id): void;

  • Status changed to RTBC 4 months ago
  • 🇮🇳India ankitv18

    Thanks @mondrake, it worked!!

  • 🇮🇳India ankitv18

    ankitv18 → changed the visibility of the branch 3466569-cannot-access-transaction to hidden.

  • Status changed to Fixed 4 months ago
  • 🇺🇸United States smustgrave

    Thanks for answering that @mondrake

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024