[r=julian-edwards][bug=1387414][author=allenap] General fix for concurrency/race problems when registering event types.
This undoes an earlier fix that used a global database lock to prevent concurrency and instead falls back on detecting database integrity violations. It absorbs some code that already did this, simplifies it, and adds a specific test for races.