General developer forum

Transactions - are the docs correct

 
Picture of Pete Smith
Transactions - are the docs correct
 

The Data API page gives an example of a transaction. Is it correct/ Shouldn't the creation of the transaction object come before the try? How can it rollback if it's not in scope?

global $DB;
try {
$transaction = $DB->start_delegated_transaction();
// Insert a record
$DB->insert('foo', $object);
$DB->insert('bar', $otherobject);
 
// Assuming the both inserts work, we get to the following line.
$transaction->allow_commit();
} catch(Exception $e) {
$transaction->rollback($e);
}

 
Average of ratings: Useful (1)
Picture of Marcus Green
Re: Transactions - are the docs correct
Core developersParticularly helpful MoodlersPlugin developersTesters

As many installations will be using databases such as MySQL with ISAM, will this make any difference anyway?

 
Average of ratings: -
Picture of Darko Miletić
Re: Transactions - are the docs correct
Core developersParticularly helpful Moodlers

Using MyISAM is not recommended for Moodle 2.

 
Average of ratings: Useful (1)
Picture of Marcus Green
Re: Transactions - are the docs correct
Core developersParticularly helpful MoodlersPlugin developersTesters

It may not be recommended but it is possible and 10+ years of Moodle code have been written on the assumption  that transactions are not available. Writing code that assumes they are available may be a high risk strategy.

 
Average of ratings: -
Picture of Darko Miletić
Re: Transactions - are the docs correct
Core developersParticularly helpful Moodlers

There are no risks here. In case Moodle is set to use non-transactional rdbms starting transaction will do nothing. This is by design.

 
Average of ratings: Useful (1)
Picture of Marcus Green
Re: Transactions - are the docs correct
Core developersParticularly helpful MoodlersPlugin developersTesters

There is a risk if you write code that assumes that transactions will do something and then it is run on a system where they do not do anything.

Of course you could write code that assumes that transactions will not do anything, but I cannot think of a good reason to do that.

 
Average of ratings: -
Picture of Darko Miletić
Re: Transactions - are the docs correct
Core developersParticularly helpful Moodlers

Developer should know what he is doing. That always applies.

 
Average of ratings: -
Picture of Marcus Green
Re: Transactions - are the docs correct
Core developersParticularly helpful MoodlersPlugin developersTesters

Really?

 
Average of ratings: -
Picture of Darko Miletić
Re: Transactions - are the docs correct
Core developersParticularly helpful Moodlers

This is PHP not C++. Variable once defined within a specific context is available everywhere within that context. And PHP has less contexts than most of the classic languages. You have global, function, class method and namespace. Everything within one of those contexts is avaliable at any point within that same context after the declaration. Which is why the example is OK. But there are other issues with that specific example. What is not obvious is that rollback throws another exception which is rather annoying. So what I do usually is this:

 
function testdb() {
    global $DB;

    try {
        try {
            $transaction = $DB->start_delegated_transaction();
            // Do something here.
            $transaction->allow_commit();
        } catch (Exception $e) {
            // Make sure transaction is valid.
            if (!empty($transaction) && !$transaction->is_disposed()) {
                $transaction->rollback($e);
            }
        }
    } catch (Exception $e) {
        // Silence the rollback exception or do something else.
    }

}
 
Average of ratings: Useful (5)
Picture of Pete Smith
Re: Transactions - are the docs correct
 

Thanks Darko. In all the time I've worked with PHP I hadn't appreciated that about the contexts.

 
Average of ratings: -