android 資料庫是否該關閉

來源:互聯網
上載者:User

android 資料庫是否該關閉

關於android多線程資料讀寫請看部落格:

android 多線程資料庫讀寫

常常糾結於擷取了SQLiteDatabase每次操作完是否要關閉的問題,每次關閉又怕影響效能,這裡記錄下SQLiteOpenHelper操作邏輯:

SQLiteOpenHelper擷取getWritableDatabase,getReadableDatabase如果之前擷取的SQLiteDatabase對象沒有close掉,就會直接使用之前已經產生的SQLiteDatabase對象,如果之前已經close了,則會產生一個新的對象!

每次操作都關閉資料庫結果,有圖有真相:



列印出來的資訊是db的hashcode,如果兩個一樣,說明是同一個對象,作業碼:

<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">@Overridepublic SQLiteDatabase getWritableDatabase() {SQLiteDatabase db= super.getWritableDatabase();Log.e("ddddBBBB","getWritableDatabase狀態:"+db.hashCode()+",path:"+db.getPath());return db;}@Overridepublic SQLiteDatabase getReadableDatabase() {// TODO Auto-generated method stubSQLiteDatabase db= super.getReadableDatabase();Log.e("ddddBBBB","getReadableDatabase狀態:"+db.hashCode()+",path:"+db.getPath());return db;}

個人覺得沒必要每次操作玩都關閉db,有點消耗效能,不如弄個全域的DB,等application 結束時再關閉。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.