I tried executing 3 “Save Operations” in a single BATCH but it didn’t worked as a single transaction as a '“CouchbaseLiteException” occured on 2nd Operation but the BATCH did not aborted.
3rd operation was successful and 1st operation changes were not rolled back.
Breaking the loop when exception occurs avoided performing the operations following the “Operation that caused Exception” but sill the operations preceding it were not rolled back.
Below is test class written in JAVA for Android platform.
public class DatabaseManager {
private Context context;
Database database;
File directory;
public Database getDatabase() {
return database;
}
public File getDirectory() {
return directory;
}
public DatabaseManager(Context context) {
try {
CouchbaseLite.init(context);
this.context = context;
// Set Database configuration
DatabaseConfiguration config = new DatabaseConfiguration();
directory = context.getDir("MYDB", Context.MODE_PRIVATE);
config.setDirectory(directory.toString());
// Create / Open a database with specified name and configuration
database = new Database("MYDB", config);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
public String executeTransaction() {
final String[] response = {"transaction successfull"};
try {
String SAVE_OP_1 = "{\"name\":\"john\",\"age\":22,\"class\":\"mca\"}";
String SAVE_OP_2 = "{\"name\":\"keth\",\"age\":25,\"class\":\"mca\"}";
String SAVE_OP_3 = "{\"name\":\"skye\",\"age\":36,\"class\":\"mca\"}";
Map<String, JSONObject> dataMap = new LinkedHashMap<>();
try {
dataMap.put("FirsrtDocId", new JSONObject(SAVE_OP_1));
dataMap.put("_SecondDocId", new JSONObject(SAVE_OP_2));
dataMap.put("ThirdDocId", new JSONObject(SAVE_OP_3));
} catch (JSONException e) {
e.printStackTrace();
}
database.inBatch(new Runnable() {
@Override
public void run() {
Iterator it = dataMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
HashMap documentMap = new Gson().fromJson(pair.getValue().toString(), HashMap.class);
MutableDocument mutableDocument = new MutableDocument(String.valueOf(pair.getKey()));
mutableDocument.setData(documentMap);
try {
database.save(mutableDocument);
} catch (CouchbaseLiteException e) {
response[0] = "CouchbaseLiteException while saving " + pair.getKey() + e.getMessage();
e.printStackTrace();
}
}
}
});
} catch (CouchbaseLiteException e) {
e.printStackTrace();
response[0] = "CouchbaseLiteException in transaction \n" + e.getMessage();
}
return response[0];
}
}
Below is CouchbaseLiteException on Operation#2 because the document id started with an underscore _
Below is Database that contains 2 documents after the execution of above BATCH.
Researching more found another thread on Github ( Couchbase Lite Issues) with discussion on same topic that says "Couchbase Lite doesn’t support rollback.
Also in Couchbase Lite documentation on Batch Operations Section couldn’t find any infromation on Rollback.
As said by @priya.rajagopal Couchbase Lite does not support transactions today. So scenario #1 with rollback will not be supported.
inBatch is for efficiency for bulk operations but it doesn’t run as a single transaction.
What is your opinion on this issue?