Android Database 并行访问
来源:互联网 发布:三步倒在淘宝怎么买 编辑:程序博客网 时间:2024/04/28 07:57
[转] --- http://blog.csdn.net/droid_zhlu/article/details/22194437
1、假设你已经有了自己的SQLiteOpenHelper实例
- public class DatabaseHelper extends SQLiteOpenHelper { ... }
2、现在你需要在不同的线程中写数据库
- // Thread 1
- Context context = getApplicationContext();
- DatabaseHelper helper = new DatabaseHelper(context);
- SQLiteDatabase database = helper.getWritableDatabase();
- database.insert(…);
- database.close();
- // Thread 2
- Context context = getApplicationContext();
- DatabaseHelper helper = new DatabaseHelper(context);
- SQLiteDatabase database = helper.getWritableDatabase();
- database.insert(…);
- database.close();
执行后,得到错误
- android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
错误的原因:每次new SQLiteOpenHelper的时候,实质上是创建一个Database Connection。如果在不同的Connection同时写数据库,其中一个就会Failed。
如果在多个线程中使用数据库,需要确保使用同一个Database Connection。
3、为了解决第二个问题,我们去创建一个DatabaseManager类,通过单例去获取SQLiteOpenHelper的对象。
- public class DatabaseManager {
- private static DatabaseManager instance;
- private static SQLiteOpenHelper mDatabaseHelper;
- public static synchronized void initialize(Context context, SQLiteOpenHelper helper) {
- if (instance == null) {
- instance = new DatabaseManager();
- mDatabaseHelper = helper;
- }
- }
- public static synchronized DatabaseManager getInstance() {
- if (instance == null) {
- throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
- " is not initialized, call initialize(..) method first.");
- }
- return instance;
- }
- public synchronized SQLiteDatabase getDatabase() {
- return new mDatabaseHelper.getWritableDatabase();
- }
- }
- // In your application class
- DatabaseManager.initializeInstance(getApplicationContext());
- // Thread 1
- DatabaseManager manager = DatabaseManager.getInstance();
- SQLiteDatabase database = manager.getDatabase()
- database.insert(…);
- database.close();
- // Thread 2
- DatabaseManager manager = DatabaseManager.getInstance();
- SQLiteDatabase database = manager.getDatabase()
- database.insert(…);
- database.close();
- java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase
因为我们使用同一个Database Connection,所以Thread1和Thread2使用的是同一个实例。现在Thread1关闭了Connection,但是Thread2却仍然在用。
所以,我们在关闭Connection之前需要确保没有线程在使用这个Connection。一个解决方案是永远的不去关闭Connection,但是在Logcat里面会出现如下的消息:
- Leak found
- Caused by: java.lang.IllegalStateException: SQLiteDatabase created and never closed
5、我们可以使用AtomicInteger处理这种并发操作
- public class DatabaseManager {
- private AtomicInteger mOpenCounter = new AtomicInteger();
- private static DatabaseManager instance;
- private static SQLiteOpenHelper mDatabaseHelper;
- private SQLiteDatabase mDatabase;
- public static synchronized void initializeInstance(SQLiteOpenHelper helper) {
- if (instance == null) {
- instance = new DatabaseManager();
- mDatabaseHelper = helper;
- }
- }
- public static synchronized DatabaseManager getInstance() {
- if (instance == null) {
- throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
- " is not initialized, call initializeInstance(..) method first.");
- }
- return instance;
- }
- public synchronized SQLiteDatabase openDatabase() {
- if(mOpenCounter.incrementAndGet() == 1) {
- // Opening new database
- mDatabase = mDatabaseHelper.getWritableDatabase();
- }
- return mDatabase;
- }
- public synchronized void closeDatabase() {
- if(mOpenCounter.decrementAndGet() == 0) {
- // Closing database
- mDatabase.close();
- }
- }
- }
- SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();
- database.insert(...);
- // database.close(); Don't close it directly!
- DatabaseManager.getInstance().closeDatabase(); // correct way
0 0
- Android Database 并行访问
- Android Database 并行访问
- Android Database 并行访问
- Android Database 并行访问 SQLiteDatabaseLockedException: database is locked 解决方案
- Database: android.database.CursorIndexOutOfBoundsException
- Android-database
- 关于并行开发的访问
- Windows Phone 数据库并行访问
- 硬盘可以并行访问吗
- Opencv并行访问图像像素
- java 访问 microsoft access database
- PgAdmin3访问远程server database
- [Android Develop_003]-Android Database
- #android training# android.database
- Android并行开发
- android.database.sqlite.SQLiteException: database is locked
- android --music database-1
- android --music database-2
- DLL文件 VC++&Matlab混合编程( 调用DLL)
- Unable to chmod ***: Read-only file system问题的解决方法
- c++ printf参数练习
- magento 1.7.0.2 通过mysql触发器实现用户评论立即显示在商品页上
- iOS—应用内跳转到AppStore进行评分
- Android Database 并行访问
- 带你走近AngularJS - 基本功能介绍
- solrcolud
- linux iptables
- *项目开发——每日总结
- 自定义 UITableViewCell 怎样在子控件触发时得到 indexPath
- 【翻译】计算机几何基础算法(三)
- 【linux】提醒"libc.so.6: version `GLIBC_2.14' not found"系统的glibc版本太低
- solrcloud结果