my $data = $thing->fetch; $thing->lock; $thing->write('new data'); $thing->unlock; #### if ($bcast_id) { my $broadcast_transaction_status = eval { # We have to disable AutoCommit so that all of the DB tasks # are build into a single transaction. This locks the row. $self->dbh->{AutoCommit} = 0; $self->dbh->do('BEGIN'); # We need to ensure the broadcast is still available. If it is, # we claim it. If it isn't, we do nothing my $bcast_status_check_query = qq~ SELECT BroadcastID FROM $db_table WHERE BroadcastID=? AND HostRunning IS NULL FOR UPDATE ~; my $broadcast_unclaimed = $self->dbh->selectrow_array( $bcast_status_check_query, undef, $bcast_id ); if ($broadcast_unclaimed) { # We only update the DB table with broadcast claimed status # if nobody else has claimed it yet $self->dbh->do( qq~ UPDATE $db_table SET HostRunning=? WHERE BroadcastID=? AND HostRunning IS NULL; ~, undef, "$hostname, Process $$", $bcast_id ); } else { # If the broadcast was claimed in between our first check and our # second check inside the transaction, we set this broadcast to no # longer available $bcast_id = 0; } # Commit the transaction and re-enable AutoCommit so that # it doesn't impact other DB operations $self->dbh->commit; $self->dbh->{AutoCommit} = 1; 1; } } if (! $broadcast_transaction_status) { $self->dbh->rollback; $bcast_id = 0; _email_sysadmins( $self->dbh, "Broadcast $bcast_id claim rolled back", "Broadcast $bcast_id host claim transaction rolled back: $@" ); }