本文整理汇总了Java中com.google.appengine.api.datastore.Transaction类的典型用法代码示例。如果您正苦于以下问题:Java Transaction类的具体用法?Java Transaction怎么用?Java Transaction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Transaction类属于com.google.appengine.api.datastore包,在下文中一共展示了Transaction类的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();
}
}
}
示例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();
}
}
}
示例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
}
示例4: 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]
}
示例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;
}
示例6: 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]
}
示例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();
}
}
}
示例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();
}
}
}
示例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;
}
示例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);
}
示例11: handleAbandonedTxns
import com.google.appengine.api.datastore.Transaction; //导入依赖的package包/类
void handleAbandonedTxns(Collection<Transaction> txns) {
// TODO(user): In the dev appserver, capture a stack trace whenever a
// transaction is started so we can print it here.
for (Transaction txn : txns) {
try {
logger.warning("Request completed without committing or rolling back transaction with id "
+ txn.getId() + ". Transaction will be rolled back.");
txn.rollback();
} catch (Exception e) {
// We swallow exceptions so that there is no risk of our cleanup
// impacting the actual result of the request.
logger.log(Level.SEVERE, "Swallowing an exception we received while trying to rollback "
+ "abandoned transaction with id " + txn.getId(), e);
}
}
}
示例12: 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;
}
示例13: register
import com.google.appengine.api.datastore.Transaction; //导入依赖的package包/类
/**
* Registers a device.
*
* @param regId device's registration id.
*/
public static void register(String regId) {
logger.info("Registering " + regId);
Transaction txn = datastore.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);
datastore.put(entity);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
}
示例14: 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();
}
}
}
示例15: 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();
}
}
}