Fixed database leak by closing cursors when no more used.