Using SET CHARACTER SET utf8 after using SET NAMES utf8 will actually reset the character_set_connection and collation_connection to
@@character_set_database and @@collation_database respectively.
The manual states that
SET NAMES x is equivalent to
SET character_set_client = x;
SET character_set_results = x;