Changed open and close database connections for better performance.