本文整理汇总了Java中com.google.appengine.api.datastore.TransactionOptions类的典型用法代码示例。如果您正苦于以下问题:Java TransactionOptions类的具体用法?Java TransactionOptions怎么用?Java TransactionOptions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TransactionOptions类属于com.google.appengine.api.datastore包,在下文中一共展示了TransactionOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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]
}
示例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"));
}
示例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));
}
}
示例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));
}
}
示例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();
}
}
}
示例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();
}
}
}
示例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;
}
});
}
示例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();
}
示例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;
}
示例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();
}
}
}
示例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();
}
}
}
示例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());
}
示例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("");
}
}
示例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("");
}
示例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;
}