close the dbhandle at the proper time