Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit eebf88d

Browse files
author
hideki
committed
Fixed Java Core 937 - Dead Lock in inTransaction
Problem: - cbforest inTransaction acquires lock of database, then acquires lock for transaction. Other code could acquire lock for transaction, then acquires lock of database. this causes dead-lock. Solution: In case of enabling threadSafety for cbforest, needs to avoid to use inTransaction of cbforest. Instead of calling inTransaction, ForestDBStore maintain transactionLevel per thread.
1 parent 010a796 commit eebf88d

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/main/java/com/couchbase/lite/store/ForestDBStore.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public class ForestDBStore implements Store, EncryptableStore, Constants {
9494
private boolean readOnly = false;
9595
private SymmetricKey encryptionKey;
9696

97+
private ThreadLocal<Integer> transactionLevel4Thread = new ThreadLocal<Integer>() {
98+
@Override
99+
protected Integer initialValue() {
100+
return 0;
101+
}
102+
};
103+
97104
// Native method for deriving PBDDF2-SHA256 key:
98105
private static native byte[] nativeDerivePBKDF2SHA256Key(String password, byte[] salt, int rounds);
99106

@@ -249,7 +256,8 @@ public long getLastSequence() {
249256

250257
@Override
251258
public boolean inTransaction() {
252-
return forest.isInTransaction();
259+
//return forest.isInTransaction();
260+
return transactionLevel4Thread.get() > 0;
253261
}
254262

255263
@Override
@@ -1226,6 +1234,7 @@ private DocumentChange changeWithNewRevision(RevisionInternal inRev,
12261234
private boolean beginTransaction() {
12271235
try {
12281236
forest.beginTransaction();
1237+
transactionLevel4Thread.set(transactionLevel4Thread.get() + 1);
12291238
} catch (ForestException e) {
12301239
Log.e(TAG, "Failed to begin transaction", e);
12311240
return false;
@@ -1235,6 +1244,7 @@ private boolean beginTransaction() {
12351244

12361245
private boolean endTransaction(boolean commit) {
12371246
try {
1247+
transactionLevel4Thread.set(transactionLevel4Thread.get() - 1);
12381248
forest.endTransaction(commit);
12391249
} catch (ForestException e) {
12401250
Log.e(TAG, "Failed to end transaction", e);

0 commit comments

Comments
 (0)