Updated database code to maintain connection to db as long as threads are active.