Skip to content

[MS-936] Fix the db locked issue by Refactor EventDatabaseFactory to create a single instance of the event DB#1139

Merged
meladRaouf merged 1 commit into
mainfrom
bugfix/MS-936-db-locking
Mar 19, 2025
Merged

[MS-936] Fix the db locked issue by Refactor EventDatabaseFactory to create a single instance of the event DB#1139
meladRaouf merged 1 commit into
mainfrom
bugfix/MS-936-db-locking

Conversation

@meladRaouf
Copy link
Copy Markdown
Collaborator

@meladRaouf meladRaouf commented Mar 12, 2025

MS-936
Will be released in: 2025.2.0

Root cause analysis (for bugfixes only)

First known affected version: **it is happing in all versions

As seen in the bug ticket and Crashlytics, we are encountering multiple errors where the events' database is locked. Upon reviewing the code, I found that we are not adhering to Room's documentation guidelines. Specifically, As SID is creating multiple instances of the EventRoomDatabase object, whereas the documentation states that the database instance should be shared. Using the Android Profiler, I observed that multiple database objects are being created during the biometric capture workflow and when opening the app dashboard.

Notable changes

  • Change EventDatabaseFactory to use get() instead of build() to retrieve the EventRoomDatabase instance, ensuring that the database is only built once.
  • Replaced deleteDatabase() and recreateDatabaseKey() with single method recreateDatabase()
  • Update the EventLocalDataSource to use get() for database retrieval.
  • Improve recreateDatabase() functionality in EventDatabaseFactory
  • Update the test suit accordingly.

Testing guidance

  • Every part of the app, including sync and biometric capture, is affected. A quick test is recommended.

Additional work checklist

  • Effect on other features and security has been considered
  • Design document marked as "In development" (if applicable)
  • External (Gitbook) and internal (Confluence) Documentation is up to date (or ticket created)
  • Test cases in Testiny are up to date (or ticket created)
  • Other teams notified about the changes (if applicable)

@@ -82,16 +84,10 @@ internal open class EventLocalDataSource @Inject constructor(
ex.let { it as? SQLiteException }?.message?.contains("file is not a database") == true

private suspend fun rebuildDatabase(ex: Throwable) = mutex.withLock {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this function to recreateDatabase would make it match the one in EventDatabaseFactory and more appropriately convey what it's doing. (Yes, I was the one to name it...)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

coEvery { securityManager.getLocalDbKeyOrThrow(dbName) } returns localDbKey
mockkObject(EventRoomDatabase)
val db: EventRoomDatabase = mockk()
every { EventRoomDatabase.getDatabase(context, any(), dbName) } returns db
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this return different mocks to match actual behaviour and ensure we are not calling getDatabase() multiple times?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the test case as it was ambiguous. The new name is get should return the same db instance on multiple calls and it now verifies that the db is created only once

@meladRaouf meladRaouf force-pushed the bugfix/MS-936-db-locking branch 2 times, most recently from 1dadcf8 to fa202a6 Compare March 12, 2025 17:28
@meladRaouf meladRaouf force-pushed the bugfix/MS-936-db-locking branch from fa202a6 to 4e4fffb Compare March 12, 2025 17:30
@sonarqubecloud
Copy link
Copy Markdown

@meladRaouf meladRaouf requested a review from BurningAXE March 18, 2025 09:06
@meladRaouf meladRaouf merged commit 31a5edd into main Mar 19, 2025
@meladRaouf meladRaouf deleted the bugfix/MS-936-db-locking branch March 19, 2025 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants