Fixed potential bug of failing to close sql objects.