Merge pull request #26 from picturae/18153

Refactor and optimize the way the external and internal relations are fetched. refs #18153
Try reconnecting if db gone away during import. refs #17564
Merge pull request #38 from picturae/17564

Try reconnecting if db gone away during import. refs #17564