I guess it's the unlock_tables() that you want to run on all except the last iteration, right? you can move it to the beginning of the loop and skip it in the first iteration:
my $firsttime = 1;
foreach my $id (@list_o_object_ids) {
unless ($firsttime) {
unlock_tables();
lock_db_tables();
} else {
$firsttime = 0;
}
my $obj = new Thingy($id); # gets stuff from db
# make changes, save back to db.
}
Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
-- P. Simon in Mrs. Robinson |
| [reply] [d/l] [select] |
No, that's not right. I want to lock the tables on all but the first iteration. Or to view this problem another way, I want to do a heck of a lot (about 1100 lines) of work the first time through the loop that is not repeated subsequent times. Right now, the first-loop-specific stuff is not in the loop at all, and due to it's size I'd prefer to keep it outside the loop. This is not a problem except that the pre-loop work and the first loop iteration must be within a single database transaction, while each subsequent loop is also it's own transaction.
As I've already shown, (and you have shown a slightly different way) this problem can be solved with a semaphore variable and a check of the sempahore each trip through the loop. I just don't like that solution because it seems messy and wonder if there is a cleaner way. I was hoping to find some syntacical thingy that behaves kind of like a join, but with blocks of code rather than array elements. Here's some pseudo code to demonstrate what I had in mind.
lock_db_tables(); # lock tables and/or start a transaction
# in a database independent way
# 1100 lines of preparation that requires a database lock
# must be in same transaction as first iteration of loop
# below
foreach my $id (@list_o_object_ids) {
my $obj = new Thingy($id); # gets stuff from db
# make changes, save back to db. (about 900 lines in my case)
unlock_tables();
} join {
lock_db_tables();
}
The lock_db_tables() within the join block would be executed between each loop iteration, but not before the first iteration, nor after the last iteration.
--DrWhy
"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."
| [reply] [d/l] [select] |
This is strange. Either you edited your node or I completely misunderstood the code you had in it. My memory is rather weak most of the time, but I do believe the code looked different when I was writing my reply. I do believe you were trying to skip the action for the last iteration in the code, not the first one. Though I may be wrong.
Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
-- P. Simon in Mrs. Robinson |
| [reply] |