Skip to content

Obtaining sql connection blocks 4+ threads when many queries are executed in parallel #59

Closed
@Snow4DV

Description

@Snow4DV

Hello! I am currently working with sqlcipher 4.7.2 community edition

Currently room in android obtains sql connection in blocking way as seen here:

  override fun query(query: String): Cursor =
        session.useReaderBlocking { connection ->
            connection.usePrepared(query) { stmt -> stmt.toCursor() }
        }

source: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:room/room-sqlite-wrapper/src/main/kotlin/androidx/room/support/RoomSupportSQLiteDatabase.kt;l=41?q=roomsupportsqlitedatabase&sq=

in library only one connection can be created at the same time due to global mLock:

   private SQLiteConnection waitForConnection(String sql, int connectionFlags,
            CancellationSignal cancellationSignal) {
        final boolean wantPrimaryConnection =
                (connectionFlags & CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY) != 0;

        final ConnectionWaiter waiter;
        final int nonce;
        synchronized (mLock) {

That leads to whole default ArchTaskExecutor thread pool (that consists of 4 threads - used in room by default) being fully locked for 1+ second when first operations are executed

That is not good because in android it is usually a good practice to use coroutines that are not blocking. It also blocks default arch thread pool that is used by work manager workers, non-security room databases and etc

So the question is - is it possible to get rid of these locks or maybe speed up the connection creation somehow?

The other problem with this approach is that i can't use existing connections while another connection is created because releaseConnection locks the same lock as acquireConnection. Maybe it could be possible to execute queries/transactions on existing connection while creating one connection?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions