refactored code to use custom authentication manager and handle retry for stale database connection