Fixed possible error due to opening DB at file level instead of at function level