Skip to content

Fix handling of sqlite transactions #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteProgram;
import android.database.sqlite.SQLiteStatement;
import android.database.sqlite.SQLiteTransactionListener;
import android.support.annotation.VisibleForTesting;
import com.google.common.base.Function;
import com.google.firebase.firestore.auth.User;
Expand Down Expand Up @@ -75,6 +76,21 @@ public static String databaseName(String persistenceKey, DatabaseId databaseId)
private final SQLiteQueryCache queryCache;
private final SQLiteRemoteDocumentCache remoteDocumentCache;
private final SQLiteLruReferenceDelegate referenceDelegate;
private final SQLiteTransactionListener transactionListener =
new SQLiteTransactionListener() {
@Override
public void onBegin() {
referenceDelegate.onTransactionStarted();
}

@Override
public void onCommit() {
referenceDelegate.onTransactionCommitted();
}

@Override
public void onRollback() {}
};

public SQLitePersistence(
Context context, String persistenceKey, DatabaseId databaseId, LocalSerializer serializer) {
Expand Down Expand Up @@ -143,35 +159,32 @@ RemoteDocumentCache getRemoteDocumentCache() {

@Override
void runTransaction(String action, Runnable operation) {
Logger.debug(TAG, "Starting transaction: %s", action);
db.beginTransactionWithListener(transactionListener);
try {
Logger.debug(TAG, "Starting transaction: %s", action);
referenceDelegate.onTransactionStarted();
db.beginTransaction();
operation.run();

// Note that an exception in operation.run() will prevent this code from running.
db.setTransactionSuccessful();
} finally {
db.endTransaction();
referenceDelegate.onTransactionCommitted();
}
}

@Override
<T> T runTransaction(String action, Supplier<T> operation) {
Logger.debug(TAG, "Starting transaction: %s", action);
T value = null;
db.beginTransactionWithListener(transactionListener);
try {
Logger.debug(TAG, "Starting transaction: %s", action);
referenceDelegate.onTransactionStarted();
db.beginTransaction();
T value = operation.get();
value = operation.get();

// Note that an exception in operation.run() will prevent this code from running.
db.setTransactionSuccessful();
return value;
} finally {
db.endTransaction();
referenceDelegate.onTransactionCommitted();
}
return value;
}

/**
Expand Down