I'm trying to put some DBIX transactions under an eval block to either make all transactions or none if something goes wrong in the middle.
I've got several transactions being done like this and none of them is giving me problems. None but one:
The way I build the transactions is rather tricky and, for what it's worth, these actually work. I can post that chunk of code if you want, but maybe the eval block will suffice:
eval {
for (my $sub = 0; $sub < $neuroexam_index; $sub++){
$transactions{neuroexams}{$sub}->insert;
}
} or die $!;
The problem would be that the eval shows an error ([error] Caught exception in pbitdb::Controller::Subjects->add "Died at /home/lioneluranl/svn/pbitdb/pbitdb/script/../lib/pbitdb/Controller/Subjects.pm line 411.") in my console, but actually performs the insertions. Why would that be?
eval {
for (my $sub = 0; $sub < $neuroexam_index; $sub++){
$transactions{neuroexams}{$sub}->insert;
}
1; # No exception.
}
or do {
# ... Perform rollback here ...
die("[error] Caught exception in pbitdb::Controller::Subjects->add: $@");
};
or
if (!eval {
for (my $sub = 0; $sub < $neuroexam_index; $sub++){
$transactions{neuroexams}{$sub}->insert;
}
1; # No exception.
}) {
# ... Perform rollback here ...
die("[error] Caught exception in pbitdb::Controller::Subjects->add: $@");
}
Three changes:
1; was added to ensure a true value is returned when there is no exception to ensure the RHS of the or isn't evaluated.$@, not $!. ($! maps to errno, the error code set by C library calls and by system calls.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With