private void apply(Log.Operation operation, Object entity, Key lock, List<Key> deletes) { Transaction transaction = datastore.beginTransaction(); if (datastore.get(transaction, lock) == null) { logger.info("Entity locked by [" + lock + "] has been applied by another transaction"); transaction.rollback(); return; } if (operation != Log.Operation.DELETE) { datastore.put(transaction, Translation.toEntities(entity)); } else { for (Entity e : Translation.toEntities(entity)) { deletes.add(e.getKey()); } } datastore.delete(transaction, deletes); transaction.commit(); }
/** Rolls back every operation in the current transaction. */ @Override public void rollback() { for (ResourceManager<Transaction> manager : managers.values()) { Transaction transaction = manager.transaction(); if (transaction.isActive()) { try { transaction.rollback(); } catch (Exception e) { logger.warning("Transaction [" + transaction.getId() + "]: Rollback failed due to [" + e + "]; Consistency will be ensured in read"); } } } }
@Override public void rollbackTransaction() { datastore.getCurrentTransaction().rollback(); }
@Override public void commit() { logger.finer("commit"); if (tx == null) { return; } tx.commit(); tx = null; logger.finer("done"); }
/** Commits this transaction with loose commitment protocol. */ @Override public void commit() throws IndoubtException { session.transaction.remove(); if (logs.size() == 0) { return; } try { for (Log log : logs) { if (log.operation() != Operation.GET) { if (transaction.isActive()) { transaction.commit(); } log.state(State.COMMITTED); } } } catch (Exception e) { throw new UncheckedException(e); } logger.fine("Transaction [" + id + "] committed"); }
@Override public boolean isActive() { return this.raw.isActive(); }
@Test public void testMiscOps() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); DatastoreAttributes attributes = waitOnFuture(service.getDatastoreAttributes()); Assert.assertNotNull(attributes); Assert.assertNotNull(attributes.getDatastoreType()); Map<Index, Index.IndexState> indexes = waitOnFuture(service.getIndexes()); Assert.assertNotNull(indexes); Transaction tx = waitOnFuture(service.beginTransaction()); try { String txId = tx.getId(); Assert.assertNotNull(txId); Assert.assertEquals(txId, tx.getId()); String appId = tx.getApp(); Assert.assertNotNull(appId); Assert.assertEquals(appId, tx.getApp()); } finally { tx.rollback(); } }
@Override public String getId() { return this.raw.getId(); }
protected <T> T inTx(Action<T> action) throws Exception { AsyncDatastoreService ads = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = ads.beginTransaction().get(); boolean ok = false; try { T result = action.run(ads); ok = true; return result; } finally { if (ok) tx.commitAsync(); else tx.rollbackAsync(); sync(); // wait for tx to finish } }
@Override public String getApp() { return this.raw.getApp(); }
FutureHelper.quietGet(fut); Future<Void> future = new TriggerFuture<Void>(this.raw.commitAsync()) { @Override protected void trigger()
void rollback() { try { txn.rollback(); clear(); } catch (IllegalArgumentException e) { throw wrapIllegalArgumentException(e); } catch (DatastoreFailureException e) { throw wrapDatastoreFailureException(e); } }
@Override public void commitTransaction() { datastore.getCurrentTransaction().commit(); }
@Override public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException { ensureInitialized(); Transaction tx = datastore.beginTransaction(); Key key = makeKey(filename); try { datastore.get(tx, key); datastore.delete(tx, key); blobstoreService.delete(getBlobKeyForFilename(filename)); } catch (EntityNotFoundException ex) { return false; } finally { if (tx.isActive()) { tx.commit(); } } return true; }
@VisibleForTesting protected boolean isParentTransactionActive() { return ObjectifyService.ofy().getTransaction() == null ? false : ObjectifyService.ofy().getTransaction().isActive(); }
@Override public void commit(Xid xid, boolean onePhase) throws XAException { super.commit(xid, onePhase); if (currentTxn != null) { currentTxn.commit(); if (NucleusLogger.TRANSACTION.isDebugEnabled()) { NucleusLogger.TRANSACTION.debug( LOCALISER.msg("AppEngine.Transaction.Committed", currentTxn.getInnerTxn().getId())); } currentTxn = null; } else { throw new XAException(LOCALISER.msg("AppEngine.Transaction.CommitInvalid")); } }
@Test public void testCommitTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); waitOnFuture(tx.commitAsync()); } catch (Exception e) { waitOnFuture(tx.rollbackAsync()); throw e; } if (key != null) { Assert.assertNotNull(getSingleEntity(service, key)); } }