use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.
the class App method onCreate.
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.
the class AsyncOperationExecutor method mergeTxAndExecute.
/** Also checks for other operations in the queue that can be merged into the transaction. */
private void mergeTxAndExecute(AsyncOperation operation1, AsyncOperation operation2) {
ArrayList<AsyncOperation> mergedOps = new ArrayList<AsyncOperation>();
mergedOps.add(operation1);
mergedOps.add(operation2);
Database db = operation1.getDatabase();
db.beginTransaction();
boolean success = false;
try {
for (int i = 0; i < mergedOps.size(); i++) {
AsyncOperation operation = mergedOps.get(i);
executeOperation(operation);
if (operation.isFailed()) {
// Operation may still have changed the DB, roll back everything
break;
}
if (i == mergedOps.size() - 1) {
AsyncOperation peekedOp = queue.peek();
if (i < maxOperationCountToMerge && operation.isMergeableWith(peekedOp)) {
AsyncOperation removedOp = queue.remove();
if (removedOp != peekedOp) {
// Paranoia check, should not occur unless threading is broken
throw new DaoException("Internal error: peeked op did not match removed op");
}
mergedOps.add(removedOp);
} else {
// No more ops in the queue to merge, finish it
db.setTransactionSuccessful();
success = true;
break;
}
}
}
} finally {
try {
db.endTransaction();
} catch (RuntimeException e) {
DaoLog.i("Async transaction could not be ended, success so far was: " + success, e);
success = false;
}
}
if (success) {
int mergedCount = mergedOps.size();
for (AsyncOperation asyncOperation : mergedOps) {
asyncOperation.mergedOperationsCount = mergedCount;
handleOperationCompleted(asyncOperation);
}
} else {
DaoLog.i("Reverted merged transaction because one of the operations failed. Executing operations one by " + "one instead...");
for (AsyncOperation asyncOperation : mergedOps) {
asyncOperation.reset();
executeOperationAndPostCompleted(asyncOperation);
}
}
}
use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.
the class AsyncOperationExecutor method executeTransactionRunnable.
private void executeTransactionRunnable(AsyncOperation operation) {
Database db = operation.getDatabase();
db.beginTransaction();
try {
((Runnable) operation.parameter).run();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.
the class AsyncOperationExecutor method executeTransactionCallable.
@SuppressWarnings("unchecked")
private void executeTransactionCallable(AsyncOperation operation) throws Exception {
Database db = operation.getDatabase();
db.beginTransaction();
try {
operation.result = ((Callable<Object>) operation.parameter).call();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.
the class AsyncSession method enqueueDatabaseOperation.
private AsyncOperation enqueueDatabaseOperation(OperationType type, Object param, int flags) {
Database database = daoSession.getDatabase();
AsyncOperation operation = new AsyncOperation(type, null, database, param, flags | sessionFlags);
executor.enqueue(operation);
return operation;
}
Aggregations