fix closing bug
Fixes bug in closing database connections