@Override public Future<Transaction> beginTransaction() { return new TransactionFutureWrapper(this.rawAsync.beginTransaction()); }
@Override public Future<Transaction> beginTransaction(TransactionOptions options) { return new TransactionFutureWrapper(this.rawAsync.beginTransaction(options)); }
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 this in your factory if you wish to use a different impl, say, * one based on the ObjectifyWrapper. * * @param ds the DatastoreService * @param opts the options for creating this Objectify * @return an instance of Objectify configured appropriately */ protected Objectify createObjectify(AsyncDatastoreService ds, ObjectifyOpts opts) { TransactionOptions txnOpts = opts.getTransactionOptions(); Transaction txn = (txnOpts == null) ? null : FutureHelper.quietGet(ds.beginTransaction(txnOpts)); Objectify ofy = (opts.getSessionCache()) ? new ObjectifyImpl(opts, new SessionCachingAsyncObjectifyImpl(this, ds, txn)) : new ObjectifyImpl(opts, new AsyncObjectifyImpl(this, ds, txn)); return ofy; }
Entity newRec = new Entity(ASYNC_ENTITY); newRec.setProperty("timestamp", new Date()); Transaction trans = asyncService.beginTransaction().get(); Future<Key> firstE = asyncService.put(trans, newRec); trans.rollback(); lisRec.add(newRec); trans = asyncService.beginTransaction().get(); Future<List<Key>> eKeys = asyncService.put(trans, lisRec); trans.commit();
final AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); Key key, key2; try { tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); try { try {
@Test public void testRollbackTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key = null; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); } finally { waitOnFuture(tx.rollbackAsync()); } if (key != null) { Assert.assertNull(getSingleEntity(service, key)); } } }
@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)); } }
@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(); } }