ADDED: an open database count

Added a count to keep track the number of the number connections open to the database so it will only be closed once no more connections are needed.