http://www.perlmonks.org?node_id=724389


in reply to How to safely test if a database handle is capable of transactions?

Hi,

While it's not breaking encapsulation, as Limbic~Region corrected, using 'begin_work' is the recommended way, as you get blamed by the driver if it's not supported

eval { $dbh->begin_work; # ... $dbh->commit; }; if ($@) { $dbh->rollback; # If you want to be paranoid, check if it rolled back. # Look at manual: man DBI }

Hope this helps you a bit

Regards,

Update: corrected about the encapsulation, thanks Limbic~Region

fmerges at irc.freenode.net
  • Comment on Re: How to safely test if a database handle is capable of transactions?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to safely test if a database handle is capable of transactions?
by Limbic~Region (Chancellor) on Nov 18, 2008 at 23:24 UTC
    fmerges,
    Where is encapsulation being broken?
    $dbh->{AutoCommit} = 0;
    The DBI is a tied interface and you are not really breaking encapsulation by doing this. This is a perfectly acceptable way of doing business with this module - just see the docs.

    You did indirectly answer the OP's question (begin_work). According to the docs - If the driver does not support transactions then when begin_work attempts to set AutoCommit off the driver will trigger a fatal error. You just need to work it a bit differently since the OP wants to know if transactions are supported prior to trying a transaction. This means checking the actual contents of $@ rather than just seeing if it is set.

    Cheers - L~R

Re^2: How to safely test if a database handle is capable of transactions?
by ikegami (Patriarch) on Nov 19, 2008 at 17:13 UTC
    The driver will die (or return an error if RaiseError => 0) whichever interface you use if transactions aren't unsupported.