Skip to content

Commit 99c15cb

Browse files
committed
DbUtils写操作加入锁,防止多线程操作写入异常。
1 parent 265b5b1 commit 99c15cb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

library/src/com/lidroid/xutils/DbUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.HashMap;
3232
import java.util.List;
3333
import java.util.concurrent.ConcurrentHashMap;
34+
import java.util.concurrent.locks.Lock;
35+
import java.util.concurrent.locks.ReentrantLock;
3436

3537
public class DbUtils {
3638

@@ -801,9 +803,15 @@ private void debugSql(String sql) {
801803
}
802804
}
803805

806+
private Lock writeLock = new ReentrantLock();
807+
private volatile boolean writeLocked = false;
808+
804809
private void beginTransaction() {
805810
if (allowTransaction) {
806811
database.beginTransaction();
812+
} else {
813+
writeLock.lock();
814+
writeLocked = true;
807815
}
808816
}
809817

@@ -817,6 +825,10 @@ private void endTransaction() {
817825
if (allowTransaction) {
818826
database.endTransaction();
819827
}
828+
if (writeLocked) {
829+
writeLock.unlock();
830+
writeLocked = false;
831+
}
820832
}
821833

822834

0 commit comments

Comments
 (0)