目录
解决问题2:如果当前执行的是许多sql语句,要用到事务怎么办?
解决问题3:如果在多线程下执行query方法怎么办,如果处理close?
解决问题4:实际使用过程中,很容易导致一个错误,当cursor或db已经关闭的时候,又去调用了它,导致异常。
最近碰到一个异常问题:
01-01 08:01:14.011 580 595 E SQLiteLog: (5) database is locked
01-01 08:01:14.015 580 595 E SQLiteDatabase: Failed to open database '/data/user_de/0/com.android.providers.settings/databases/settings.db'.
01-01 08:01:14.015 580 595 E SQLiteDatabase: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:635)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:321)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:295)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:216)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry$UpgradeController.upgradeIfNeededLocked(SettingsProvider.java:2966)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry.ensureSettingsForUserLocked(SettingsProvider.java:2354)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry.peekSettingsStateLocked(SettingsProvider.java:2618)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry.getSettingsNamesLocked(SettingsProvider.java:2300)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry.syncSsaidTableOnStart(SettingsProvider.java:2282)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider$SettingsRegistry.<init>(SettingsProvider.java:2180)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.providers.settings.SettingsProvider.onCreate(SettingsProvider.java:326)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.app.ActivityThread.installProvider(ActivityThread.java:6266)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.app.ActivityThread.acquireProvider(ActivityThread.java:5875)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:2508)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.content.ContentResolver.acquireProvider(ContentResolver.java:1764)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.provider.Settings$ContentProviderHolder.getProvider(Settings.java:1798)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:1887)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.provider.Settings$Global.getStringForUser(Settings.java:10532)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.provider.Settings$Global.getString(Settings.java:10521)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.DropBoxManagerService.isTagEnabled(DropBoxManagerService.java:323)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.DropBoxManagerService$2.isTagEnabled(DropBoxManagerService.java:144)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.os.DropBoxManager.isTagEnabled(DropBoxManager.java:346)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.am.ActivityManagerService.addErrorToDropBox(ActivityManagerService.java:14957)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.am.ActivityManagerService.handleApplicationWtfInner(ActivityManagerService.java:14838)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.am.ActivityManagerService$21.run(ActivityManagerService.java:14808)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.os.Handler.handleCallback(Handler.java:790)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.os.Looper.loop(Looper.java:164)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at android.os.HandlerThread.run(HandlerThread.java:65)
01-01 08:01:14.015 580 595 E SQLiteDatabase: at com.android.server.ServiceThread.run(ServiceThread.java:46)
01-01 08:01:14.017 580 595 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: ActivityManager
01-01 08:01:14.017 580 595 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider com.android.providers.settings.SettingsProvider: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6269)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.app.ActivityThread.acquireProvider(ActivityThread.java:5875)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:2508)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.content.ContentResolver.acquireProvider(ContentResolver.java:1764)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.provider.Settings$ContentProviderHolder.getProvider(Settings.java:1798)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:1887)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.provider.Settings$Global.getStringForUser(Settings.java:10532)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.provider.Settings$Global.getString(Settings.java:10521)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.DropBoxManagerService.isTagEnabled(DropBoxManagerService.java:323)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.DropBoxManagerService$2.isTagEnabled(DropBoxManagerService.java:144)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.os.DropBoxManager.isTagEnabled(DropBoxManager.java:346)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.am.ActivityManagerService.addErrorToDropBox(ActivityManagerService.java:14957)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.am.ActivityManagerService.handleApplicationWtfInner(ActivityManagerService.java:14838)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.am.ActivityManagerService$21.run(ActivityManagerService.java:14808)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:790)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:65)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.server.ServiceThread.run(ServiceThread.java:46)
01-01 08:01:14.017 580 595 E AndroidRuntime: Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:635)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:321)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:295)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:216)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
01-01 08:01:14.017 580 595 E AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry$UpgradeController.upgradeIfNeededLocked(SettingsProvider.java:2966)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry.ensureSettingsForUserLocked(SettingsProvider.java:2354)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry.peekSettingsStateLocked(SettingsProvider.java:2618)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry.getSettingsNamesLocked(SettingsProvider.java:2300)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry.syncSsaidTableOnStart(SettingsProvider.java:2282)
01-01 08:01:14.017 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider$SettingsRegistry.<init>(SettingsProvider.java:2180)
01-01 08:01:14.018 580 595 E AndroidRuntime: at com.android.providers.settings.SettingsProvider.onCreate(SettingsProvider.java:326)
01-01 08:01:14.018 580 595 E AndroidRuntime: at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
01-01 08:01:14.018 580 595 E AndroidRuntime: at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
01-01 08:01:14.018 580 595 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6266)
01-01 08:01:14.018 580 595 E AndroidRuntime: ... 18 more
类似的错误信息:android-emulator - 从 18到 17的Android API级别更改,但Android从不是"Boots Up"
参考了各大博主的博客以及论坛,对于该问题的分析如下:
1. database is locked的原因
参考自SQLiteDatabaseLockedException。
(1)多线程访问造成的数据库锁定
sqlite同一时间只能进行一个写操作,当同同时有两个写操作时,后执行的只能先等待,如果等待时间超过5s就会产生这种错误。同样一个文件正在写入,重复打开数据库操作更容易导致这种问题发生。(参考自SQLite并发操作下的分析与处理)
如A线程在访问当前的数据库,这时候B线程也需要访问数据库,这样在B线程中,就会有类似以上的异常产生,我们需要将提供数据库访问的方法设置成同步的,防止异步调用时出现问题,如在调用方法中增加 synchronized 修饰符。
使用synchronized 关键字来修饰获取数据库连接的方法,或者使用isDbLockedByOtherThreads方法判断数据库是否被锁住了,然后等待一定的时间再进行访问。
(2)执行事务操作未正常关闭
如下面代码,使用事务操作数据库,但事务执行完成后未调用db.endTransaction();关闭事务。
public ArrayList GetIndustryList(){
ArrayList IndustryList=new ArrayList();
SQLiteDatabase db=openDatabase();
db.beginTransaction(); //执行事务,无对应的关闭事务
Cursor cursor = db.rawQuery("select * from dcIndustry",null);
while(cursor.moveToNext()){
IndustryList.add(new Industry(cursor.getString(0),cursor.getString(1)));
}
db.close();
return IndustryList;
}
Android 4.0以前的版本db.close();会结束事务,而Jelly Bean 以后的版本因为安全性的问题,必须结束即endTransactiony以后才能再次访问本地数据库。
所以要修改为:
public ArrayList GetIndustryList(){
ArrayList IndustryList=new ArrayList();
SQLiteDatabase db=openDatabase();
db.beginTransaction(); //执行事务,无对应的关闭事务
Cursor cursor = db.rawQuery("select * from dcIndustry",null);
while(cursor.moveToNext()){
IndustryList.add(new Industry(cursor.getString(0),cursor.getString(1)));
}
db.endTransaction();
db.close();
return IndustryList;
}
(3)sqlite自身问题
有时我们会在调试程序的时候发现Log控制台频繁的出现上述异常,而在运行程序的时候就没有这个问题,这种现象我在调试ResultSet时也会出现,查资料找原因是因为sqlite数据不完全是线程安全的,导致在一些地方会莫名其妙的出问题,如果遇到这样的情况,那只能不要将断点打到数据库连接的地方了。
2. 解决办法
(1)办法1
参考自SQLite并发操作下的分析与处理,解决database is locked,以及多线程下执行事务等问题
解决问题1:避免重复打开数据库
引入单例方法与SQLiteOpenHelper类:
public class DBOpenHelper extends SQLiteOpenHelper{
private DBOpenHelper(Context context,String dbPath, int version) {
super(context, dbPath , null, version);
}
private volatile static DBOpenHelper uniqueInstance;
public static DBOpenHelper getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (DBOpenHelper.class) {
if (uniqueInstance == null) {
uniqueInstance = new DBOpenHelper(context,context.getFilesDir().getAbsolutePath()+"/foowwlite.db",1);
}
}
}
return uniqueInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
通过getInstance()方法得到helper对象来得到数据库,保证helper类是单例的。然后通过代理类SQLiteDataProxy来控制对数据库的访问:
private static SQLiteDataProxy proxy;
private static DBOpenHelper helper;
public static SQLiteDataProxy getSQLiteProxy(Context context) {
helper = DBOpenHelper.getInstance(context);
if (proxy == null) {
synchronized (SQLiteDataProxy.class) {
if (proxy == null) {
proxy = new SQLiteDataProxy();
}
}
}
return proxy;
}
}
同样使用单例来保证对象的唯一性。然后写了一个方法用于执行sql语句:
@Override
public boolean execSQL(String sql) {
boolean result = true;
if (!db.isOpen()) {
db = helper.getWritableDatabase();
}
try {
db.execSQL(sql);
} catch (Exception e) {
Log.e("SQLERROR", "In SQLDA:" + e.getMessage() + sql);
result = false;
} finally {
db.close();
}
return result;
}
每次获取db对象即SQLiteDataBase的时候先判断是否已经打开,如果已经打开则不需要重新获取,避免了db的重复开启。
接下来实验一下,通过Viewpager+Fragment的方式测试,因为fragment会同时被加载。在两个fragment中各自新建一个子线程来执行大量insert语句:
new Thread(new Runnable() {
@Override
public void run() {
for (String sql:getSQLList()){
SQLiteDataProxy.getSQLiteProxy(getActivity()).execSQL(sql);
}
}
}).start();
先分析一下,虽然都是子线程,但是两个fragment都是通过单例获得db对象来执行sql语句,因此db应该是同一个,这时候它们并发执行sql应该没问题。
但是运行还是报错了:attempt to re-open an already-closed object。
意思是数据库已经关闭了,但是仍然用一个已经关闭的db去执行sql语句。错误原因是什么呢?明明已经判断了db.isOpen,再执行sql的。
例子中的原因如下:
线程1和线程2同时执行execSQL方法,线程1先获取到了db对象,开始执行sql语句,与此同时,线程2判断db.isOpen结果是打开的,于是不重新get,直接调用db开始执行sql语句。但现在问题来了,线程2准备执行sql的时候,线程1已经把sql执行完并且关闭了。由于线程1和线程2得到的db是同一个对象,线程2的db也关闭了,这时候执行sql就导致了这个错误。
这种情况,可以引入一个全局变量来计算打开关闭db的次数,而java也刚好提供了这个方法:AtomicInteger
AtomicInteger是一个线程安全的类,可以通过它计数,无论什么线程AtomicInteger值+1后都会改变。
将判断db是否打开的方法改成以下:
private AtomicInteger mOpenCounter = new AtomicInteger();
关闭db的方法修改为:
if (mOpenCounter.incrementAndGet() == 1) {
db = helper.getWritableDatabase();
}
return db;
意思就是每当想要获得db对象的时,计数器mOpenCounter会加1,第一次打开数据库mOpenCounter是0,mOpenCounter调用incrementAndGet()方法后加1等于1,说明还没有被获得。此时有第二个线程项执行sql语句,它在执行getSQLiteDataBase方法的时候mOpenCounter值是1,然后mOpenCounter+1 = 2不等于1说明db已经开启,直接return db即可。
在关闭db的时候,mOpenCounter会首先减1,如果mOpenCounter==0说明此时没有其他操作,就可以关闭数据库,如果不等于则说明还有其他sql在执行,就不去关闭。
接着说上面,两个线程各自想执行sql,此时mOpenCounter是2,当线程1的sql执行完后,线程1的db尝试关闭,会调用mOpenCounter.decrementAndGet()自减1,结果就等于1。说明还有1个正在执行的sql,即线程2正在执行。因此db不会去关闭,然后线程2正常执行,线程2执行完sql,尝试关闭db,此时decrementAndGet再自减1,就等于0,说明已经没有其他真正执行的sql,于是可以正常关闭。
这种方法保证了只有在所有sql都执行完后才去关闭,并且只会最后一次关闭,保证了不会出现re-open an already-closed问题。
修改后还是报错:
Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object referenceInsert
显然db为null。为什么会导致这种错误呢?
分析一下AtomicInteger,并没有逻辑上的问题呀。
将代码改成如下:
private SQLiteDatabase getSQLiteDataBase() {
Log.e("111", "Once start");
if (mOpenCounter.incrementAndGet() == 1 || db == null) {
db = helper.getWritableDatabase();
}
if (db == null) {
Log.e("111", mOpenCounter.intValue() + "");
} else {
Log.e("111", mOpenCounter.intValue() + " NOT NULL");
}
return db;
}
运行后结果:
可以想到,线程1和线程2同时尝试获取db,线程1中mOpenCounter + 1 = =1,但此时db还没有获取的情况下,线程2也执行了获取db的方法,mOpenCounter +1==2,但由于获取db的getWritableDatabase()需要一定的时间,而先执行的线程1的db还没有被获取到,线程2却已经也经过判断并且return db,此时的db就是null了,导致了空指针错误。
既然原因已经找到了,那么解决就很简单了,只需要多加一个非空判断就行了,而getWritableDataBase()本身就是线程安全的,应该只需要这样就可以解决。
private SQLiteDatabase getSQLiteDataBase() {
if (mOpenCounter.incrementAndGet() == 1 || db == null) {
db = helper.getWritableDatabase();
}
return db;
}
这样修改后,经过测试没问题。
解决问题2:如果当前执行的是许多sql语句,要用到事务怎么办?
如果是事务,事务执行的时候调用beginTransaction(),完成后调用db.setTransactionSuccessful()、db.endTransaction()标志事务的结束,但是如果多线程下调用了事务怎么办?尤其还是单例模式下,同时调用方法开启事务,肯定会出问题。
如以下方法:
public boolean execSQLList(List<String> sqlList) {
boolean result = true;
db = getSQLiteDataBase();
String currentSqlString = "";
try {
db.beginTransaction();
for (String sql : sqlList) {
currentSqlString = sql;
db.execSQL(sql);
}
db.setTransactionSuccessful();
result = true;
} catch (Exception e) {
result = false;
Log.e("SQLERROR", "IN SQLDA: " + e.getMessage() + currentSqlString);
} finally {
db.endTransaction();
closeSQLiteDatabase();
}
return result;
}
for循环中间执行sql,并且开始和结束分别打开关闭事务。
为了解决这个问题,只有保证执行事务时是同步的,但是多线程调用如何控制其同步呢。
这里要引入类java.util.concurrent.Semaphore,这个类可以用来协调多线程下的控制同步问题。
首先初始化这个类,并且设置信号量为1:
private java.util.concurrent.Semaphore semaphoreTransaction = new java.util.concurrent.Semaphore(1);
这句的意思是多线程下的调用许可数为1,当
semaphoreTransaction.acquire()
执行后,semaphore会检测是否有其他信号量已经执行。如果有,该线程就会停止,直到另一个semaphore释放资源之后,才会继续执行下去,即:
semaphoreTransaction.release();
我们只需要在开始事务前调用acquire方法,当其他事务想要执行,会先判断,如果有事务在执行,该线程就会等待,直到第一个事务结束并调用release之后,该线程的事务就会继续执行。这样解决事务并发产生的问题,也保证了事务可以执行完毕。
改进后代码:
private java.util.concurrent.Semaphore semaphoreTransaction = new java.util.concurrent.Semaphore(1);
public boolean execSQLList(List<String> sqlList) {
boolean result = true;
db = getSQLiteDataBase();
String currentSqlString = "";
try {
semaphoreTransaction.acquire();
db.beginTransaction();
for (String sql : sqlList) {
currentSqlString = sql;
db.execSQL(sql);
}
db.setTransactionSuccessful();
result = true;
} catch (Exception e) {
result = false;
Log.e("SQLERROR", "IN SQLDA: " + e.getMessage() + currentSqlString);
} finally {
db.endTransaction();
semaphoreTransaction.release();
closeSQLiteDatabase();
}
return result;
}
经过修改后,经过测试,多线程下事务也可以正常插入,一些常见的由SQLite并发产生的问题也得以解决。
全部代码如下:
【ISQLiteOperate】
package com.xiaoqi.sqlitedemo;
import android.database.Cursor;
import java.util.List;
/**
* Created by xiaoqi on 2016/9/1.
*/
public interface ISQLiteOperate {
boolean execSQL(String sql);
boolean execSQLList(List<String> sqlList);
boolean execSQLs(List<String[]> sqlList);
boolean execSQLIgnoreError(List<String> sqlList);
Cursor query(String sql);
Cursor query(String sql, String[] params);
void close();
}
【DBOpenHelper】
package com.xiaoqi.sqlitedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by xiaoqi on 2016/9/1.
*/
public class DBOpenHelper extends SQLiteOpenHelper{
private DBOpenHelper(Context context,String dbPath, int version) {
super(context, dbPath , null, version);
}
private volatile static DBOpenHelper uniqueInstance;
public static DBOpenHelper getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (DBOpenHelper.class) {
if (uniqueInstance == null) {
uniqueInstance = new DBOpenHelper(context,context.getFilesDir().getAbsolutePath()+"/foowwlite.db",1);
}
}
}
return uniqueInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
【SQLiteDataProxy】
package com.xiaoqi.sqlitedemo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class SQLiteDataProxy implements ISQLiteOperate {
private java.util.concurrent.Semaphore semaphoreTransaction = new java.util.concurrent.Semaphore(1);
private AtomicInteger mOpenCounter = new AtomicInteger();
private SQLiteDatabase db;
private Cursor cursor;
private SQLiteDataProxy() {
}
private static SQLiteDataProxy proxy;
private static DBOpenHelper helper;
public static SQLiteDataProxy getSQLiteProxy(Context context) {
helper = DBOpenHelper.getInstance(context);
if (proxy == null) {
synchronized (SQLiteDataProxy.class) {
if (proxy == null) {
proxy = new SQLiteDataProxy();
}
}
}
return proxy;
}
private SQLiteDatabase getSQLiteDataBase() {
if (mOpenCounter.incrementAndGet() == 1) {
db = helper.getWritableDatabase();
}
return db;
}
private void closeSQLiteDatabase(){
if(mOpenCounter.decrementAndGet() == 0){
db.close();
}
}
@Override
public boolean execSQL(String sql) {
boolean result = true;
db = getSQLiteDataBase();
try {
db.execSQL(sql);
} catch (Exception e) {
Log.e("SQLERROR", "In SQLDA:" + e.getMessage() + sql);
result = false;
} finally {
closeSQLiteDatabase();
}
return result;
}
@Override
public boolean execSQLList(List<String> sqlList) {
boolean result = true;
db = getSQLiteDataBase();
String currentSqlString = "";
try {
semaphoreTransaction.acquire();
db.beginTransaction();
for (String sql : sqlList) {
currentSqlString = sql;
db.execSQL(sql);
}
db.setTransactionSuccessful();
result = true;
} catch (Exception e) {
result = false;
Log.e("SQLERROR", "IN SQLDA: " + e.getMessage() + currentSqlString);
} finally {
db.endTransaction();
semaphoreTransaction.release();
closeSQLiteDatabase();
}
return result;
}
@Override
public boolean execSQLs(List<String[]> sqlList) {
boolean result = true;
db = getSQLiteDataBase();
String currentSql = "";
try {
semaphoreTransaction.acquire();
db.beginTransaction();
for (String[] arr : sqlList) {
currentSql = arr[0];
Cursor curCount = db.rawQuery(arr[0], null);
curCount.moveToFirst();
int count = curCount.getInt(0);
curCount.close();
if (count == 0) {
if (arr[1] != null && arr[1].length() > 0) {
currentSql = arr[1];
db.execSQL(arr[1]);
}
} else {
if (arr.length > 2 && arr[2] != null && arr[2].length() > 0) {
currentSql = arr[2];
db.execSQL(arr[2]);
}
}
}
db.setTransactionSuccessful();
result = true;
} catch (Exception e) {
Log.e("SQLERROR", "IN SQLDA: " + currentSql + e.getMessage());
result = false;
} finally {
db.endTransaction();
semaphoreTransaction.release();
closeSQLiteDatabase();
}
return result;
}
@Override
public boolean execSQLIgnoreError(List<String> sqlList) {
db = getSQLiteDataBase();
try {
semaphoreTransaction.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
db.beginTransaction();
for (String sql : sqlList) {
try {
db.execSQL(sql);
} catch (Exception e) {
Log.e("SQLERROR", "IN SQLDA: " + sql + e.getMessage());
}
}
db.setTransactionSuccessful();
db.endTransaction();
semaphoreTransaction.release();
closeSQLiteDatabase();
return true;
}
@Override
public Cursor query(String sql) {
return query(sql, null);
}
@Override
public Cursor query(String sql, String[] params) {
db = getSQLiteDataBase();
cursor = db.rawQuery(sql, params);
return cursor;
}
@Override
public void close() {
if (cursor != null) {
cursor.close();
}
closeSQLiteDatabase();
}
}
【DBManager】
package com.xiaoqi.sqlitedemo;
import android.content.Context;
import android.database.Cursor;
import java.util.List;
public class DBManager {
public static void asyncExecSQL(final Context context, final String sql){
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDataProxy.getSQLiteProxy(context).execSQL(sql);
}
}).start();
}
public static void asyncExecSQLList(final Context context,final List<String> sqlList){
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDataProxy.getSQLiteProxy(context).execSQLList(sqlList);
}
}).start();
}
public static void asyncExecSQLs(final Context context,final List<String[]> sqlList){
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDataProxy.getSQLiteProxy(context).execSQLs(sqlList);
}
}).start();
}
public static void asyncExecSQLIgnoreError(final Context context,final List<String> sqlList){
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDataProxy.getSQLiteProxy(context).execSQLIgnoreError(sqlList);
}
}).start();
}
public static boolean execSQL( Context context, String sql){
return SQLiteDataProxy.getSQLiteProxy(context).execSQL(sql);
}
public static boolean execSQLList( Context context, List<String> sqlList){
return SQLiteDataProxy.getSQLiteProxy(context).execSQLList(sqlList);
}
public static boolean execSQLs( Context context, List<String[]> sqlList){
return SQLiteDataProxy.getSQLiteProxy(context).execSQLs(sqlList);
}
public static boolean execSQL( Context context, List<String> sqlList){
return SQLiteDataProxy.getSQLiteProxy(context).execSQLIgnoreError(sqlList);
}
public static Cursor query(Context context, String sql){
return SQLiteDataProxy.getSQLiteProxy(context).query(sql);
}
public static Cursor query(Context context, String sql, String[] params){
return SQLiteDataProxy.getSQLiteProxy(context).query(sql, params);
}
public static void close(Context context){
SQLiteDataProxy.getSQLiteProxy(context).close();
}
}
建议使用的时候不要直接调用SQLiteDataProxy的方法,而是通过DBManager来执行SQL操作。使用方法如下:
DBManager.asyncExecSQLList(context,getSQLList())
DBManager.execSQLList(context,getSQLList())
解决问题3:如果在多线程下执行query方法怎么办,如果处理close?
public Cursor query(String sql, String[] params) {
db = getSQLiteDataBase();
cursor = db.rawQuery(sql, params);
return cursor;
}
其他的方法无论什么情况最后都会执行close方法,但是query方法不一样,因为调用这个方法需要我们手动控制close,如果多线程下执行这个方法,并且有一个线程的query出现异常了怎么办?这样close方法就不能正常调用,会导致数据库永远关闭不了。
为了解决这个方法,可以引入一个变量:
private ThreadLocal<Boolean> isQuery = new ThreadLocal<>();
ThreadLocal可以让这个boolean值保持只在自己的线程中改变,不受其他线程影响。因此我们可以通过这个值来判断是否query执行成功并且关闭,如果query中发生异常,就调用close方法,没有异常就不调用,这样就不会影响到openCount的值。
具体修改部分如下:
private ThreadLocal<Boolean> isQuery = new ThreadLocal<>();
@Override
public Cursor query(String sql) {
return query(sql, null);
}
@Override
public Cursor query(String sql, String[] params) {
isQuery.set(true);//设置为true,表示正在查询</span>
db = getSQLiteDataBase();
cursor = db.rawQuery(sql, params);
return cursor;
}
/*如果调用query方法,抛异常时要调用此方法</span>
*/
@Override
public void closeWhileError(){
if (cursor != null) {
cursor.close();
}
if(isQuery.get()){//没有执行完毕,异常后需要去关闭数据库
closeSQLiteDataBase();
}
}
private void closeSQLiteDataBase(){
if(mOpenCounter.decrementAndGet() == 0){
db.close();
isQuery.set(false);//设置为false,表示执行完毕</span>
Log.i("DataBaseState","DB------Closed");
}
}
解决问题4:实际使用过程中,很容易导致一个错误,当cursor或db已经关闭的时候,又去调用了它,导致异常。
上面的写法容错率太低,每次query和close都得对应,否则很容易导致上述错误。因此,使用了新的方法:
因为在代码中,用上述方法,如果少写了一个close,就会导致数据库永远不能关闭。为了避免这个问题,同时避免调用已经关闭的cursor、db问题,将所有的close方法都取消了,新建了一个service,监听最后一次查询的时间,如果超过5s没有进行新的数据库操作,再去关闭db,同时,每次查询时,用HashSet存储cursor,关闭db前先把所有的cursor关闭。
这种方法降低了容错率,同时避免了数据库的一直打开关闭,减少了资源的消耗。