Fixed some db resource leaks

Most of leaks come from cursors. It will soon cause the App to crash with a big account tree.