move database handle creation to top level