ensure db connection is closed at the end of each thread