Added database connection checks. Tries to reconnect if disconnected.