当前位置: 首页>>代码示例>>Java>>正文


Java Transaction.commit方法代码示例

本文整理汇总了Java中com.google.appengine.api.datastore.Transaction.commit方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.commit方法的具体用法?Java Transaction.commit怎么用?Java Transaction.commit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.appengine.api.datastore.Transaction的用法示例。


在下文中一共展示了Transaction.commit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: deleteSessionVariables

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
/**
 * Delete a value stored in the project's datastore.
 * @param sessionId Request from which the session is extracted.
 */
protected void deleteSessionVariables(String sessionId, String... varNames) {
  if (sessionId.equals("")) {
    return;
  }
  Key key = KeyFactory.createKey(SESSION_KIND, sessionId);
  Transaction transaction = datastore.beginTransaction();
  try {
    Entity stateEntity = datastore.get(transaction, key);
    for (String varName : varNames) {
      stateEntity.removeProperty(varName);
    }
    datastore.put(transaction, stateEntity);
    transaction.commit();
  } catch (EntityNotFoundException e) {
    // Ignore - if there's no session, there's nothing to delete.
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:getting-started-java,代码行数:26,代码来源:DatastoreSessionFilter.java

示例2: deleteSessionWithValue

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.beginTransaction();
  try {
    Query query = new Query(SESSION_KIND)
        .setFilter(new FilterPredicate(varName, FilterOperator.EQUAL, varValue));
    Iterator<Entity> results = datastore.prepare(transaction, query).asIterator();
    while (results.hasNext()) {
      Entity stateEntity = results.next();
      datastore.delete(transaction, stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:getting-started-java,代码行数:18,代码来源:DatastoreSessionFilter.java

示例3: creatingAnEntityInASpecificEntityGroup

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@Test
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
  String boardName = "my-message-board";

  //CHECKSTYLE.OFF: VariableDeclarationUsageDistance - Increased clarity in sample
  // [START creating_an_entity_in_a_specific_entity_group]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  String messageTitle = "Some Title";
  String messageText = "Some message.";
  Date postDate = new Date();

  Transaction txn = datastore.beginTransaction();

  Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);

  Entity message = new Entity("Message", messageBoardKey);
  message.setProperty("message_title", messageTitle);
  message.setProperty("message_text", messageText);
  message.setProperty("post_date", postDate);
  datastore.put(txn, message);

  txn.commit();
  // [END creating_an_entity_in_a_specific_entity_group]
  //CHECKSTYLE.ON: VariableDeclarationUsageDistance
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:27,代码来源:TransactionsTest.java

示例4: creatingAnEntityInASpecificEntityGroup

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@Test
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
  String boardName = "my-message-board";

  // [START creating_an_entity_in_a_specific_entity_group]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  String messageTitle = "Some Title";
  String messageText = "Some message.";
  Date postDate = new Date();

  Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);

  Entity message = new Entity("Message", messageBoardKey);
  message.setProperty("message_title", messageTitle);
  message.setProperty("message_text", messageText);
  message.setProperty("post_date", postDate);

  Transaction txn = datastore.beginTransaction();
  datastore.put(txn, message);

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

示例5: fetchOrCreate

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
private Entity fetchOrCreate(String boardName) {
  // [START uses_for_transactions_2]
  Transaction txn = datastore.beginTransaction();
  Entity messageBoard;
  Key boardKey;
  try {
    boardKey = KeyFactory.createKey("MessageBoard", boardName);
    messageBoard = datastore.get(boardKey);
  } catch (EntityNotFoundException e) {
    messageBoard = new Entity("MessageBoard", boardName);
    messageBoard.setProperty("count", 0L);
    boardKey = datastore.put(txn, messageBoard);
  }
  txn.commit();
  // [END uses_for_transactions_2]

  return messageBoard;
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:19,代码来源:TransactionsTest.java

示例6: crossGroupTransactions

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的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

示例7: storeTask

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@ApiMethod(name = "storeTask")
public void storeTask(TaskBean taskBean) {
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
    Transaction txn = datastoreService.beginTransaction();
    try {
        Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
        Entity taskEntity = new Entity("TaskBean", taskBean.getId(), taskBeanParentKey);
        taskEntity.setProperty("data", taskBean.getData());
        datastoreService.put(taskEntity);
        txn.commit();
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:endpoints-codelab-android,代码行数:17,代码来源:MyEndpoint.java

示例8: clearTasks

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@ApiMethod(name = "clearTasks")
public void clearTasks() {
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
    Transaction txn = datastoreService.beginTransaction();
    try {
        Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
        Query query = new Query(taskBeanParentKey);
        List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
        for (Entity result : results) {
            datastoreService.delete(result.getKey());
        }
        txn.commit();
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:endpoints-codelab-android,代码行数:19,代码来源:MyEndpoint.java

示例9: datastoreCount

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
private int datastoreCount() {
  Key key = KeyFactory.createKey(Long.class.getName(), KEY);
  DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
  Transaction tx = ds.beginTransaction();
  Entity entity;
  long value = -1;
  try {
    entity = ds.get(key);
    Long old = ((Long) entity.getProperty(KEY));
    value = old.longValue() + 1;
    entity.setProperty(KEY, Long.valueOf(value));
    ds.put(entity);
  } catch (Exception e) {
    // not found, insert
    entity = new Entity(key);
    value = 1;
    entity.setProperty(KEY, Long.valueOf(value));
    ds.put(entity);
  }
  tx.commit();
  return (int) value;
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-java-vm-runtime,代码行数:23,代码来源:CountServlet.java

示例10: run

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@Override
public void run(
    DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) {
  // Put a fresh entity.
  Entity originalEntity = new Entity(getFreshKindName());
  originalEntity.setProperty("prop1", 75L);
  ds.put(originalEntity);
  Key key = originalEntity.getKey();

  // Prepare a new version of it with a different property value.
  Entity mutatedEntity = new Entity(key);
  mutatedEntity.setProperty("prop1", 76L);

  // Test Get/Put within a transaction.
  Transaction txn = ds.beginTransaction();
  assertGetEquals(ds, key, originalEntity);
  ds.put(mutatedEntity); // Write the mutated Entity.
  assertGetEquals(ds, key, originalEntity); // Within a txn, the put is not yet reflected.
  txn.commit();

  // Now that the txn is committed, the mutated entity will show up in Get.
  assertGetEquals(ds, key, mutatedEntity);
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-java-vm-runtime,代码行数:24,代码来源:RemoteApiSharedTests.java

示例11: deleteObject

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
@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;
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-gcs-client,代码行数:20,代码来源:LocalRawGcsService.java

示例12: register

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
/**
 * Registers a device.
 */
public static void register(String regId) {
    logger.info("Registering " + regId);
    Transaction txn = ds.beginTransaction();
    try {
        Entity entity = findDeviceByRegId(regId);
        if (entity != null) {
            logger.fine(regId + " is already registered; ignoring.");
            return;
        }
        entity = new Entity(DEVICE_TYPE);
        entity.setProperty(DEVICE_REG_ID_PROPERTY, regId);
        ds.put(entity);
        txn.commit();
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
开发者ID:amardeshbd,项目名称:google-iosched,代码行数:23,代码来源:Datastore.java

示例13: unregister

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
/**
 * Unregisters a device.
 *
 * @param regId device's registration id.
 */
public static void unregister(String regId) {
  logger.info("Unregistering " + regId);
  Transaction txn = datastore.beginTransaction();
  try {
    Entity entity = findDeviceByRegId(regId);
    if (entity == null) {
      logger.warning("Device " + regId + " already unregistered");
    } else {
      Key key = entity.getKey();
      datastore.delete(key);
    }
    txn.commit();
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
}
 
开发者ID:army-of-dumbledore,项目名称:pennappsF13,代码行数:24,代码来源:Datastore.java

示例14: updateRegistration

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
/**
 * Updates the registration id of a device.
 */
public static void updateRegistration(String oldId, String newId) {
  logger.info("Updating " + oldId + " to " + newId);
  Transaction txn = datastore.beginTransaction();
  try {
    Entity entity = findDeviceByRegId(oldId);
    if (entity == null) {
      logger.warning("No device for registration id " + oldId);
      return;
    }
    entity.setProperty(DEVICE_REG_ID_PROPERTY, newId);
    datastore.put(entity);
    txn.commit();
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
}
 
开发者ID:army-of-dumbledore,项目名称:pennappsF13,代码行数:22,代码来源:Datastore.java

示例15: getDevices

import com.google.appengine.api.datastore.Transaction; //导入方法依赖的package包/类
/**
 * Gets all registered devices.
 */
public static List<String> getDevices() {
  List<String> devices;
  Transaction txn = datastore.beginTransaction();
  try {
    Query query = new Query(DEVICE_TYPE);
    Iterable<Entity> entities =
        datastore.prepare(query).asIterable(DEFAULT_FETCH_OPTIONS);
    devices = new ArrayList<String>();
    for (Entity entity : entities) {
      String device = (String) entity.getProperty(DEVICE_REG_ID_PROPERTY);
      devices.add(device);
    }
    txn.commit();
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
  return devices;
}
 
开发者ID:army-of-dumbledore,项目名称:pennappsF13,代码行数:24,代码来源:Datastore.java


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