Fix bug when model cannot be built

Tries to close db connection twice leading to error.