我曾经写过一篇很简短的文章,阐述了线程安全的访问android sqlite数据库。样例程序可以在这里获取到。
原文链接:https://www.f2er.com/sqlite/200661.html
假设你已经有一个自己的sqliteOpenHelper。
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中将会得到下面的输出信息,并且有一个写操作将不会成功
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(// 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
因为我们只使用一个数据库连接,Thread1和Thread2的都是由getDatabase()方法返回的相同连接。发生的什么事呢,在Thread2还在使用数据库连接时,Thread1可能已经把它给关闭了,那就是为什么你会得到崩溃异常。
Leak found
Caused by: java.lang.IllegalStateException: sqliteDatabase created and never closed
下面是一个样例程序
DatabaseManager { private AtomicInteger mOpenCounter = new AtomicInteger(); private static DatabaseManager instance; private static sqliteOpenHelper mDatabaseHelper; private sqliteDatabase mDatabase; public static synchronized initializeInstance(sqliteOpenHelper helper) { if (instance == null) { instance = new DatabaseManager(); mDatabaseHelper = helper; } } public static synchronized DatabaseManager ); } return instance; } public synchronized sqliteDatabase openDatabase() { if(mOpenCounter.incrementAndGet() == 1) { // opening new database mDatabase = mDatabaseHelper.getWritableDatabase(); } return mDatabase; } public synchronized 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
每当你需要使用数据库时,你需要使用DatabaseManager的openDatabase()方法来取得数据库。我们会使用一个引用计数来判断是否要创建数据库对象。如果引用计数为1,则需要创建一个数据库,如果不为1,说明我们已经创建过了。