Should close $wpdb via $wpdb->close()
The close() function was added for completeness in the database abstraction. If the database is prematurely closed in a plugin or theme (as you are using it), the next call to wpdb::query() will re-open the connection. Your Commands out of sync error is a MySQL error, not a WordPress one, and it might be coming … Read more