Bug 15854: Use a READ and WRITE LOCK on message_queue

To make sure we will not never get a race conditions for these kinds of
notices, we need to add a LOCK on the message_queue table.

This does not smell the best way to do that, but I faced deadlock issues
when I tried to use "UPDATE FOR"

https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
https://dev.mysql.com/doc/refman/5.7/en/lock-tables.html
https://dev.mysql.com/doc/refman/5.7/en/commit.html

To test this patch, or another solution, you need to apply manually this
change:

         my $message = C4::Message->find_last_message($borrower, $type, $mtt);
         unless ( $message ) {
+            sleep(1);
             C4::Message->enqueue($letter, $borrower, $mtt);
         } else {

And repeat the test plan from first patch.
Do not forget to truncate the message_queue table.

Followed test plans, works as expected.
Signed-off-by: Marc Véron <veron@veron.ch>

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2017-02-09 12:44:38 +01:00 committed by Brendan A Gallagher
parent 607b14516a
commit be156d9ad9

View file

@ -3341,6 +3341,7 @@ sub SendCirculationAlert {
});
my $issues_table = ( $type eq 'CHECKOUT' || $type eq 'RENEWAL' ) ? 'issues' : 'old_issues';
my $schema = Koha::Database->new->schema;
my @transports = keys %{ $borrower_preferences->{transports} };
for my $mtt (@transports) {
my $letter = C4::Letters::GetPreparedLetter (
@ -3358,13 +3359,19 @@ sub SendCirculationAlert {
}
) or next;
$schema->storage->txn_begin;
C4::Context->dbh->do(q|LOCK TABLE message_queue READ|);
C4::Context->dbh->do(q|LOCK TABLE message_queue WRITE|);
my $message = C4::Message->find_last_message($borrower, $type, $mtt);
unless ( $message ) {
C4::Context->dbh->do(q|UNLOCK TABLES|);
C4::Message->enqueue($letter, $borrower, $mtt);
} else {
$message->append($letter);
$message->update;
}
C4::Context->dbh->do(q|UNLOCK TABLES|);
$schema->storage->txn_commit;
}
return;