add retry to mysql queries and add exit function