Cache database connection in thread