Android 數(shù)據(jù)存儲(chǔ)知識(shí)梳理(1) - SQLiteOpenHelper 源碼解析

一、概述

這篇文章主要涉及到項(xiàng)目當(dāng)中,使用數(shù)據(jù)庫(kù)相關(guān)的操作:

  • 使用SQLiteOpenHelper來封裝數(shù)據(jù)庫(kù)。
  • 多線程的情況下使用SQLiteOpenHelper

二、使用SQLiteOpenHelper封裝數(shù)據(jù)庫(kù)

2.1 使用SQLiteOpenHelper的原因

之所以需要使用SQLiteOpenHelper,而不是調(diào)用Context的方法來直接得到SQLiteDatabase,主要是因?yàn)樗袃蓚€(gè)好處:

  • 自動(dòng)管理創(chuàng)建:當(dāng)需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作的時(shí)候,不用關(guān)心SQLiteOpenHelper所關(guān)聯(lián)的SQLiteDatabase是否創(chuàng)建,SQLiteOpenHelper會(huì)幫我們?nèi)ヅ袛啵绻麤]有創(chuàng)建,那么就先創(chuàng)建該數(shù)據(jù)庫(kù)后,再返回給使用者。
  • 自動(dòng)管理版本:當(dāng)需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作之前,如果發(fā)現(xiàn)當(dāng)前聲明的數(shù)據(jù)庫(kù)的版本和手機(jī)內(nèi)的數(shù)據(jù)庫(kù)版本不同的時(shí)候,那么會(huì)分別調(diào)用onUpgradeonDowngrade,這樣使用者就可以在里面來處理新舊版本的兼容問題。

2.2 SQLiteOpenHelperAPI

SQLiteOpenHelperAPI很少,我們來看一下:

2.2.1 構(gòu)造函數(shù)

    /**
     * Create a helper object to create, open, and/or manage a database.
     * The database is not actually created or opened until one of
     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
     *
     * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
     * used to handle corruption when sqlite reports database corruption.</p>
     *
     * @param context to use to open or create the database
     * @param name of the database file, or null for an in-memory database
     * @param factory to use for creating cursor objects, or null for the default
     * @param version number of the database (starting at 1); if the database is older,
     *     {@link #onUpgrade} will be used to upgrade the database; if the database is
     *     newer, {@link #onDowngrade} will be used to downgrade the database
     * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database
     * corruption, or null to use the default error handler.
     */
    public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
            DatabaseErrorHandler errorHandler) {
        if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);

        mContext = context;
        mName = name;
        mFactory = factory;
        mNewVersion = version;
        mErrorHandler = errorHandler;
    }

這里有一點(diǎn)很重要:當(dāng)我們實(shí)例化一個(gè)SQLiteOpenHelper的子類時(shí),并不會(huì)立刻創(chuàng)建或者打開它對(duì)應(yīng)的數(shù)據(jù)庫(kù),這個(gè)操作是等到調(diào)用了getWritableDatabase或者getReadableDatabase才進(jìn)行的

  • context:用來打開或者關(guān)閉數(shù)據(jù)的上下文,需要注意內(nèi)存泄露問題。
  • name:數(shù)據(jù)庫(kù)的名字,一般為xxx.db,如果為空,那么使用的是內(nèi)存數(shù)據(jù)庫(kù)。
  • factory:創(chuàng)建cursor的工廠類,如果為空,那么使用默認(rèn)的。
  • version:數(shù)據(jù)庫(kù)的當(dāng)前版本號(hào),必須大于等于1
  • erroeHandler:數(shù)據(jù)庫(kù)發(fā)生錯(cuò)誤時(shí)的處理者,如果為空,那么使用默認(rèn)處理方式。

2.2.2 獲得SQLiteDatabase

一般情況下,當(dāng)我們實(shí)例完一個(gè)SQLiteOpenHelper對(duì)象之后,就可以通過它所關(guān)聯(lián)的SQLiteDatabase,來對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作了,獲得數(shù)據(jù)庫(kù)的方式有下面兩種:

    /**
     * Create and/or open a database that will be used for reading and writing.
     * The first time this is called, the database will be opened and
     * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be
     * called.
     *
     * <p>Once opened successfully, the database is cached, so you can
     * call this method every time you need to write to the database.
     * (Make sure to call {@link #close} when you no longer need the database.)
     * Errors such as bad permissions or a full disk may cause this method
     * to fail, but future attempts may succeed if the problem is fixed.</p>
     *
     * <p class="caution">Database upgrade may take a long time, you
     * should not call this method from the application main thread, including
     * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
     *
     * @throws SQLiteException if the database cannot be opened for writing
     * @return a read/write database object valid until {@link #close} is called
     */
    public SQLiteDatabase getWritableDatabase() {
        synchronized (this) {
            return getDatabaseLocked(true);
        }
    }

    /**
     * Create and/or open a database.  This will be the same object returned by
     * {@link #getWritableDatabase} unless some problem, such as a full disk,
     * requires the database to be opened read-only.  In that case, a read-only
     * database object will be returned.  If the problem is fixed, a future call
     * to {@link #getWritableDatabase} may succeed, in which case the read-only
     * database object will be closed and the read/write object will be returned
     * in the future.
     *
     * <p class="caution">Like {@link #getWritableDatabase}, this method may
     * take a long time to return, so you should not call it from the
     * application main thread, including from
     * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
     *
     * @throws SQLiteException if the database cannot be opened
     * @return a database object valid until {@link #getWritableDatabase}
     *     or {@link #close} is called.
     */
    public SQLiteDatabase c() {
        synchronized (this) {
            return getDatabaseLocked(false);
        }
    }

注意到,它們最終都是調(diào)用了同一個(gè)方法,并且在該方法上加上了同步代碼塊。

關(guān)于getWritableDatabase,源碼當(dāng)中提到了以下幾點(diǎn):

  • 該方法是用來創(chuàng)建或者打開一個(gè)可讀寫的數(shù)據(jù)庫(kù),當(dāng)這個(gè)方法第一次被調(diào)用時(shí),數(shù)據(jù)庫(kù)會(huì)被打開,并且onCreateonUpgrade或者onOpen方法可能會(huì)被調(diào)用。
  • 一旦打開成功之后,這個(gè)數(shù)據(jù)庫(kù)會(huì)被緩存,也就是其中的mDatabase成員變量,但是如果權(quán)限檢查失敗或者磁盤慢了,那么有可能會(huì)打開失敗。
  • Upgrade方法有時(shí)候可能會(huì)執(zhí)行耗時(shí)的操作,因此不要在主線程當(dāng)中調(diào)用這個(gè)方法,包括ContentProvideronCreate()方法

關(guān)于getWritableDatabase,有幾點(diǎn)說明:

  • 在除了某些特殊情況,它和getWritableDatabase返回的一樣,都是一個(gè)可讀寫的數(shù)據(jù)庫(kù),如果磁盤滿了,那么才有可能返回一個(gè)只讀的數(shù)據(jù)庫(kù)。
  • 如果當(dāng)前mDatabase是只讀的,但是之后又調(diào)用了一個(gè)getWritableDatabase方法并且成功地獲取到了可寫的數(shù)據(jù)庫(kù),那么原來的mDatabase會(huì)被關(guān)閉,重新打開一個(gè)可讀寫的數(shù)據(jù)庫(kù),調(diào)用db.reopenReadWrite()方法。

下面,我們來看一下getDatabaseLocked的具體實(shí)現(xiàn),來了解其中的細(xì)節(jié)問題:

    private SQLiteDatabase getDatabaseLocked(boolean writable) {
        if (mDatabase != null) {
            if (!mDatabase.isOpen()) {
                //如果使用者獲取了db對(duì)象,但不是通過SQLiteOpenHelper關(guān)閉它,那么下次調(diào)用的時(shí)候會(huì)返回null。
                mDatabase = null;
            } else if (!writable || !mDatabase.isReadOnly()) {
                //如果不要求可寫或者當(dāng)前緩存的數(shù)據(jù)庫(kù)已經(jīng)是可寫的了,那么直接返回.
                return mDatabase;
            }
        }

        if (mIsInitializing) {
            throw new IllegalStateException("getDatabase called recursively");
        }

        SQLiteDatabase db = mDatabase;
        try {
            mIsInitializing = true;
            //如果要求可寫,但是當(dāng)前緩存的是只讀的,那么嘗試關(guān)閉后再重新打開來獲取一個(gè)可寫的。
            if (db != null) {
                if (writable && db.isReadOnly()) {
                    db.reopenReadWrite();
                }
            //下面就是沒有緩存的情況.
            } else if (mName == null) {
                db = SQLiteDatabase.create(null);
            //這里就是我們第一次調(diào)用時(shí)候的情況.
            } else {
                try {
                    if (DEBUG_STRICT_READONLY && !writable) {
                        final String path = mContext.getDatabasePath(mName).getPath();
                        db = SQLiteDatabase.openDatabase(path, mFactory,
                                SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                    } else {
                        //以可寫的方式打開或者創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),注意這里有一個(gè)標(biāo)志位mEnableWriteAheadLogging,我們后面來解釋.
                        db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ?
                                Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
                                mFactory, mErrorHandler);
                    }
                } catch (SQLiteException ex) {
                    //如果發(fā)生異常,并且要求可寫的,那么直接拋出異常.
                    if (writable) {
                        throw ex;
                    }
                    Log.e(TAG, "Couldn't open " + mName
                            + " for writing (will try read-only):", ex);
                    //如果不要求可寫,那么嘗試調(diào)用只讀的方式來打開。
                    final String path = mContext.getDatabasePath(mName).getPath();
                    db = SQLiteDatabase.openDatabase(path, mFactory,
                            SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                }
            }
            //抽象方法,子類實(shí)現(xiàn)。
            onConfigure(db);
           
            final int version = db.getVersion();
            //如果新舊版本不想等,那么才會(huì)進(jìn)入下面的判斷.
            if (version != mNewVersion) {
                //當(dāng)前數(shù)據(jù)庫(kù)是只讀的,那么會(huì)拋出異常。
                if (db.isReadOnly()) {
                    throw new SQLiteException("Can't upgrade read-only database from version " +
                            db.getVersion() + " to " + mNewVersion + ": " + mName);
                }
                //開啟事務(wù),onCreate/onDowngrade/OnUpgrade只會(huì)調(diào)用其中一個(gè)。
                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db);
                    } else {
                        if (version > mNewVersion) {
                            onDowngrade(db, version, mNewVersion);
                        } else {
                            onUpgrade(db, version, mNewVersion);
                        }
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            }
            //數(shù)據(jù)庫(kù)打開完畢.
            onOpen(db);

            if (db.isReadOnly()) {
                Log.w(TAG, "Opened " + mName + " in read-only mode");
            }

            mDatabase = db;
            return db;
        } finally {
            mIsInitializing = false;
            if (db != null && db != mDatabase) {
                db.close();
            }
        }
    }

2.2.3 onConfig/onOpen

在上面獲取數(shù)據(jù)庫(kù)的過程中,有兩個(gè)方法:

    /**
     * Called when the database connection is being configured, to enable features
     * such as write-ahead logging or foreign key support.
     * <p>
     * This method is called before {@link #onCreate}, {@link #onUpgrade},
     * {@link #onDowngrade}, or {@link #onOpen} are called.  It should not modify
     * the database except to configure the database connection as required.
     * </p><p>
     * This method should only call methods that configure the parameters of the
     * database connection, such as {@link SQLiteDatabase#enableWriteAheadLogging}
     * {@link SQLiteDatabase#setForeignKeyConstraintsEnabled},
     * {@link SQLiteDatabase#setLocale}, {@link SQLiteDatabase#setMaximumSize},
     * or executing PRAGMA statements.
     * </p>
     *
     * @param db The database.
     */
    public void onConfigure(SQLiteDatabase db) {}
    /**
     * Called when the database has been opened.  The implementation
     * should check {@link SQLiteDatabase#isReadOnly} before updating the
     * database.
     * <p>
     * This method is called after the database connection has been configured
     * and after the database schema has been created, upgraded or downgraded as necessary.
     * If the database connection must be configured in some way before the schema
     * is created, upgraded, or downgraded, do it in {@link #onConfigure} instead.
     * </p>
     *
     * @param db The database.
     */
    public void onOpen(SQLiteDatabase db) {}
  • onConfigure:在onCreate/onUpgrade/onDowngrade調(diào)用之前,可以在它其中來配置數(shù)據(jù)庫(kù)連接的參數(shù),這時(shí)候數(shù)據(jù)庫(kù)已經(jīng)創(chuàng)建完成,但是表有可能還沒創(chuàng)建,或者不是最新的。
  • onOpen:在數(shù)據(jù)庫(kù)連接配置完成,并且數(shù)據(jù)庫(kù)表已經(jīng)更新到最新的,當(dāng)我們?cè)谶@里對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作時(shí),需要判斷它是否是只讀的。

2.2.4 onCreate/onUpgrade/onDowngrade

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    public abstract void onCreate(SQLiteDatabase db);

    /**
     * Called when the database needs to be upgraded. The implementation
     * should use this method to drop tables, add tables, or do anything else it
     * needs to upgrade to the new schema version.
     *
     * <p>
     * The SQLite ALTER TABLE documentation can be found
     * <a >here</a>. If you add new columns
     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
     * you can use ALTER TABLE to rename the old table, then create the new table and then
     * populate the new table with the contents of the old table.
     * </p><p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

    /**
     * Called when the database needs to be downgraded. This is strictly similar to
     * {@link #onUpgrade} method, but is called whenever current version is newer than requested one.
     * However, this method is not abstract, so it is not mandatory for a customer to
     * implement it. If not overridden, default implementation will reject downgrade and
     * throws SQLiteException
     *
     * <p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        throw new SQLiteException("Can't downgrade database from version " +
                oldVersion + " to " + newVersion);
    }
  • onCreate原有數(shù)據(jù)庫(kù)版本為0時(shí)調(diào)用,在里面我們進(jìn)行剪標(biāo)操作;而onUpgrade/onDowngrade則在不相等時(shí)調(diào)用,在里面我們對(duì)表的字段進(jìn)行更改。
  • onDowngrade的默認(rèn)實(shí)現(xiàn)是拋出異常。
  • onUpgrade沒有默認(rèn)實(shí)現(xiàn)。
  • 這三個(gè)操作都是放在事務(wù)當(dāng)中,如果發(fā)生了錯(cuò)誤,那么會(huì)回滾。

2.2.5 關(guān)閉

    /**
     * Close any open database object.
     */
    public synchronized void close() {
        if (mIsInitializing) throw new IllegalStateException("Closed during initialization");

        if (mDatabase != null && mDatabase.isOpen()) {
            mDatabase.close();
            mDatabase = null;
        }
    }

會(huì)關(guān)閉當(dāng)前緩存的數(shù)據(jù)庫(kù),并把清空mDatabase緩存,注意這個(gè)方法也被加上了對(duì)象鎖。

三、多線程情況下對(duì)SQLiterDBHelper的使用

  • 在多線程的情況下,每個(gè)線程對(duì)同一個(gè)SQLiterDBHelper實(shí)例進(jìn)行操作,并不會(huì)產(chǎn)生影響,因?yàn)閯倓偽覀兛吹剑讷@取和關(guān)閉數(shù)據(jù)庫(kù)的方法上,都加上了對(duì)象鎖,所以最終我們只是打開了一條到數(shù)據(jù)庫(kù)上的連接,這時(shí)候就轉(zhuǎn)變?yōu)槿ビ懻?code>SQLiteDatabase的增刪改查操作是否是線程安全的了。
  • 然而,如果每個(gè)線程獲取SQLiteDatabase時(shí),不是用的同一個(gè)SQLiterDBHelper,那么其實(shí)是打開了多個(gè)連接,假如通過這多個(gè)連接同數(shù)據(jù)庫(kù)的操作是沒有同步的話,那么就會(huì)出現(xiàn)問題。

下面,我們總結(jié)一下在多線程情況下,可能出現(xiàn)問題的幾種場(chǎng)景:

3.1 多線程情況下每個(gè)線程創(chuàng)建一個(gè)SQLiteOpenHelper,并且之前沒有創(chuàng)建過關(guān)聯(lián)的db

    /**
     * 多線程同時(shí)創(chuàng)建,每個(gè)線程持有一個(gè)SQLiteOpenHelper
     * @param view
     */
    public void multiOnCreate(View view) {
        int threadCount = 50;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    MultiThreadDBHelper dbHelper = new MultiThreadDBHelper(MainActivity.this);
                    SQLiteDatabase database = dbHelper.getWritableDatabase();
                    ContentValues contentValues = new ContentValues(1);
                    contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                    contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()));
                    database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                }
            };
            thread.start();
        }
    }

在上面這種情況下,由于多個(gè)線程的getWritableDatabase沒有進(jìn)行同步操作,并且這時(shí)候手機(jī)里面沒有對(duì)應(yīng)的數(shù)據(jù)庫(kù),那么就有可能出現(xiàn)下面的情況:

  • Thread#1調(diào)用getWritableDatabase,在其中獲取數(shù)據(jù)庫(kù)的版本號(hào)為0,因此它調(diào)用onCreate建表,建表完成。
  • Thread#1建表完成,但是還沒有來得及給數(shù)據(jù)庫(kù)設(shè)置版本號(hào)時(shí),Thread#2也調(diào)用了getWritableDatabase,在其中它獲取數(shù)據(jù)庫(kù)版本號(hào)也是0,因此也執(zhí)行了onCreate操作,那么這時(shí)候就會(huì)出現(xiàn)對(duì)一個(gè)數(shù)據(jù)庫(kù)多次建立同一張表的情況發(fā)生。

3.2 多線程情況下每個(gè)線程創(chuàng)建一個(gè)SQLiteOpenHelper,同時(shí)對(duì)關(guān)聯(lián)的db進(jìn)行寫入操作

    /**
     * 多個(gè)線程同時(shí)寫入,每個(gè)線程持有一個(gè)SQLiteOpenHelper
     * @param view
     */
    public void multiWriteUseMultiDBHelper(View view) {
        MultiThreadDBHelper init = new MultiThreadDBHelper(MainActivity.this);
        SQLiteDatabase database = init.getWritableDatabase();
        database.close();
        int threadCount = 10;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    MultiThreadDBHelper dbHelper = new MultiThreadDBHelper(MainActivity.this);
                    SQLiteDatabase database = dbHelper.getWritableDatabase();
                    for (int i = 0; i < 1000; i++) {
                        ContentValues contentValues = new ContentValues(1);
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()) + "_" + i);
                        database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                    }
                }
            };
            thread.start();
        }
    }

假如我們啟動(dòng)了多個(gè)線程,并且在每個(gè)線程中新建了SQLiteOpenHelper實(shí)例,那么當(dāng)它們調(diào)用各自的getWritableDatabase方法時(shí),其實(shí)是對(duì)手機(jī)中的db建立了多個(gè)數(shù)據(jù)庫(kù)連接,當(dāng)通過多個(gè)數(shù)據(jù)庫(kù)連接同時(shí)對(duì)db進(jìn)行寫入,那么會(huì)拋出下面的異常:


3.13.2我們就可以看出,在多線程的情況下,每個(gè)線程新建一個(gè)SQLiteOpenHelper會(huì)出現(xiàn)問題,因此,我們盡量把它設(shè)計(jì)為單例的模式,那么是不是多個(gè)線程持有同一個(gè)SQLiteOpenHelper實(shí)例就不會(huì)出現(xiàn)問題呢,其實(shí)并不然,我們看一下下面這些共用同一個(gè)SQLiteOpenHelper的情形。

3.3 多線程情況下所有線程共用一個(gè)SQLiteOpenHelper,其中一個(gè)線程調(diào)用了close方法

    /**
     * 多線程下共用一個(gè)SQLiteOpenHelper
     * @param view
     */
    public void multiCloseUseOneDBHelper(View view) {
        final MultiThreadDBHelper init = new MultiThreadDBHelper(MainActivity.this);
        final SQLiteDatabase database = init.getWritableDatabase();
        database.close();
        Thread thread1 = new Thread() {

            @Override
            public void run() {
                SQLiteDatabase database = init.getWritableDatabase();
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    Log.e("MainActivity", "e=" + e);
                }
                ContentValues contentValues = new Conten;
                contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()));
                //由于Thread2已經(jīng)關(guān)閉了數(shù)據(jù)庫(kù),因此這里再調(diào)用插入操作就會(huì)出現(xiàn)問題。
                database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
            }
        };
        thread1.start();
        Thread thread2 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    Log.e("MainActivity", "e=" + e);
                }
                init.close();
            }
        };
        thread2.start();
    }

3.4 多線程情況下所有線程共用一個(gè)SQLiteOpenHelper,在寫的過程中同時(shí)讀

由于是共用了同一個(gè)SQLiteOpenHelper,因此我們需要考慮的是對(duì)于同一個(gè)SQLiteDatabase連接,是否允許讀寫并發(fā),默認(rèn)情況下是不允許的,但是,我們可以通過SQLiteOpenHelper#setWriteAheadLoggingEnabled,這個(gè)配置默認(rèn)是關(guān)的,當(dāng)開啟時(shí)表示:它允許一個(gè)寫線程與多個(gè)讀線程同時(shí)在一個(gè)SQLiteDatabase上起作用。實(shí)現(xiàn)原理是寫操作其實(shí)是在一個(gè)單獨(dú)的文件,不是原數(shù)據(jù)庫(kù)文件。所以寫在執(zhí)行時(shí),不會(huì)影響讀操作,讀操作讀的是原數(shù)據(jù)文件,是寫操作開始之前的內(nèi)容。在寫操作執(zhí)行成功后,會(huì)把修改合并會(huì)原數(shù)據(jù)庫(kù)文件。此時(shí)讀操作才能讀到修改后的內(nèi)容。但是這樣將花費(fèi)更多的內(nèi)存。

四、解決多線程的例子

工廠類負(fù)責(zé)根據(jù)dbName創(chuàng)建對(duì)應(yīng)的SQLiteOpenHelper

public abstract class DBHelperFactory {
    public abstract SQLiteOpenHelper createDBHelper(String dbName);
}

通過管理類來插入指定數(shù)據(jù)庫(kù)指定表

public class DBHelperManager {

    private HashMap<String, SQLiteOpenHelperWrapper> mDBHelperWrappers;
    private DBHelperFactory mDBHelperFactory;

    static class Nested {
        public static DBHelperManager sInstance = new DBHelperManager();
    }

    public static DBHelperManager getInstance() {
        return Nested.sInstance;
    }

    private DBHelperManager() {
        mDBHelperWrappers = new HashMap<>();
    }

    public void setDBHelperFactory(DBHelperFactory dbHelperFactory) {
        mDBHelperFactory = dbHelperFactory;
    }

    private synchronized SQLiteOpenHelperWrapper getSQLiteDBHelperWrapper(String dbName) {
        SQLiteOpenHelperWrapper wrapper = mDBHelperWrappers.get(dbName);
        if (wrapper == null) {
            if (mDBHelperFactory != null) {
                SQLiteOpenHelper dbHelper = mDBHelperFactory.createDBHelper(dbName);
                if (dbHelper != null) {
                    SQLiteOpenHelperWrapper newWrapper = new SQLiteOpenHelperWrapper();
                    newWrapper.mSQLiteOpenHelper = dbHelper;
                    newWrapper.mSQLiteOpenHelper.setWriteAheadLoggingEnabled(true);
                    mDBHelperWrappers.put(dbName, newWrapper);
                    wrapper = newWrapper;
                }
            }
        }
        return wrapper;
    }

    private synchronized SQLiteDatabase getReadableDatabase(String dbName) {
        SQLiteOpenHelperWrapper wrapper = getSQLiteDBHelperWrapper(dbName);
        if (wrapper != null && wrapper.mSQLiteOpenHelper != null) {
            return wrapper.mSQLiteOpenHelper.getReadableDatabase();
        } else {
            return null;
        }
    }

    private synchronized SQLiteDatabase getWritableDatabase(String dbName) {
        SQLiteOpenHelperWrapper wrapper = getSQLiteDBHelperWrapper(dbName);
        if (wrapper != null && wrapper.mSQLiteOpenHelper != null) {
            return wrapper.mSQLiteOpenHelper.getWritableDatabase();
        } else {
            return null;
        }
    }

    private class SQLiteOpenHelperWrapper {
        public SQLiteOpenHelper mSQLiteOpenHelper;
    }

    public long insert(String dbName, String tableName, String nullColumn, ContentValues contentValues) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.insert(tableName, nullColumn, contentValues);
        }
        return -1;
    }

    public Cursor query(String dbName, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        SQLiteDatabase db = getReadableDatabase(dbName);
        if (db != null) {
            return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
        }
        return null;
    }

    public int update(String dbName, String table, ContentValues values, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.update(table, values, whereClause, whereArgs);
        }
        return 0;
    }

    public int delete(String dbName, String table, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.delete(table, whereClause, whereArgs);
        }
        return 0;
    }

}

多線程插入的方式改為下面這樣:

    public void multiWriteUseManager(View view) {
        int threadCount = 10;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        ContentValues contentValues = new ContentValues(1);
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()) + "_" + i);
                        DBHelperManager.getInstance().insert(MultiThreadDBContract.DATABASE_NAME, MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                    }
                }
            };
            thread.start();
        }
    }

五、小結(jié)

這篇文章主要介紹的是SQLiteOpenHelper,需要注意以下三點(diǎn):

  • 不要在多線程情況且沒有進(jìn)行線程同步的情況下,操作由不同的SQLiteOpenHelper對(duì)象所返回的SQLiteDatabase
  • 在多線程共用一個(gè)SQLiteOpenHelper時(shí),需要注意關(guān)閉時(shí),是否有其它線程正在使用該Helper所關(guān)聯(lián)的db
  • 在多線程共用一個(gè)SQLiteOpenHelper時(shí),是否有同時(shí)讀寫的需求,如果有,那么需要設(shè)置setWriteAheadLoggingEnabled標(biāo)志位。

對(duì)于SQLiteDatabase,還有更多的優(yōu)化操作,當(dāng)我們有關(guān)數(shù)據(jù)庫(kù)的錯(cuò)誤時(shí),我們都可以根據(jù)錯(cuò)誤碼,在下面的網(wǎng)站當(dāng)中找到說明:

https://www.sqlite.org/rescode.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容