Move database connection to a function.