larryl has asked for the wisdom of the Perl Monks concerning the following question:
Hi all -
I'm writing a module that receives a DBI database handle as input to my constructor. I'd like to be able to test if the handle is transaction-capable, so that I can conditionally wrap certain code in a transaction like so:
if ($self->{_can_do_transactions}) { local $dbh->{AutoCommit} = 0; eval { code_that_touches_db(); $dbh->commit; }; if ($@) { my $msg = $@; eval { $dbh->rollback }; die $msg; } } else { code_that_touches_db(); }
First question, is there a better way to do this?
UPDATE: Thanks to feedback, am now using something like this to handle transactions:
if ($self->{_can_do_transactions}) { # If RaiseError is true, begin_work() will: # return true if a new transaction was started # croak if already in a transaction # croak if transactions not supported # local $dbh->{RaiseError} = 1; my $started_a_new_transaction = 0; eval { $started_a_new_transaction = $dbh->begin_work }; eval { $code->(@_); $dbh->commit if $started_a_new_transaction; }; if ($@) { my $msg = $@; eval { $dbh->rollback } if $started_a_new_transaction; die $msg; } } else { code_that_touches_db(); }
Second, for testing the handle I am doing this in my constructor:
eval { local $dbh->{AutoCommit} = 0; }; $self->{_can_do_transactions} = $@ ? 0 : 1;
which seems to work for the DBs that I have tried, but is it safe, e.g. what if the handle is already mid-transaction? Is there a better way?
UPDATE: Thanks to feedback, am now using something like this to test for transaction capability:
# If RaiseError is false, begin_work() will: # return true if a new transaction was started # return false if already in a transaction # croak if transactions not supported # my $started_a_new_transaction = 0; eval { local $dbh->{RaiseError} = 0; $started_a_new_transaction = $dbh->begin_work; }; $self->{_can_do_transactions} = $@ ? 0 : 1; eval { $dbh->rollback } if $started_a_new_transaction;
Third, with the AutoCommit test set up as above, I'm occasionally seeing my test harness report errors like:
closing dbh with active statement handles during global destruction
if I croak later on in the constructor. For reasons I can't determine, changing the test just by adding a line like so causes those error messages to go away:
eval { my $val = $dbh->{AutoCommit}; # makes error go away? local $dbh->{AutoCommit} = 0; }; $self->{_can_do_transactions} = $@ ? 0 : 1;
Any ideas/comments much appreciated!
Larry
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to safely test if a database handle is capable of transactions?
by ikegami (Patriarch) on Nov 18, 2008 at 21:51 UTC | |
by larryl (Monk) on Nov 19, 2008 at 17:04 UTC | |
by larryl (Monk) on Nov 19, 2008 at 19:40 UTC | |
by ikegami (Patriarch) on Nov 19, 2008 at 21:50 UTC | |
|
Re: How to safely test if a database handle is capable of transactions?
by fmerges (Chaplain) on Nov 18, 2008 at 21:19 UTC | |
by Limbic~Region (Chancellor) on Nov 18, 2008 at 23:24 UTC | |
by ikegami (Patriarch) on Nov 19, 2008 at 17:13 UTC |