Skip to content

Commit af4d13b

Browse files
authored
Merge pull request #1368 from couchbase/feature/issue_and_929
Fixed https://github.com/couchbase/couchbase-lite-android/issues/929
2 parents f85b64f + 8f8681f commit af4d13b

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.couchbase.lite.internal.InterfaceAudience;
3232
import com.couchbase.lite.internal.RevisionInternal;
3333
import com.couchbase.lite.internal.database.ContentValues;
34+
import com.couchbase.lite.internal.database.sqlite.exception.SQLiteDatabaseLockedException;
3435
import com.couchbase.lite.storage.Cursor;
3536
import com.couchbase.lite.storage.SQLException;
3637
import com.couchbase.lite.storage.SQLiteStorageEngine;
@@ -63,6 +64,9 @@ public class SQLiteStore implements Store, EncryptableStore {
6364

6465
public static String kDBFilename = "db.sqlite3";
6566

67+
private static final int kTransactionMaxRetries = 10;
68+
private static final int kTransactionRetryDelay = 50; //50ms
69+
6670
// Default value for maxRevTreeDepth, the max rev depth to preserve in a prune operation
6771
private static final int DEFAULT_MAX_REVS = Integer.MAX_VALUE;
6872

@@ -2298,7 +2302,26 @@ protected boolean beginTransaction() {
22982302
try {
22992303
// Outer (level 0) transaction. Use SQLiteDatabase.beginTransaction()
23002304
if (tLevel == 0) {
2301-
storageEngine.beginTransaction();
2305+
boolean retry = true;
2306+
int retries = 0;
2307+
do {
2308+
try {
2309+
storageEngine.beginTransaction();
2310+
retry = false;
2311+
} catch (SQLiteDatabaseLockedException lockedException) {
2312+
if (++retries > kTransactionMaxRetries) {
2313+
Log.e(TAG, "Db busy, too many retries, giving up");
2314+
throw lockedException;
2315+
}
2316+
Log.i(TAG, "Db busy, retrying transaction (#%d)...", retries);
2317+
try {
2318+
// sleep 50ms to wait db will be unlocked
2319+
Thread.sleep(kTransactionRetryDelay);
2320+
} catch (InterruptedException e) {
2321+
}
2322+
}
2323+
// other exceptions should be caught outer try-catch block
2324+
} while (retry);
23022325
}
23032326
// Inner (level 1 or higher) transaction. Use SQLite's SAVEPOINT
23042327
else {

0 commit comments

Comments
 (0)