[prev in list] [next in list] [prev in thread] [next in thread] 

List:       dbi-users
Subject:    AW: Need help with an unexpected behaviour
From:       "Andreas Mock" <andreas.mock () web ! de>
Date:       2018-01-28 14:12:43
Message-ID: 001801d39842$11021910$33064b30$ () web ! de
[Download RAW message or body]

Hi Tim,

thank you for your answer. Besides of the explanation given,
do you think someone should expect this behaviour?

Is it something that should be explained and warned about
explicitly?

Best regards
Andreas


-----Ursprüngliche Nachricht-----
Von: Tim Bunce [mailto:Tim.Bunce@pobox.com] 
Gesendet: Sonntag, 28. Januar 2018 14:44
An: Andreas Mock <andreas.mock@web.de>
Cc: dbi-users@perl.org
Betreff: Re: Need help with an unexpected behaviour

On Fri, Jan 26, 2018 at 04:47:35PM +0100, Andreas Mock wrote:
> Hi all,
> 
>   9 my %attr = (
>  10     'AutoCommit' => 1,
>  11     'RaiseError' => 1,

>  35 sub doit {
>  36     my $dbh = shift;
>  37
>  38     local $dbh->{'AutoCommit'} = 1;
>  39
>  40     $dbh->begin_work;
>  41     $dbh->do("insert into mca_rb_test values ('short')");
>  42     $dbh->do("insert into mca_rb_test values
> ('looooooooooooooooooooooooooooooooooooooooooooooooooong')");
>  43     $dbh->commit;

> Whithout line 38 I get what I expect. [...]

> BUT: As soon as I have line 38 in there, which shouldn't change the 
> initially set 'AutoCommit', the first insert is commited to the 
> database even the exeption is raised in the opened transaction.

That seems like a driver bug at first sight.

While "local $dbh->{'AutoCommit'} = 1;" looks like a simple hash assignment
there's a lot going on behind the scenes.
($dbh is a ref to a tied hash so a STORE method gets called to handle the
assignment.)

While it doesn't "change" the AutoCommit setting, since it's already
enabled, the "local" does cause Perl to arrange to execute
$dbh->{'AutoCommit'} = 1 when the scope exits.

Both the initial assignment and the re-setting assignment may have side
effects.

> Can someone explain what is happening behind the scenes or give a 
> pointer to some helpful documentation which I have overlooked?

The DBI (and most drivers) have extensive tracing built in.
The trace output is often very helpful to see what's really happening.
See https://metacpan.org/pod/DBI#TRACING

It'll show you the effect of the local AutoCommit assignment and re-setting
assignment at scope-exit.

Tim.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic