public class DatabaseHelper extends sqliteOpenHelper { ... }
现在你想要在不同的线程中向数据库写数据:
// 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();
上面的代码将使Logcat中出现如下信息,
并且one of your changes will not be written
android.database.sqlite.sqliteDatabaseLockedException: database is locked (code 5)
发生这种情况是因为你每次都创建了一个新的sqliteOpenHelper,实际上你每次都创建了一个数据库的连接。如果你在同一时间用不同的数据库连接来对同一的数据库进行写操作的话,那么其中一个会失败。
在Android平台上,如果你想在多线程环境下使用数据库,
public class DatabaseManager { private static DatabaseManager instance; private static sqliteOpenHelper mDatabaseHelper; 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 initialize(..) method first."); } return instance; } public synchronized sqliteDatabase getDatabase() { return mDatabaseHelper.getWritableDatabase(); } }
Updated code which write data to database in separate threads will look like this:
// In your application class DatabaseManager.initializeInstance(new DatabaseHelper()); // 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();
新的code将会导致一个新的crash
java.lang.IllegalStateException: attempt to re-open an already-closed object: sqliteDatabase
因为我们只使用一个数据库连接,Thread1和Thread2的都是由getDatabase()方法返回的相同连接。
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
每当你需要使用数据库时,你需要使用Class DatabaseManager的openDatabase()方法来取得数据库。我们会使用一个计数 器counter来判断是否要创建数据库对象。如果计数为1,则需要创建一个数据库,如果不为1,说明我们已经创建过了。
在closeDatabase()方法中我们同样通过判断引用计数的值,如果计数降为0,则说明我们需要close数据库。
Now you should be able to use your database and be sure - it's thread safe.
英文地址 https://github.com/dmytrodanylyk/dmytrodanylyk/blob/gh-pages/articles/Concurrent%20Database%20Access.md
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。