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..."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.