The problem with putting it into an eval block is that it will be commited
no matter what and then your rollback statement is useless.  You should
really check on status of execute to commit/rollback based on that.  Also
with autocommit off is any of the statements fail though stopping the
program, the inserts/updates would not of been commited.  In your case eval
prevents from failing a script, though commiting no matter what.

Ilya Sterin

-----Original Message-----
From: Peter J. Schoenster [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 04, 2001 6:50 PM
To: [EMAIL PROTECTED]
Subject: Re: executing atomic transactions in DBI


On 4 May 2001, at 14:46, traja wrote:

> Hi,
>
> I have 2 insert statements which insert data into two different
> tables. These 2 inserts need to be run atomically, How can I do this?
> If I commit at the end of the two inserts, would that work?

If your rdbms supports it and you have autocommit off,

Page 159 of "Programming the Perl DBI" has the code (and all
sorts of other useful info easily accessible).

Here's a snippet from one of my test scripts, essentially I put the
dbi work in the eval statement. The Perl DBI book has a lot more
info on this.

eval {
        my $sql = qq|INSERT INTO company_profile
(company_description,company_name,culture,employees,employer
_id,growth_rate,leadership,mission,sec_fillings_url,type,url,year)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?)|;
        my @vals = qw(sadfds 007_company_name sadfsadfsa 500 47
91-100% sadfsad afsadf sdfdsa established sadfsadf 1965);
        my $sth;
        $sth= $dbh->prepare($sql);
        $sth->execute(@vals);

        $sql = qq|UPDATE users set username = 'NOT' where f_id = ?|;
        $sth= $dbh->prepare($sql);
        $sth->execute('49');

        $sth->finish();
        $dbh->commit;
};

if($@) {
        $dbh->rollback;
        print qq|Cry me to sleep: $@<hr>|;
}


---------------------------
"Reality is that which, when you stop believing in it, doesn't go
away".
                -- Philip K. Dick

Reply via email to