Here are the examples of the java api class com.google.appengine.api.datastore.Transaction taken from open source projects.
1. MyEndpoint#clearTasks()
Project: endpoints-codelab-android
File: MyEndpoint.java
File: MyEndpoint.java
@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(); } } }
2. MyEndpoint#storeTask()
Project: endpoints-codelab-android
File: MyEndpoint.java
File: MyEndpoint.java
@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(); } } }
3. BlobManager#tryStoreBlobMetadata()
Project: solutions-mobile-backend-starter-java
File: BlobManager.java
File: BlobManager.java
/** * 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(); } } }
4. EvilMemcacheBugTests#testRawCaching()
Project: objectify
File: EvilMemcacheBugTests.java
File: EvilMemcacheBugTests.java
/** */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testRawCaching() throws Exception { // I can not for the life of me figure out why this test passes when the // previous test fails. MemcacheService cs1 = MemcacheServiceFactory.getMemcacheService("blah"); com.google.appengine.api.datastore.Key parentKey = KeyFactory.createKey("SimpleParent", "asdf"); com.google.appengine.api.datastore.Key childKey = KeyFactory.createKey(parentKey, "SimpleEntity", "asdf"); Entity ent = new Entity(childKey); ent.setProperty("foo", "original"); cs1.put(childKey, ent); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); ds.put(ent); Transaction txn = ds.beginTransaction(); try { Entity ent2 = ds.get(txn, childKey); //Entity ent2 = (Entity)cs1.get(childKey); assert ent2.getProperty("foo").equals("original"); ent2.setProperty("foo", "changed"); Map<Object, Object> holder = new HashMap<>(); holder.put(childKey, ent2); cs1.putAll(holder); Map<Object, Object> fetched = cs1.getAll((Collection) Collections.singleton(childKey)); Entity ent3 = (Entity) fetched.get(childKey); assert ent3.getProperty("foo").equals("changed"); } finally { if (txn.isActive()) txn.rollback(); } }
5. EvilMemcacheBugTests#testRawTransactionalCaching()
Project: objectify
File: EvilMemcacheBugTests.java
File: EvilMemcacheBugTests.java
// /** */ // @Test // public void testMoreSophisticatedInAndOutOfTransaction() throws Exception { // fact().register(SimpleParent.class); // fact().register(SimpleEntity.class); // String simpleId = "btoc"; // // Key<SimpleEntity> childKey = SimpleEntity.getSimpleChildKey(simpleId); // SimpleEntity simple = new SimpleEntity(simpleId); // // TestObjectify nonTxnOfy = fact().begin(); // nonTxnOfy.put(simple); // // TestObjectify txnOfy = fact().begin().startTransaction(); // SimpleEntity simple2; // try { // simple2 = txnOfy.get(childKey); // simple2.foo = "joe"; // txnOfy.put(simple2); // txnOfy.getTransaction().commit(); // } finally { // if (txnOfy.getTransaction().isActive()) // txnOfy.getTransaction().rollback(); // } // // nonTxnOfy.clear(); // SimpleEntity simple3 = nonTxnOfy.get(childKey); // // assert simple2.foo.equals(simple3.foo); // } /** */ @Test public void testRawTransactionalCaching() throws Exception { // Need to register it so the entity kind becomes cacheable fact().register(SimpleEntity.class); DatastoreService ds = TestObjectifyService.ds(); DatastoreService cacheds = CachingDatastoreServiceFactory.getDatastoreService(); // This is the weirdest thing. If you change the *name* of one of these two keys, the test passes. // If the keys have the same *name*, the test fails because ent3 has the "original" property. WTF?? com.google.appengine.api.datastore.Key parentKey = KeyFactory.createKey("SimpleParent", "asdf"); com.google.appengine.api.datastore.Key childKey = KeyFactory.createKey(parentKey, "SimpleEntity", "asdf"); Entity ent1 = new Entity(childKey); ent1.setProperty("foo", "original"); cacheds.put(ent1); // Weirdly, this will solve the problem too //MemcacheService cs = MemcacheServiceFactory.getMemcacheService(); //cs.clearAll(); Transaction txn = cacheds.beginTransaction(); Entity ent2; try { ent2 = ds.get(txn, childKey); //ent2 = new Entity(childKey); // solves the problem ent2.setProperty("foo", "changed"); cacheds.put(txn, ent2); txn.commit(); } finally { if (txn.isActive()) txn.rollback(); } Entity ent3 = cacheds.get(childKey); assert "changed".equals(ent3.getProperty("foo")); }
6. LoadEngine#fetch()
Project: objectify
File: LoadEngine.java
File: LoadEngine.java
/** * Fetch the keys from the async datastore using the current transaction context */ public Result<Map<com.google.appengine.api.datastore.Key, Entity>> fetch(Set<com.google.appengine.api.datastore.Key> keys) { Transaction txn = (ofy.getTransaction() == null) ? null : ofy.getTransaction().getRaw(); Future<Map<com.google.appengine.api.datastore.Key, Entity>> fut = ads.get(txn, keys); return ResultAdapter.create(fut); }
7. BlobManager#tryStoreBlobMetadata()
Project: io2014-codelabs
File: BlobManager.java
File: BlobManager.java
/** * 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(); } } }