[rt-users] Database transaction handling

Maciej Dobrzanski reg.bestpractical at posterus.com
Fri Jul 10 03:37:16 EDT 2015


Hello,

I have extensions or scripts where I'm updating several things such as 
changing ticket status and multiple custom field values. In such cases 
partial updates would be bad, so I'm wondering about wrapping such code 
inside a database transaction, so that if there's a problem somewhere in 
the middle, I can get things back to where they were before my code 
started. But this raises a couple of questions:

- Are scrips implicitly wrapped inside database transactions, so that 
rolling back is handled automatically if a scrip fails? If so, is it 
enough to return 0 from Commit method or does the scrip have to fail in 
some other fashion? Or if not, can I safely explicitly use transactions 
inside the scrip code?

- I found this somewhere in your extensions. Is this a recommended way 
of starting a transaction, so that it never - even accidentally - can 
interfere with pre-existing transactions?

    my $in_txn = RT->DatabaseHandle->TransactionDepth;
    RT->DatabaseHandle->BeginTransaction unless $in_txn;
    ...
    RT->DatabaseHandle->Commit unless $in_txn;

Maciej




More information about the rt-users mailing list