Added disconnect logic to run db query when no clients are connected