Skip to content

Commit 464e45d

Browse files
author
hideki
committed
1 parent f85b64f commit 464e45d

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

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

Lines changed: 25 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,10 @@ public class SQLiteStore implements Store, EncryptableStore {
6364

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

67+
private static final int kSQLiteBusyTimeout = 5000; // 5sec - Currently not used - default: 2.5sec
68+
private static final int kTransactionMaxRetries = 10;
69+
private static final int kTransactionRetryDelay = 50; //50ms
70+
6671
// Default value for maxRevTreeDepth, the max rev depth to preserve in a prune operation
6772
private static final int DEFAULT_MAX_REVS = Integer.MAX_VALUE;
6873

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

0 commit comments

Comments
 (0)