Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62697e57 authored by Vasu Nori's avatar Vasu Nori Committed by Android (Google) Code Review
Browse files

Merge "close() on anything other than database shouldn't acquire db lock."

parents 3ba577e6 6f37f83a
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -95,13 +95,8 @@ import android.util.Log;
            if (SQLiteDebug.DEBUG_ACTIVE_CURSOR_FINALIZATION) {
                Log.v(TAG, "closed and deallocated DbObj (id#" + nStatement +")");
            }
            try {
                mDatabase.lock();
                native_finalize();
            mDatabase.finalizeStatementLater(nStatement);
            nStatement = 0;
            } finally {
                mDatabase.unlock();
            }
        }
    }

@@ -159,5 +154,4 @@ import android.util.Log;
     * @param sql The SQL to compile.
     */
    private final native void native_compile(String sql);
    private final native void native_finalize();
}
+59 −2
Original line number Diff line number Diff line
@@ -230,8 +230,8 @@ public class SQLiteDatabase extends SQLiteClosable {
    // lock acquistions of the database.
    /* package */ static final String GET_LOCK_LOG_PREFIX = "GETLOCK:";

    /** Used by native code, do not rename */
    /* package */ int mNativeHandle = 0;
    /** Used by native code, do not rename. make it volatile, so it is thread-safe. */
    /* package */ volatile int mNativeHandle = 0;

    /** Used to make temp table names unique */
    /* package */ int mTempTableSequence = 0;
@@ -313,6 +313,9 @@ public class SQLiteDatabase extends SQLiteClosable {
    private static final String LOG_SLOW_QUERIES_PROPERTY = "db.log.slow_query_threshold";
    private final int mSlowQueryThreshold;

    /** stores the list of statement ids that need to be finalized by sqlite */
    private ArrayList<Integer> mClosedStatementIds = new ArrayList<Integer>();

    /** {@link DatabaseErrorHandler} to be used when SQLite returns any of the following errors
     *    Corruption
     * */
@@ -1778,6 +1781,7 @@ public class SQLiteDatabase extends SQLiteClosable {
        }
        logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX);
        try {
            closePendingStatements();
            native_execSQL(sql);
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
@@ -2101,6 +2105,52 @@ public class SQLiteDatabase extends SQLiteClosable {
        mMaxSqlCacheSize = cacheSize;
    }

    /* package */ void finalizeStatementLater(int id) {
        if (!isOpen()) {
            // database already closed. this statement will already have been finalized.
            return;
        }
        synchronized(mClosedStatementIds) {
            if (mClosedStatementIds.contains(id)) {
                // this statement id is already queued up for finalization.
                return;
            }
            mClosedStatementIds.add(id);
        }
    }

    /**
     * public visibility only for testing. otherwise, package visibility is sufficient
     * @hide
     */
    public void closePendingStatements() {
        if (!isOpen()) {
            // since this database is already closed, no need to finalize anything.
            mClosedStatementIds.clear();
            return;
        }
        verifyLockOwner();
        /* to minimize synchronization on mClosedStatementIds, make a copy of the list */
        ArrayList<Integer> list = new ArrayList<Integer>(mClosedStatementIds.size());
        synchronized(mClosedStatementIds) {
            list.addAll(mClosedStatementIds);
            mClosedStatementIds.clear();
        }
        // finalize all the statements from the copied list
        int size = list.size();
        for (int i = 0; i < size; i++) {
            native_finalize(list.get(i));
        }
    }

    /**
     * for testing only
     * @hide
     */
    public ArrayList<Integer> getQueuedUpStmtList() {
        return mClosedStatementIds;
    }

    static class ActiveDatabases {
        private static final ActiveDatabases activeDatabases = new ActiveDatabases();
        private HashSet<WeakReference<SQLiteDatabase>> mActiveDatabases =
@@ -2310,4 +2360,11 @@ public class SQLiteDatabase extends SQLiteClosable {
     * @return int value of SQLITE_DBSTATUS_LOOKASIDE_USED
     */
    private native int native_getDbLookaside();

    /**
     * finalizes the given statement id.
     *
     * @param statementId statement to be finzlied by sqlite
     */
    private final native void native_finalize(int statementId);
}
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ public class SQLiteDirectCursorDriver implements SQLiteCursorDriver {

        try {
            mDatabase.lock();
            mDatabase.closePendingStatements();
            query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs);
            // Arg binding
            int numArgs = selectionArgs == null ? 0 : selectionArgs.length;
+1 −6
Original line number Diff line number Diff line
@@ -291,12 +291,7 @@ public abstract class SQLiteProgram extends SQLiteClosable {
        if (!mDatabase.isOpen()) {
            return;
        }
        mDatabase.lock();
        try {
        releaseReference();
        } finally {
            mDatabase.unlock();
        }
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ public class SQLiteStatement extends SQLiteProgram

        acquireReference();
        try {
            mDatabase.closePendingStatements();
            native_execute();
            mDatabase.logTimeStat(mSql, timeStart);
        } finally {
@@ -81,6 +82,7 @@ public class SQLiteStatement extends SQLiteProgram

        acquireReference();
        try {
            mDatabase.closePendingStatements();
            native_execute();
            mDatabase.logTimeStat(mSql, timeStart);
            return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1;
@@ -107,6 +109,7 @@ public class SQLiteStatement extends SQLiteProgram

        acquireReference();
        try {
            mDatabase.closePendingStatements();
            long retValue = native_1x1_long();
            mDatabase.logTimeStat(mSql, timeStart);
            return retValue;
@@ -133,6 +136,7 @@ public class SQLiteStatement extends SQLiteProgram

        acquireReference();
        try {
            mDatabase.closePendingStatements();
            String retValue = native_1x1_string();
            mDatabase.logTimeStat(mSql, timeStart);
            return retValue;
Loading