Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

DBI AutoCommit clarification

by Grygonos (Chaplain)
on Sep 18, 2003 at 17:17 UTC ( [id://292457]=perlquestion: print w/replies, xml ) Need Help??

Grygonos has asked for the wisdom of the Perl Monks concerning the following question:

I'm sitting here with my copy of Programming the Perl DBI and I'm confused. The following code hurts my wee little brain

my $dbh = DBI->connect('dbi:mydriver:mydsn', {AutoCommit=>0, RaiseError=>1});

puny weakling you say... why does this hurt your brain? The comment says that this enables AutoCommit and RaiseError. How is this accomplished by setting one of them to zero and the other to one? This doesn't make sense to me. Traditionally 1 is true and 0 is false, so by quick inspection it would seem that this code disables AutoCommit Can someone make the brain pain go away?


Thanks,
Gry

Replies are listed 'Best First'.
Re: DBI AutoCommit clarification
by gmax (Abbot) on Sep 18, 2003 at 17:49 UTC

    What the book says exactly is (pag. 159) :

    Connect to the database with transactions and error handling enabled.

    What this comment means is that, having AutoCommit disabled, you can use transactions, where you use begin, commit, or rollback.

    When Autocommit is on, instead, every statement is automatically committed, and therefore transactions are disabled, i.e. you can't rollback.

    _ _ _ _ (_|| | |(_|>< _|
      When Autocommit is on, instead, every statement is automatically committed, and therefore transactions are disabled, i.e. you can't rollback.
      This is mostly correct. However, with MS-SQL and Sybase (and possibly others) you can still use transactions by explicitly issuing BEGIN TRAN requests in do() and/or prepare() requests.

      Michael

        And then there is always the $dbh->begin_work() method, but I don't know how many drivers support it at this time; I know that neither DBD::mysql nor DBD::Pg support it right now. Oracle?

        The nice thing about  $dbh->begin_work() over local($dbh->{AutoCommit}) = 0; is that $dbh->begin_work() will not automatically commit on exiting scope which could be a bad thing if you missed catching an exception or forgot to rollback in the exception handler.

Re: DBI AutoCommit clarification
by NetWallah (Canon) on Sep 18, 2003 at 17:29 UTC
    The meaning of AutoCommit becomes clearer if you look at the documentation for the commit method (Relevant verbage highlighted):

    commit

    $rc = $dbh->commit or die $dbh->errstr;
    Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off. If AutoCommit is on, then calling commit will issue a ``commit ineffective with AutoCommit'' warning.

    I believe the book is attempting to distinguish between the condition where AutoCommit is UNDEFINED and the example where AutoCommit is set explicitly.

    A better explanation is in the comment below, in the documentation of "Transactions":

    $dbh->{AutoCommit} = 0; # enable transactions, if possible
Re: DBI AutoCommit clarification
by hmerrill (Friar) on Sep 18, 2003 at 18:42 UTC
    I don't have the book handy so I can't confirm what you're saying the book says. But, setting AutoCommit to 0 is turning AutoCommit *off* - that is, by setting AutoCommit to 0, autocommit will NOT be in effect, which allows you to do your own transaction processing.

    Setting RaiseError to 1 *enables* RaiseError - same as you thought. Again, not sure how this is worded in the book, but I have read the book and was not confused by this point, nor by the wording :-)
      I understand what transactions are and all about commit. I just read it wrong. Sorry..brainfart. Thanks.
Re: DBI AutoCommit clarification
by Anonymous Monk on Sep 18, 2003 at 17:45 UTC
    That explicitly "sets" the AutoCommit behaviour, in this case by turning AutoCommit off (it is on by default).
Re: DBI AutoCommit clarification
by Anonymous Monk on Jan 29, 2017 at 17:45 UTC
    Even though this is 10+ years old... I stumbled upon this looking for some details on AutoCommit

    If I'm not mistaken, everyone is missing the original question completely.

    Quoted "The comment says that this enables AutoCommit and RaiseError. How is this accomplished by setting one of them to zero and the other to one?."

    He said *enables* AutoCommit...

    AutoCommit=>0

    I checked out the book in question. Page 197 (the source of the code snippit) doesn't actually say that AutoCommit=>0 will enable it. It does note however that you should explicitly define it rather than relying on the default.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://292457]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found