fixed bug when closing connection to db to early