• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java TransactionOptions类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.google.appengine.api.datastore.TransactionOptions的典型用法代码示例。如果您正苦于以下问题:Java TransactionOptions类的具体用法?Java TransactionOptions怎么用?Java TransactionOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



TransactionOptions类属于com.google.appengine.api.datastore包,在下文中一共展示了TransactionOptions类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: crossGroupTransactions

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void crossGroupTransactions() throws Exception {
  // [START cross-group_XG_transactions_using_the_Java_low-level_API]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  TransactionOptions options = TransactionOptions.Builder.withXG(true);
  Transaction txn = datastore.beginTransaction(options);

  Entity a = new Entity("A");
  a.setProperty("a", 22);
  datastore.put(txn, a);

  Entity b = new Entity("B");
  b.setProperty("b", 11);
  datastore.put(txn, b);

  txn.commit();
  // [END cross-group_XG_transactions_using_the_Java_low-level_API]
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:19,代码来源:TransactionsTest.java


示例2: testTransactionRollback

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void testTransactionRollback() throws Exception {
    clearData(kindName);
    clearData(otherKind);
    GroupParentKeys keys = writeMultipleGroup(true);
    List<Entity> es = readMultipleGroup(keys);

    TransactionOptions tos = TransactionOptions.Builder.withXG(true);
    Transaction tx = service.beginTransaction(tos);
    es.get(0).setProperty("check", "parent-update");
    es.get(1).setProperty("check", "other-update");
    service.put(tx, es);
    tx.rollback();
    es = readMultipleGroup(keys);
    assertEquals("parent", es.get(0).getProperty("check"));
    assertEquals("other", es.get(1).getProperty("check"));
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:18,代码来源:TransactionTest.java


示例3: testCommitTx

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@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));
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:19,代码来源:AsyncTest.java


示例4: testRollbackTx

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@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));
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:17,代码来源:AsyncTest.java


示例5: putTempData

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
public static Key putTempData(TempData data) {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true));
    try {
        Class<? extends TempData> type = data.getClass();
        String kind = getKind(type);
        Entity entity = new Entity(kind);
        for (Map.Entry<String, Object> entry : data.toProperties(ds).entrySet()) {
            entity.setProperty(entry.getKey(), entry.getValue());
        }
        entity.setProperty(TEMP_DATA_READ_PROPERTY, false);
        data.prePut(ds);
        Key key = ds.put(txn, entity);
        data.postPut(ds);
        txn.commit();
        return key;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:25,代码来源:TestBase.java


示例6: deleteTempDataInTx

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
protected static void deleteTempDataInTx(DatastoreService ds, Entity entity, Class<? extends TempData> type) {
    Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true));
    try {
        TempData data = type.newInstance();
        data.fromProperties(entity.getProperties());
        data.preDelete(ds);
        ds.delete(txn, entity.getKey());
        data.postDelete(ds);
        txn.commit();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:18,代码来源:TestBase.java


示例7: beginTransaction

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
private CheckedTransaction beginTransaction(final TransactionOptions options)
    throws PermanentFailure, RetryableFailure {
  return safeRun(new Evaluater<CheckedTransaction>() {
    @Override public CheckedTransaction run() {
      Transaction rawTransaction = datastore.beginTransaction(options);
      // NOTE(ohler): Calling rawTransaction.getId() forces TransactionImpl to
      // wait for the result of the beginTransaction RPC.  We do this here to
      // get the DatastoreTimeoutException (in the case of a timeout) right
      // away rather than at some surprising time later in
      // TransactionImpl.toString() or similar.
      //
      // Hopefully, TransactionImpl will be fixed to eliminate the need for
      // this.
      try {
        rawTransaction.getId();
      } catch (DatastoreTimeoutException e) {
        // We don't log transaction itself because I'm worried its toString()
        // might fail.  TODO(ohler): confirm this.
        log.log(Level.WARNING, "Failed to begin transaction", e);
        // Now we need to roll back the transaction (even though it doesn't
        // actually exist), otherwise TransactionCleanupFilter will try to
        // roll it back, which is bad because it's not prepared for the crash
        // that we catch below.
        try {
          rawTransaction.rollback();
          throw new Error("Rollback of nonexistent transaction did not fail");
        } catch (DatastoreTimeoutException e2) {
          log.log(Level.INFO, "Rollback of nonexistent transaction failed as expected", e2);
        }
        throw e;
      }
      CheckedTransaction checkedTransaction = new CheckedTransactionImpl(rawTransaction);
      log.info("Begun transaction " + checkedTransaction);
      return checkedTransaction;
    }
  });
}
 
开发者ID:ArloJamesBarnes,项目名称:walkaround,代码行数:38,代码来源:CheckedDatastore.java


示例8: run

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Override
public void run(
    DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) {
  Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true));
  if (ds.put(new Entity("xgfoo")).getId() == 0) {
    throw new RuntimeException("first entity should have received an id");
  }
  if (ds.put(new Entity("xgfoo")).getId() == 0) {
    throw new RuntimeException("second entity should have received an id");
  }
  txn.commit();
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-java-vm-runtime,代码行数:13,代码来源:RemoteApiSharedTests.java


示例9: beginX

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Override
public TransactionDriver beginX() {
    logger.finer("begin X");
    if (!environment.isProduction()) {
        return this;
    }

    TransactionOptions options = TransactionOptions.Builder.withXG(true);
    tx = datastore().beginTransaction(options);
    logger.finer("done");
    return this;
}
 
开发者ID:feroult,项目名称:yawp,代码行数:13,代码来源:AppengineTransationDriver.java


示例10: tryStoreBlobMetadata

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
/**
 * Stores metadata if this is a new blob or existing blob owned by this user.
 *
 * @param bucketName Google Cloud Storage bucket for this blob.
 * @param objectPath path to the object in the bucket.
 * @param accessMode controls how the blob can be accessed.
 * @param ownerId    the id of the owner.
 * @return true if metadata was stored; false if the blob already exists but has a different
 * owner.
 */
public static boolean tryStoreBlobMetadata(
  String bucketName, String objectPath, BlobAccessMode accessMode, String ownerId) {

  Transaction tx = dataStore.beginTransaction(TransactionOptions.Builder.withXG(true));
  try {
    BlobMetadata metadata = getBlobMetadata(bucketName, objectPath);

    if (metadata != null) {
      if (!ownerId.equalsIgnoreCase(metadata.getOwnerId())) {
        // Object exists and is owned by a different owner.
        return false;
      } else if (accessMode == metadata.getAccessMode()) {
        // The new metadata is the same as the existing one. No need to update anything.
        return true;
      }
    }

    metadata =
      new BlobMetadata(getCanonicalizedResource(bucketName, objectPath), accessMode, ownerId);
    dataStore.put(metadata.getEntity());
    tx.commit();
    return true;
  } catch (ConcurrentModificationException e) {
    return false;
  } finally {
    if (tx != null && tx.isActive()) {
      tx.rollback();
    }
  }
}
 
开发者ID:googlesamples,项目名称:io2014-codelabs,代码行数:41,代码来源:BlobManager.java


示例11: tryStoreBlobMetadata

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
/**
 * Stores metadata if this is a new blob or existing blob owned by this user.
 *
 * @param bucketName Google Cloud Storage bucket for this blob.
 * @param objectPath path to the object in the bucket.
 * @param accessMode controls how the blob can be accessed.
 * @param ownerId the id of the owner.
 * @return true if metadata was stored; false if the blob already exists but has a different
 *         owner.
 */
public static boolean tryStoreBlobMetadata(
    String bucketName, String objectPath, BlobAccessMode accessMode, String ownerId) {

  Transaction tx = dataStore.beginTransaction(TransactionOptions.Builder.withXG(true));
  try {
    BlobMetadata metadata = getBlobMetadata(bucketName, objectPath);

    if (metadata != null) {
      if (!ownerId.equalsIgnoreCase(metadata.getOwnerId())) {
        // Object exists and is owned by a different owner.
        return false;
      } else if (accessMode == metadata.getAccessMode()) {
        // The new metadata is the same as the existing one. No need to update anything.
        return true;
      }
    }

    metadata =
        new BlobMetadata(getCanonicalizedResource(bucketName, objectPath), accessMode, ownerId);
    dataStore.put(metadata.getEntity());
    tx.commit();
    return true;
  } catch (ConcurrentModificationException e) {
    return false;
  } finally {
    if (tx != null && tx.isActive()) {
      tx.rollback();
    }
  }
}
 
开发者ID:googlearchive,项目名称:solutions-mobile-backend-starter-java,代码行数:41,代码来源:BlobManager.java


示例12: testTransactionOptions

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void testTransactionOptions() {
    TransactionOptions tos = TransactionOptions.Builder.withXG(true);
    assertEquals(true, tos.isXG());
    tos.clearXG();
    assertEquals(false, tos.isXG());
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:8,代码来源:TransactionTest.java


示例13: testAllowMultipleGroupFalseWithNs

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test(expected = IllegalArgumentException.class)
public void testAllowMultipleGroupFalseWithNs() throws Exception {
    NamespaceManager.set("");
    clearData(kindName);
    NamespaceManager.set("trns");
    try {
        clearData(kindName);
        TransactionOptions tos = TransactionOptions.Builder.withXG(false);
        Transaction tx = service.beginTransaction(tos);
        try {
            List<Entity> es = new ArrayList<>();
            NamespaceManager.set("");
            Entity ens1 = new Entity(kindName);
            ens1.setProperty("check", "entity-nons");
            ens1.setProperty("stamp", new Date());
            es.add(ens1);

            NamespaceManager.set("trns");
            Entity ens2 = new Entity(kindName);
            ens2.setProperty("check", "entity-trns");
            ens2.setProperty("stamp", new Date());
            es.add(ens2);
            service.put(tx, es);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e;
        }
    } finally {
        NamespaceManager.set("");
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:33,代码来源:TransactionTest.java


示例14: testAllowMultipleGroupTrueWithNs

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void testAllowMultipleGroupTrueWithNs() throws InterruptedException {
    NamespaceManager.set("");
    clearData(kindName);
    NamespaceManager.set("trns");
    clearData(kindName);
    List<Entity> es = new ArrayList<>();
    TransactionOptions tos = TransactionOptions.Builder.withXG(true);
    Transaction tx = service.beginTransaction(tos);

    NamespaceManager.set("");
    Entity ens1 = new Entity(kindName);
    ens1.setProperty("check", "entity-nons");
    ens1.setProperty("stamp", new Date());
    es.add(ens1);

    NamespaceManager.set("trns");
    Entity ens2 = new Entity(kindName);
    ens2.setProperty("check", "entity-trns");
    ens2.setProperty("stamp", new Date());
    es.add(ens2);
    service.put(tx, es);
    tx.commit();

    sync(sleepTime);

    NamespaceManager.set("");
    Query q = new Query(kindName);
    Entity e = service.prepare(q).asSingleEntity();
    assertEquals("entity-nons", e.getProperty("check"));
    NamespaceManager.set("trns");
    q = new Query(kindName);
    e = service.prepare(q).asSingleEntity();
    assertEquals("entity-trns", e.getProperty("check"));
    NamespaceManager.set("");
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:37,代码来源:TransactionTest.java


示例15: writeMultipleGroup

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
private GroupParentKeys writeMultipleGroup(boolean allow) throws Exception {

        GroupParentKeys keys = new GroupParentKeys();

        TransactionOptions tos = TransactionOptions.Builder.withXG(allow);
        Transaction tx = service.beginTransaction(tos);
        try {
            Entity parent = new Entity(kindName);
            parent.setProperty("check", "parent");
            parent.setProperty("stamp", new Date());
            keys.firstParent = service.put(tx, parent);

            Entity other = new Entity(otherKind);
            other.setProperty("check", "other");
            other.setProperty("stamp", new Date());
            keys.secondParent = service.put(tx, other);
            tx.commit();

            sync(sleepTime);
        } catch (Exception e) {
            tx.rollback();
            throw e;
        }
        sync(sleepTime);

        return keys;
    }
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:28,代码来源:TransactionTest.java


示例16: writeMultipleInList

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
private GroupParentKeys writeMultipleInList(boolean allow) throws Exception {

        GroupParentKeys keys = new GroupParentKeys();

        List<Entity> es = new ArrayList<>();
        TransactionOptions tos = TransactionOptions.Builder.withXG(allow);
        Transaction tx = service.beginTransaction(tos);
        try {
            Entity parent = new Entity(kindName);
            parent.setProperty("check", "parent");
            parent.setProperty("stamp", new Date());
            es.add(parent);
            keys.firstParent = parent.getKey();

            Entity other = new Entity(otherKind);
            other.setProperty("check", "other");
            other.setProperty("stamp", new Date());
            es.add(other);
            keys.secondParent = other.getKey();
            service.put(tx, es);
            tx.commit();

            sync(sleepTime);
        } catch (Exception e) {
            tx.rollback();
            throw e;
        }
        sync(sleepTime);
        return keys;
    }
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:31,代码来源:TransactionTest.java


示例17: testXGTransaction

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void testXGTransaction() throws Exception {

    final int N = 25; // max XG entity groups

    List<Key> keys = new ArrayList<>();
    for (int i = 0; i < N + 1; i++) {
        keys.add(service.put(new Entity("XG")));
    }

    boolean ok = false;
    Transaction tx = service.beginTransaction(TransactionOptions.Builder.withXG(true));
    try {
        for (int i = 0; i < N; i++) {
            service.get(keys.get(i));
        }

        try {
            service.get(keys.get(N));
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // pass
        }
        ok = true;
    } finally {
        if (ok) {
            tx.commit();
        } else {
            tx.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:33,代码来源:TransactionsTest.java


示例18: testBeginTx

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
@Test
public void testBeginTx() throws Exception {
    final AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService();

    Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true)));
    Key key, key2;
    try {
        key = waitOnFuture(service.put(tx, new Entity("AsyncTx")));
        key2 = waitOnFuture(service.put(tx, new Entity("AsyncTx")));
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        throw e;
    }

    if (key != null && key2 != null) {
        tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true)));
        try {
            try {
                try {
                    Assert.assertNotNull(waitOnFuture(service.get(tx, key)));
                    Assert.assertNotNull(waitOnFuture(service.get(tx, Collections.singleton(key2))));
                } finally {
                    service.delete(tx, key2);
                }
            } finally {
                service.delete(tx, Collections.singleton(key));
            }
        } finally {
            tx.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:34,代码来源:AsyncTest.java


示例19: service

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String entityGroup = req.getParameter("eg");
    String counter = req.getParameter("c");
    String parent = req.getParameter("p");
    boolean xg = Boolean.parseBoolean(req.getParameter("xg"));

    Key parentKey = "2".equals(parent) ? ROOT_2.getKey() : ROOT_1.getKey();

    Entity entity = new Entity(entityGroup, parentKey);
    entity.setProperty("foo", RANDOM.nextInt());

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    final Transaction tx = ds.beginTransaction(TransactionOptions.Builder.withXG(xg));
    try {
        log.warning("Before put ... " + counter);
        putEntity(ds, entity);
        log.warning("After put ... " + counter);
        tx.commit();
        resp.getWriter().write("OK" + counter);
    } catch (Exception e) {
        log.warning("Error ... " + e);
        tx.rollback();
        resp.getWriter().write("ERROR" + counter + ":" + e.getClass().getName());
        error(counter);
    } finally {
        cleanup(counter);
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:29,代码来源:ConcurrentTxServlet.java


示例20: sendInvitation

import com.google.appengine.api.datastore.TransactionOptions; //导入依赖的package包/类
/**
 * Atomically create game entity, invitation entity and a task to deliver the invitation
 * notification.
 *
 * @param senderEntity the Player entity that wants to invite another player.
 * @param invitee the Player entity of the invited player.
 * @param board the board to use for the new game
 * @return Invitation resource representing the created invitation.
 */
private Invitation sendInvitation(
    PlayerEntity senderEntity, PlayerEntity invitee, BoardEntity board) {
  Transaction transaction = dataStore.beginTransaction(TransactionOptions.Builder.withXG(true));

  try {
    GameEntity game = new GameEntity(board.getKey());
    dataStore.put(game.getEntity());

    InvitationEntity invitation =
        new InvitationEntity(senderEntity.getKey(), invitee.getKey(), game.getKey());
    dataStore.put(transaction, invitation.getEntity());

    String invitationText = String.format(
        "%s has invited you to play a game of Griddler", senderEntity.getNickname());

    TaskOptions taskOptions = TaskOptions.Builder.withUrl("/admin/push/invitation")
        .param("InvitationId", String.valueOf(invitation.getId()))
        .param("MessageText", invitationText) 
        .param("GameId", String.valueOf(game.getId())).method(TaskOptions.Method.POST);

    invitationQueue.add(taskOptions);

    transaction.commit();

    return new Invitation(
        invitation.getKey().getId(), game.getKey().getId(), Invitation.Status.SENT);
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:solutions-griddler-sample-backend-java,代码行数:42,代码来源:InvitationService.java



注:本文中的com.google.appengine.api.datastore.TransactionOptions类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ScheduledExecutorScheduler类代码示例发布时间:2022-05-21
下一篇:
Java ServerCnxn类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap