Android Database 并行访问

来源:互联网 发布:三步倒在淘宝怎么买 编辑:程序博客网 时间:2024/04/28 07:57

[转] --- http://blog.csdn.net/droid_zhlu/article/details/22194437


1、假设你已经有了自己的SQLiteOpenHelper实例

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class DatabaseHelper extends SQLiteOpenHelper { ... }  

2、现在你需要在不同的线程中写数据库

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. // Thread 1  
  2.  Context context = getApplicationContext();  
  3.  DatabaseHelper helper = new DatabaseHelper(context);  
  4.  SQLiteDatabase database = helper.getWritableDatabase();  
  5.  database.insert(…);  
  6.  database.close();  
  7.   
  8.  // Thread 2  
  9.  Context context = getApplicationContext();  
  10.  DatabaseHelper helper = new DatabaseHelper(context);  
  11.  SQLiteDatabase database = helper.getWritableDatabase();  
  12.  database.insert(…);  
  13.  database.close();  

执行后,得到错误

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)  

错误的原因:每次new SQLiteOpenHelper的时候,实质上是创建一个Database Connection。如果在不同的Connection同时写数据库,其中一个就会Failed。

如果在多个线程中使用数据库,需要确保使用同一个Database Connection。

3、为了解决第二个问题,我们去创建一个DatabaseManager类,通过单例去获取SQLiteOpenHelper的对象。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class DatabaseManager {  
  2.   
  3.     private static DatabaseManager instance;  
  4.     private static SQLiteOpenHelper mDatabaseHelper;  
  5.   
  6.     public static synchronized void initialize(Context context, SQLiteOpenHelper helper) {  
  7.         if (instance == null) {  
  8.             instance = new DatabaseManager();  
  9.             mDatabaseHelper = helper;  
  10.         }  
  11.     }  
  12.   
  13.     public static synchronized DatabaseManager getInstance() {  
  14.         if (instance == null) {  
  15.             throw new IllegalStateException(DatabaseManager.class.getSimpleName() +  
  16.                     " is not initialized, call initialize(..) method first.");  
  17.         }  
  18.   
  19.         return instance;  
  20.     }  
  21.   
  22.     public synchronized SQLiteDatabase getDatabase() {  
  23.         return new mDatabaseHelper.getWritableDatabase();  
  24.     }  
  25.   
  26. }  
4、那么现在第二步中的功能,应该这样去实现

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. // In your application class  
  2.  DatabaseManager.initializeInstance(getApplicationContext());  
  3.   
  4.  // Thread 1  
  5.  DatabaseManager manager = DatabaseManager.getInstance();  
  6.  SQLiteDatabase database = manager.getDatabase()  
  7.  database.insert(…);  
  8.  database.close();  
  9.   
  10.  // Thread 2  
  11.  DatabaseManager manager = DatabaseManager.getInstance();  
  12.  SQLiteDatabase database = manager.getDatabase()  
  13.  database.insert(…);  
  14.  database.close();  
执行后,得到错误

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase  

因为我们使用同一个Database Connection,所以Thread1和Thread2使用的是同一个实例。现在Thread1关闭了Connection,但是Thread2却仍然在用。

所以,我们在关闭Connection之前需要确保没有线程在使用这个Connection。一个解决方案是永远的不去关闭Connection,但是在Logcat里面会出现如下的消息:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Leak found  
  2. Caused by: java.lang.IllegalStateException: SQLiteDatabase created and never closed  

5、我们可以使用AtomicInteger处理这种并发操作

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class DatabaseManager {  
  2.   
  3.     private AtomicInteger mOpenCounter = new AtomicInteger();  
  4.   
  5.     private static DatabaseManager instance;  
  6.     private static SQLiteOpenHelper mDatabaseHelper;  
  7.     private SQLiteDatabase mDatabase;  
  8.   
  9.     public static synchronized void initializeInstance(SQLiteOpenHelper helper) {  
  10.         if (instance == null) {  
  11.             instance = new DatabaseManager();  
  12.             mDatabaseHelper = helper;  
  13.         }  
  14.     }  
  15.   
  16.     public static synchronized DatabaseManager getInstance() {  
  17.         if (instance == null) {  
  18.             throw new IllegalStateException(DatabaseManager.class.getSimpleName() +  
  19.                     " is not initialized, call initializeInstance(..) method first.");  
  20.         }  
  21.   
  22.         return instance;  
  23.     }  
  24.   
  25.     public synchronized SQLiteDatabase openDatabase() {  
  26.         if(mOpenCounter.incrementAndGet() == 1) {  
  27.             // Opening new database  
  28.             mDatabase = mDatabaseHelper.getWritableDatabase();  
  29.         }  
  30.         return mDatabase;  
  31.     }  
  32.   
  33.     public synchronized void closeDatabase() {  
  34.         if(mOpenCounter.decrementAndGet() == 0) {  
  35.             // Closing database  
  36.             mDatabase.close();  
  37.   
  38.         }  
  39.     }  
  40. }  
最后,我们这样使用就可以实现安全的数据库并发操作

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();  
  2. database.insert(...);  
  3. // database.close(); Don't close it directly!  
  4. DatabaseManager.getInstance().closeDatabase(); // correct way  



0 0