Skip to content

Commit dfecce3

Browse files
committed
Uses primary connection if available prior to using/creating secondary connections
1 parent f5d9335 commit dfecce3

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteConnectionPool.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,27 @@ private SQLiteConnection waitForConnection(String sql, int connectionFlags,
617617

618618
// Try to acquire a connection.
619619
SQLiteConnection connection = null;
620-
if (!wantPrimaryConnection) {
621-
connection = tryAcquireNonPrimaryConnectionLocked(
622-
sql, connectionFlags); // might throw
623-
}
624-
if (connection == null) {
625-
connection = tryAcquirePrimaryConnectionLocked(connectionFlags); // might throw
620+
621+
// When in WAL mode, we want to avoid the startup penalty of creating both a primary and
622+
// non-primary connection for initial read operations on the database. If the pool of non-primary
623+
// connections has not been initialized yet, and the primary connection is available, use the primary.
624+
// If the primary connection is required, always use that.
625+
if ((mAvailablePrimaryConnection != null && mAvailableNonPrimaryConnections.isEmpty()) || wantPrimaryConnection) {
626+
connection = tryAcquirePrimaryConnectionLocked(connectionFlags);
627+
if (connection != null) {
628+
return connection;
629+
}
626630
}
627-
if (connection != null) {
628-
return connection;
631+
632+
// If a primary connection is not required by the caller, and either the non-primary connection pool
633+
// has already been established or the primary connection is not available, then try to get
634+
// non primary connection. This may establish a new non-primary connection a free one is not
635+
// already available.
636+
if (!wantPrimaryConnection) {
637+
connection = tryAcquireNonPrimaryConnectionLocked(sql, connectionFlags);
638+
if (connection != null) {
639+
return connection;
640+
}
629641
}
630642

631643
// No connections available. Enqueue a waiter in priority order.

0 commit comments

Comments
 (0)