Fixed database close and open.