Recently while running a large integration test suite on a Drupal site, we started getting the error: "Integrity constraint violation: 1062 Duplicate entry 'rules_event_whitelist' for key 'Primary'." The error would appear randomly and we couldn't find a way to reproduce the error consistently. Looking at stack trace, we discovered that the error is coming variable_set() function. If you are looking for a quick fix for your problem, add the following to your settings.php:
$databases['default']['default']['init_commands'] = array( 'isolation' => "SET SESSION tx_isolation='READ-COMMITTED'" );
In all likelihood, your error will disappear!