Testing database deadlock retry logicI just spent a *really* long time writing a test for automatic deadlock retry in a database api module I maintain. So you or I don't have to figure it out in the future, here's the recipe. So firstly you can't use transactions to build the deadlock situation. That would be easy, but it defeats our purpose since your auto-deadlock retry logic is not going to be able to retry intra-transaction statements (of course you could implement full transaction replay, but that's another story). So I did it using cursors. Here is a perl sub that builds Sybase TSQL. It returns two values only because Sybase requires the cursor declaration in a separate batch. sub deadlock_sql {
my ($table1, $table1_char_col, $table2, $table2_col) = @_;
"declare the_cursor cursor for select $table1_char_col from $table1 for update",
sprintf 'declare @col_value varchar(100)
open the_cursor
fetch the_cursor into @col_value
waitfor delay "00:00:04" -- wait 4 seconds
update %s set %s = %s
-- only need to go round once to cause deadlock
', $table2, $table2_col, $table2_col;
}
So we are taking out a write lock on the first table, waiting 4 seconds, then taking out a write lock on the second table. Do that
twice in opposite order and you have yourself a deadlock:
sub cause_a_deadlock {
my $child_pid;
my $parent_retries;
pipe(FROM_CHILD, TO_PARENT);
if ($child_pid = fork) {
# parent
close(TO_PARENT);
my $dbh = new_dbh();
my @sql = deadlock_sql('table1', 'a_char_col_from_table1', 'table2', 'any_col_from_table_2');
sleep 2; # sleep half of sql sleep to ensure overlap
$dbh->exec_sql($sql[0]);
lives_ok { $dbh->exec_sql($sql[1]) };
$parent_retries = $dbh->deadlock_retry_attempts;
} else {
# child
close(FROM_CHILD);
my $dbh = new_dbh();
my @sql = deadlock_sql('table2', 'a_char_col_from_table2', 'table1', 'any_col_from_table_1');
$dbh->exec_sql($sql[0]);
lives_ok { $dbh->exec_sql($sql[1]) };
print TO_PARENT $dbh->deadlock_retry_attempts . "\n";
exit;
}
my $child_retries =
Of course the exact code will depend on your database and database abstraction layer. 07:35 PM, 20 Aug 2008 by Mark Aufflick Permalink | Short Link | Comments (0) |
Archive
November 2011 October 2011 April 2011 March 2011 January 2011 November 2010 October 2010 September 2010 August 2010 July 2010 June 2010 May 2010 April 2010 March 2010 February 2010 January 2010 October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 September 2007 August 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 Blog Categoriessoftware (39)..cocoa (21) ..heads up 'tunes (5) ..ruby (6) ..lisp (3) ..perl (4) ..openacs (1) mac (20) embedded (2) ..microprocessor (2) ..avr (1) electronics (3) design (1) photography (25) ..black and white (6) ..A day in Sydney (18) ..The Daily Shoot (6) food (2) Book Review (2) Notifications Request notifications
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||








Request notifications