Listing 10-1 is the complete, uninterrupted code example from this chapter.
Listing 10-1: Transactions in practice
01: #! /usr/bin/perl -w 02: # 03: # transaction.pl 04: # Chapter 11 05: # Listing 1 06: # 07: use strict; 08: use DBI; 09: my $dbh = DBI->connect ("DBI:mysql:BibleBook","bookuser","testpass", 10: {AutoCommit => 0, RaiseError => 1}) 11: or die("Cannot connect: $DBI::errstr\n"); 12: $dbh->begin_work; 13: eval { 14: $dbh->do("DELETE FROM pubslist"); 15: my $sth=$dbh->prepare("SELECT COUNT(*),publisher FROM library 16: GROUP BY publisher"); 17: $sth->execute; 18: while (my ($count,$publisher)=$sth->fetchrow_array) { 19: my $publisherq=$dbh->quote($publisher); 20: $dbh->do("INSERT INTO pubslist (publisher,count) 21: VALUES ($publisherq,$count)"); 22: } 23: $sth->finish; 24: }; 25: if ($@) { 26: $dbh->rollback; 27: } 28: else { 29: $dbh->commit; 30: } 31: $dbh->disconnect;