Thinking for a while I understand that we dont need to count(*) query in order the while() to stop.. we can use if oldSize == users.size(); Because of sometime in the end the sql will return nothing