use strict; use DBI; use Exception::Class::TryCatch; my $db = 'test'; my $hostname = 'localhost'; my $user = 'myuser'; my $dbpwd = 'xxxxxxxxxxxxx'; my $dbh = DBI->connect_cached("DBI:mysql:database=$db;host=$hostname",$user,$dbpwd,{RaiseError => 1}) or die "Failed to connect to the DB.\n"; ##$dbh->{AutoCommit} = 0; # enable transactions, if possible $dbh->{RaiseError} = 1; try eval { $dbh->begin_work or die $dbh->errstr; my $sql = "BEGIN;"; # $dbh->do($sql); $sql = "UPDATE foo SET y='aa' WHERE x=1;"; $dbh->do($sql); $sql = "SELECT SLEEP(10);"; $dbh->do($sql); $sql = "UPDATE foo SET y='bb' WHERE x=2;"; $dbh->do($sql); $dbh->commit; }; if ( catch my $err) { print $err->error,"\n"; eval { $dbh->rollback }; print "Exiting from catch block\n"; exit(0); } $dbh->disconnect; print "DONE!\n";