Android-Room-Database-Backup icon indicating copy to clipboard operation
Android-Room-Database-Backup copied to clipboard

Do I have to restart the app after backing up or restoring?

Open simphonydeveloper opened this issue 8 months ago • 1 comments

After using backup or restore, any new or editing operations will cause the program to crash. message:Cannot perform this operation because the connection pool has been closed. Will it be supported in the future?, or have I missed something. Thanks.

simphonydeveloper avatar May 19 '25 07:05 simphonydeveloper

After using backup or restore, any new or editing operations will cause the program to crash. message:Cannot perform this operation because the connection pool has been closed. Will it be supported in the future?, or have I missed something. Thanks.

Hi @simphonydeveloper. I think I’m arriving here very late, but I faced this same problem recently.

This happens because, in the most recent versions of Room (2.6+), you can’t close the Room database and keep using the old DAOs. After any backup or restore, or if you close the database, you must create a new RoomDatabase instance and get fresh DAOs from it. Otherwise, any operation you try will throw a “connection pool has been closed” error.

The right way now is: after closing or replacing the database file, always drop any old DAO or database references and create new ones before doing anything else. There’s no way to just “reopen” the old instance and keep using the same DAOs.

It’s not a bug, it’s a change in how Room works to avoid data issues or strange bugs.

In my case: For backup: I don’t close the database before performing the backup, I just execute a PRAGMA query to force a SQLite checkpoint and finish all read and write operations: query("PRAGMA wal_checkpoint(FULL)", null)

For restore: I close the database, and after it finishes, I restart the application.

I hope this solution helps other devs in the future.

omesquita avatar Oct 30 '25 01:10 omesquita