@@ -81,6 +81,7 @@ public final class SQLiteConnectionPool implements Closeable {
8181 private final Object mLock = new Object ();
8282 private final AtomicBoolean mConnectionLeaked = new AtomicBoolean ();
8383 private final SQLiteDatabaseConfiguration mConfiguration ;
84+ private int mMaxConnectionPoolSize ;
8485 private boolean mIsOpen ;
8586 private int mNextConnectionId ;
8687
@@ -146,6 +147,7 @@ enum AcquiredConnectionStatus {
146147
147148 private SQLiteConnectionPool (SQLiteDatabaseConfiguration configuration ) {
148149 mConfiguration = new SQLiteDatabaseConfiguration (configuration );
150+ setMaxConnectionPoolSizeLocked ();
149151 }
150152
151153 @ Override
@@ -257,8 +259,9 @@ public void reconfigure(SQLiteDatabaseConfiguration configuration) {
257259 synchronized (mLock ) {
258260 throwIfClosedLocked ();
259261
260- boolean restrictToOneConnection = false ;
261- if (mConfiguration .walEnabled != configuration .walEnabled ) {
262+ boolean walModeChanged = ((configuration .openFlags ^ mConfiguration .openFlags )
263+ & SQLiteDatabase .ENABLE_WRITE_AHEAD_LOGGING ) != 0 ;
264+ if (walModeChanged ) {
262265 // WAL mode can only be changed if there are no acquired connections
263266 // because we need to close all but the primary connection first.
264267 if (!mAcquiredConnections .isEmpty ()) {
@@ -272,15 +275,13 @@ public void reconfigure(SQLiteDatabaseConfiguration configuration) {
272275 // because none of them are in use.
273276 closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked ();
274277 assert mAvailableNonPrimaryConnections .isEmpty ();
275-
276- restrictToOneConnection = true ;
277278 }
278279
279280 if (mConfiguration .openFlags != configuration .openFlags ) {
280281 // If we are changing open flags and WAL mode at the same time, then
281282 // we have no choice but to close the primary connection beforehand
282283 // because there can only be one connection open when we change WAL mode.
283- if (restrictToOneConnection ) {
284+ if (walModeChanged ) {
284285 closeAvailableConnectionsAndLogExceptionsLocked ();
285286 }
286287
@@ -296,9 +297,11 @@ public void reconfigure(SQLiteDatabaseConfiguration configuration) {
296297
297298 mAvailablePrimaryConnection = newPrimaryConnection ;
298299 mConfiguration .updateParametersFrom (configuration );
300+ setMaxConnectionPoolSizeLocked ();
299301 } else {
300302 // Reconfigure the database connections in place.
301303 mConfiguration .updateParametersFrom (configuration );
304+ setMaxConnectionPoolSizeLocked ();
302305
303306 closeExcessConnectionsAndLogExceptionsLocked ();
304307 reconfigureAllConnectionsLocked ();
@@ -360,8 +363,7 @@ public void releaseConnection(SQLiteConnection connection) {
360363 mAvailablePrimaryConnection = connection ;
361364 }
362365 wakeConnectionWaitersLocked ();
363- } else if (mAvailableNonPrimaryConnections .size () >=
364- mConfiguration .maxConnectionPoolSize - 1 ) {
366+ } else if (mAvailableNonPrimaryConnections .size () >= mMaxConnectionPoolSize - 1 ) {
365367 closeConnectionAndLogExceptionsLocked (connection );
366368 } else {
367369 if (recycleConnectionLocked (connection , status )) {
@@ -499,7 +501,7 @@ private void closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked() {
499501 // Can't throw.
500502 private void closeExcessConnectionsAndLogExceptionsLocked () {
501503 int availableCount = mAvailableNonPrimaryConnections .size ();
502- while (availableCount -- > mConfiguration . maxConnectionPoolSize - 1 ) {
504+ while (availableCount -- > mMaxConnectionPoolSize - 1 ) {
503505 SQLiteConnection connection =
504506 mAvailableNonPrimaryConnections .remove (availableCount );
505507 closeConnectionAndLogExceptionsLocked (connection );
@@ -874,7 +876,7 @@ private SQLiteConnection tryAcquireNonPrimaryConnectionLocked(
874876 if (mAvailablePrimaryConnection != null ) {
875877 openConnections += 1 ;
876878 }
877- if (openConnections >= mConfiguration . maxConnectionPoolSize ) {
879+ if (openConnections >= mMaxConnectionPoolSize ) {
878880 return null ;
879881 }
880882 connection = openConnectionLocked (mConfiguration ,
@@ -926,6 +928,18 @@ private static int getPriority(int connectionFlags) {
926928 return (connectionFlags & CONNECTION_FLAG_INTERACTIVE ) != 0 ? 1 : 0 ;
927929 }
928930
931+ private void setMaxConnectionPoolSizeLocked () {
932+ if ((mConfiguration .openFlags & SQLiteDatabase .ENABLE_WRITE_AHEAD_LOGGING ) != 0 ) {
933+ mMaxConnectionPoolSize = SQLiteGlobal .getWALConnectionPoolSize ();
934+ } else {
935+ // TODO: We don't actually need to restrict the connection pool size to 1
936+ // for non-WAL databases. There might be reasons to use connection pooling
937+ // with other journal modes. For now, enabling connection pooling and
938+ // using WAL are the same thing in the API.
939+ mMaxConnectionPoolSize = 1 ;
940+ }
941+ }
942+
929943 private void throwIfClosedLocked () {
930944 if (!mIsOpen ) {
931945 throw new IllegalStateException ("Cannot perform this operation "
@@ -972,7 +986,7 @@ public void dump(Printer printer, boolean verbose) {
972986 synchronized (mLock ) {
973987 printer .println ("Connection pool for " + mConfiguration .path + ":" );
974988 printer .println (" Open: " + mIsOpen );
975- printer .println (" Max connections: " + mConfiguration . maxConnectionPoolSize );
989+ printer .println (" Max connections: " + mMaxConnectionPoolSize );
976990
977991 printer .println (" Available primary connection:" );
978992 if (mAvailablePrimaryConnection != null ) {
0 commit comments