Ejemplo n.º 1
0
 public void enableLocalizedCollators() {
   synchronized (mLock) {
     if (!mAcquiredConnections.isEmpty() || mAvailablePrimaryConnection == null) {
       throw new IllegalStateException(
           "Cannot enable localized collators while database is in use");
     }
     mAvailablePrimaryConnection.enableLocalizedCollators();
   }
 }
Ejemplo n.º 2
0
 public static void unbindFromService(final ServiceToken token) {
   if (token == null) {
     return;
   }
   final ContextWrapper mContextWrapper = token.mWrappedContext;
   final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper);
   if (mBinder == null) {
     return;
   }
   mContextWrapper.unbindService(mBinder);
   if (mConnectionMap.isEmpty()) {
     mService = null;
   }
 }
Ejemplo n.º 3
0
 // Can't throw.
 private void markAcquiredConnectionsLocked(AcquiredConnectionStatus status) {
   if (!mAcquiredConnections.isEmpty()) {
     ArrayList<SQLiteConnection> keysToUpdate =
         new ArrayList<SQLiteConnection>(mAcquiredConnections.size());
     for (Map.Entry<SQLiteConnection, AcquiredConnectionStatus> entry :
         mAcquiredConnections.entrySet()) {
       AcquiredConnectionStatus oldStatus = entry.getValue();
       if (status != oldStatus && oldStatus != AcquiredConnectionStatus.DISCARD) {
         keysToUpdate.add(entry.getKey());
       }
     }
     final int updateCount = keysToUpdate.size();
     for (int i = 0; i < updateCount; i++) {
       mAcquiredConnections.put(keysToUpdate.get(i), status);
     }
   }
 }
Ejemplo n.º 4
0
  // Can't throw.
  private void logConnectionPoolBusyLocked(long waitMillis, int connectionFlags) {
    final Thread thread = Thread.currentThread();
    StringBuilder msg = new StringBuilder();
    msg.append("The connection pool for database '").append(mConfiguration.label);
    msg.append("' has been unable to grant a connection to thread ");
    msg.append(thread.getId()).append(" (").append(thread.getName()).append(") ");
    msg.append("with flags 0x").append(Integer.toHexString(connectionFlags));
    msg.append(" for ").append(waitMillis * 0.001f).append(" seconds.\n");

    ArrayList<String> requests = new ArrayList<String>();
    int activeConnections = 0;
    int idleConnections = 0;
    if (!mAcquiredConnections.isEmpty()) {
      for (SQLiteConnection connection : mAcquiredConnections.keySet()) {
        String description = connection.describeCurrentOperationUnsafe();
        if (description != null) {
          requests.add(description);
          activeConnections += 1;
        } else {
          idleConnections += 1;
        }
      }
    }
    int availableConnections = mAvailableNonPrimaryConnections.size();
    if (mAvailablePrimaryConnection != null) {
      availableConnections += 1;
    }

    msg.append("Connections: ").append(activeConnections).append(" active, ");
    msg.append(idleConnections).append(" idle, ");
    msg.append(availableConnections).append(" available.\n");

    if (!requests.isEmpty()) {
      msg.append("\nRequests in progress:\n");
      for (String request : requests) {
        msg.append("  ").append(request).append("\n");
      }
    }

    Log.w(TAG, msg.toString());
  }
 public boolean isEmpty() {
   return myToExpand.isEmpty() && myToSelect.isEmpty() && myAdjustedSelection.isEmpty();
 }
Ejemplo n.º 6
0
  /**
   * Reconfigures the database configuration of the connection pool and all of its connections.
   *
   * <p>Configuration changes are propagated down to connections immediately if they are available
   * or as soon as they are released. This includes changes that affect the size of the pool.
   *
   * @param configuration The new configuration.
   * @throws IllegalStateException if the pool has been closed.
   */
  public void reconfigure(SQLiteDatabaseConfiguration configuration) {
    if (configuration == null) {
      throw new IllegalArgumentException("configuration must not be null.");
    }

    synchronized (mLock) {
      throwIfClosedLocked();

      boolean walModeChanged =
          ((configuration.openFlags ^ mConfiguration.openFlags)
                  & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING)
              != 0;
      if (walModeChanged) {
        // WAL mode can only be changed if there are no acquired connections
        // because we need to close all but the primary connection first.
        if (!mAcquiredConnections.isEmpty()) {
          throw new IllegalStateException(
              "Write Ahead Logging (WAL) mode cannot "
                  + "be enabled or disabled while there are transactions in "
                  + "progress.  Finish all transactions and release all active "
                  + "database connections first.");
        }

        // Close all non-primary connections.  This should happen immediately
        // because none of them are in use.
        closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked();
        assert mAvailableNonPrimaryConnections.isEmpty();
      }

      boolean foreignKeyModeChanged =
          configuration.foreignKeyConstraintsEnabled != mConfiguration.foreignKeyConstraintsEnabled;
      if (foreignKeyModeChanged) {
        // Foreign key constraints can only be changed if there are no transactions
        // in progress.  To make this clear, we throw an exception if there are
        // any acquired connections.
        if (!mAcquiredConnections.isEmpty()) {
          throw new IllegalStateException(
              "Foreign Key Constraints cannot "
                  + "be enabled or disabled while there are transactions in "
                  + "progress.  Finish all transactions and release all active "
                  + "database connections first.");
        }
      }

      if (mConfiguration.openFlags != configuration.openFlags) {
        // If we are changing open flags and WAL mode at the same time, then
        // we have no choice but to close the primary connection beforehand
        // because there can only be one connection open when we change WAL mode.
        if (walModeChanged) {
          closeAvailableConnectionsAndLogExceptionsLocked();
        }

        // Try to reopen the primary connection using the new open flags then
        // close and discard all existing connections.
        // This might throw if the database is corrupt or cannot be opened in
        // the new mode in which case existing connections will remain untouched.
        SQLiteConnection newPrimaryConnection =
            openConnectionLocked(configuration, true /*primaryConnection*/); // might throw

        closeAvailableConnectionsAndLogExceptionsLocked();
        discardAcquiredConnectionsLocked();

        mAvailablePrimaryConnection = newPrimaryConnection;
        mConfiguration.updateParametersFrom(configuration);
        setMaxConnectionPoolSizeLocked();
      } else {
        // Reconfigure the database connections in place.
        mConfiguration.updateParametersFrom(configuration);
        setMaxConnectionPoolSizeLocked();

        closeExcessConnectionsAndLogExceptionsLocked();
        reconfigureAllConnectionsLocked();
      }

      wakeConnectionWaitersLocked();
    }
  }