mergedOps.add(operation2); Database db = operation1.getDatabase(); db.beginTransaction(); boolean success = false; AsyncOperation operation = mergedOps.get(i); executeOperation(operation); if (operation.isFailed()) { if (i < maxOperationCountToMerge && operation.isMergeableWith(peekedOp)) { AsyncOperation removedOp = queue.remove(); if (removedOp != peekedOp) { "one instead..."); for (AsyncOperation asyncOperation : mergedOps) { asyncOperation.reset(); executeOperationAndPostCompleted(asyncOperation);
/** * The operation's result after it has completed. Waits until a result is available. * * @return The operation's result or null if the operation type does not produce any result. * @throws {@link AsyncDaoException} if the operation produced an exception * @see #waitForCompletion() */ public synchronized Object getResult() { if (!completed) { waitForCompletion(); } if (throwable != null) { throw new AsyncDaoException(this, throwable); } return result; }
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; }
/** * @return true if this operation is mergeable with the given operation. Checks for null, {@link #FLAG_MERGE_TX}, * and if the database instances match. */ boolean isMergeableWith(AsyncOperation other) { return other != null && isMergeTx() && other.isMergeTx() && getDatabase() == other.getDatabase(); }
if (operation.isMergeTx()) { if (operation.isMergeableWith(operation2)) { mergeTxAndExecute(operation, operation2); } else {
public void testOperationGetResultException() { SimpleEntity entity = new SimpleEntity(); daoSession.insert(entity); AsyncOperation operation = asyncSession.insert(entity); try { operation.getResult(); fail("getResult should have thrown"); } catch (AsyncDaoException expected) { // OK } assertTrue(operation.isCompleted()); assertFalse(operation.isCompletedSucessfully()); assertTrue(operation.isFailed()); }
public void testAsyncException() { SimpleEntity entity = new SimpleEntity(); daoSession.insert(entity); AsyncOperation operation = asyncSession.insert(entity); assertWaitForCompletion1Sec(); assertSingleOperationCompleted(operation); assertTrue(operation.isFailed()); assertNotNull(operation.getThrowable()); }
public void testAsyncInsert() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("heho"); AsyncOperation operation = asyncSession.insert(entity); assertWaitForCompletion1Sec(); SimpleEntity entity2 = daoSession.load(SimpleEntity.class, entity.getId()); assertNotNull(entity2); assertEquals("heho", entity2.getSimpleString()); assertFalse(operation.isFailed()); assertSingleOperationCompleted(operation); }
private void executeTransactionRunnable(AsyncOperation operation) { Database db = operation.getDatabase(); db.beginTransaction(); try { ((Runnable) operation.parameter).run(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
private void handleOperationCompleted(AsyncOperation operation) { operation.setCompleted(); AsyncOperationListener listenerToCall = listener; if (listenerToCall != null) { listenerToCall.onAsyncOperationCompleted(operation); } if (listenerMainThread != null) { if (handlerMainThread == null) { handlerMainThread = new Handler(Looper.getMainLooper(), this); } Message msg = handlerMainThread.obtainMessage(1, operation); handlerMainThread.sendMessage(msg); } synchronized (this) { countOperationsCompleted++; if (countOperationsCompleted == countOperationsEnqueued) { notifyAll(); } } }
public void testAsyncExceptionCreator() { SimpleEntity entity = new SimpleEntity(); daoSession.insert(entity); AsyncOperation operation = asyncSession.insert(entity); assertWaitForCompletion1Sec(); assertNull(operation.getCreatorStacktrace()); operation = asyncSession.insert(entity, AsyncOperation.FLAG_TRACK_CREATOR_STACKTRACE); assertWaitForCompletion1Sec(); assertNotNull(operation.getCreatorStacktrace()); asyncSession.setSessionFlags(AsyncOperation.FLAG_TRACK_CREATOR_STACKTRACE); operation = asyncSession.insert(entity); assertWaitForCompletion1Sec(); assertNotNull(operation.getCreatorStacktrace()); StackTraceElement[] stack = operation.getCreatorStacktrace().getStackTrace(); boolean found = false; for (StackTraceElement stackTraceElement : stack) { found |= stackTraceElement.getClassName().equals(getClass().getName()); } assertTrue(found); }
/** * @return true if this operation is mergeable with the given operation. Checks for null, {@link #FLAG_MERGE_TX}, * and if the database instances match. */ boolean isMergeableWith(AsyncOperation other) { return other != null && isMergeTx() && other.isMergeTx() && getDatabase() == other.getDatabase(); }
if (operation.isMergeTx()) { if (operation.isMergeableWith(operation2)) { mergeTxAndExecute(operation, operation2); } else {
public void testAsyncUpdate() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("heho"); daoSession.insert(entity); entity.setSimpleString("updated"); AsyncOperation operation = asyncSession.update(entity); assertWaitForCompletion1Sec(); daoSession.clear(); SimpleEntity entity2 = daoSession.load(SimpleEntity.class, entity.getId()); assertNotNull(entity2); assertEquals("updated", entity2.getSimpleString()); assertFalse(operation.isFailed()); assertSingleOperationCompleted(operation); }
@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(); } }
private void handleOperationCompleted(AsyncOperation operation) { operation.setCompleted(); AsyncOperationListener listenerToCall = listener; if (listenerToCall != null) { listenerToCall.onAsyncOperationCompleted(operation); } if (listenerMainThread != null) { if (handlerMainThread == null) { handlerMainThread = new Handler(Looper.getMainLooper(), this); } Message msg = handlerMainThread.obtainMessage(1, operation); handlerMainThread.sendMessage(msg); } synchronized (this) { countOperationsCompleted++; if (countOperationsCompleted == countOperationsEnqueued) { notifyAll(); } } }
mergedOps.add(operation2); Database db = operation1.getDatabase(); db.beginTransaction(); boolean success = false; AsyncOperation operation = mergedOps.get(i); executeOperation(operation); if (operation.isFailed()) { if (i < maxOperationCountToMerge && operation.isMergeableWith(peekedOp)) { AsyncOperation removedOp = queue.remove(); if (removedOp != peekedOp) { "one instead..."); for (AsyncOperation asyncOperation : mergedOps) { asyncOperation.reset(); executeOperationAndPostCompleted(asyncOperation);
public void testAsynCallInTx() { AsyncOperation operation = asyncSession.callInTx(new Callable<String>() { @Override public String call() throws Exception { txThread = Thread.currentThread(); return "OK"; } }); assertEquals("OK", operation.waitForCompletion()); assertNotNull(txThread); assertFalse(Thread.currentThread().equals(txThread)); }
private void executeTransactionRunnable(AsyncOperation operation) { Database db = operation.getDatabase(); db.beginTransaction(); try { ((Runnable) operation.parameter).run(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
private <E> AsyncOperation enqueEntityOperation(OperationType type, Class<E> entityClass, Object param, int flags) { AbstractDao<?, ?> dao = daoSession.getDao(entityClass); AsyncOperation operation = new AsyncOperation(type, dao, null, param, flags | sessionFlags); executor.enqueue(operation); return operation; }