Grygonos has asked for the wisdom of the Perl Monks concerning the following question:
I come seeking your guidance in managing some transactions I'm doing w/ DBI. I have the following subroutine, which will be handling the transaction
sub insertRecord { my ($record,$dbh,$tapeID) = @_; my %max = getMaxID($dbh); my $sth; $sth = $$dbh->prepare(addressQuery()); $sth->execute($max{'addresses'}, $$record{'subscriber_address_one'}, $$record{'subscriber_address_two'}, $$record{'subscriber_city'}, $$record{'subscriber_state'}, $$record{'subscriber_zip'}, $$record{'subscriber_zip4'}, $$tapeID) or do{$$dbh->rollback(); return 0}; ##Execute more queries in the same fashion $$dbh->commit(); return 1; }
The variable that the return code is received into then is checked and the main program will write the record to an error log if we return 0 at any point. Is the the $sth->execute(....) or do {$$dbh->rollback; return 0;};portion of the code an ok way to handle this? I have a set of tables which obviously have keys into each other, so they must be inserted in a certain order to achieve integrity, so there are about 7 queries being run .. but all in the same fashion as the one I listed
Any other monks have experience with managing transactions of this nature w/ DBI?
<PS> the getMaxID($dbh); sub just returns the max max+1 of an id column for each table that i'm inserting into. Since I have to know the value of my foreign key for inserting into other tables I can't allow SQL Server to have these be identity columns(also for identity insert reasons), but they are primary keys</PS>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Managing DBI Transactions
by dragonchild (Archbishop) on Mar 30, 2004 at 15:17 UTC | |
|
Re: Managing DBI Transactions
by bart (Canon) on Mar 30, 2004 at 15:24 UTC | |
by Grygonos (Chaplain) on Mar 30, 2004 at 15:33 UTC | |
|
Re: Managing DBI Transactions
by mpeppler (Vicar) on Mar 30, 2004 at 15:32 UTC |