Modify to connect to database when app launch and close database when app terminal