A couple of fixes for bug #8309.  It now locks the table before it does anything to it.