@@ -617,15 +617,27 @@ private SQLiteConnection waitForConnection(String sql, int connectionFlags,
617
617
618
618
// Try to acquire a connection.
619
619
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
+ }
626
630
}
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
+ }
629
641
}
630
642
631
643
// No connections available. Enqueue a waiter in priority order.
0 commit comments