- some code refactoring, moving database connection code out of function

Change-Id: I13c16c65c071308ac51a74a15df6a3e2d6390094