Java Code Examples for com.google.cloud.datastore.Transaction#rollback()
The following examples show how to use
com.google.cloud.datastore.Transaction#rollback() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskList.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Marks a task entity as done. * * @param id The ID of the task entity as given by {@link Key#id()} * @return true if the task was found, false if not * @throws DatastoreException if the transaction fails */ boolean markDone(long id) { Transaction transaction = datastore.newTransaction(); try { Entity task = transaction.get(keyFactory.newKey(id)); if (task != null) { transaction.put(Entity.newBuilder(task).set("done", true).build()); } transaction.commit(); return task != null; } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 2
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 3
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 4
Source File: GoogleJobStore.java From data-transfer-project with Apache License 2.0 | 6 votes |
@Override public <T extends DataModel> void update(UUID jobId, String key, T model) { Transaction transaction = datastore.newTransaction(); Key entityKey = getDataKey(jobId, key); try { Entity previousEntity = transaction.get(entityKey); if (previousEntity == null) { throw new IOException("Could not find record for data key: " + entityKey.getName()); } String serialized = objectMapper.writeValueAsString(model); Entity entity = Entity.newBuilder(entityKey) .set(CREATED_FIELD, Timestamp.now()) .set(model.getClass().getName(), serialized) .build(); transaction.put(entity); transaction.commit(); } catch (IOException t) { transaction.rollback(); throw new RuntimeException("Failed atomic update of key: " + key, t); } }
Example 5
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
/** * 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.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); if (stateEntity != null) { Entity.Builder builder = Entity.newBuilder(stateEntity); StringBuilder delNames = new StringBuilder(); for (String varName : varNames) { delNames.append(varName + " "); builder = builder.remove(varName); } datastore.update(builder.build()); } } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 6
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
/** * 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.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); if (stateEntity != null) { Entity.Builder builder = Entity.newBuilder(stateEntity); StringBuilder delNames = new StringBuilder(); for (String varName : varNames) { delNames.append(varName + " "); builder = builder.remove(varName); } datastore.update(builder.build()); } } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 7
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 8
Source File: ShardedCounterMetricsStore.java From nexus-blobstore-google-cloud with Eclipse Public License 1.0 | 6 votes |
private Long getCount(String fieldName) { Transaction txn = datastore.newTransaction( TransactionOptions.newBuilder() .setReadOnly(ReadOnly.newBuilder().build()) .build() ); QueryResults<ProjectionEntity> results; try { Query<ProjectionEntity> countQuery = Query.newProjectionEntityQueryBuilder() .setKind(SHARD) .setNamespace(namespace) .setProjection(fieldName) .build(); results = datastore.run(countQuery); return StreamSupport.stream(Spliterators.spliteratorUnknownSize(results, Spliterator.NONNULL), false) .map(entity -> Long.valueOf(entity.getLong(fieldName))) .reduce(0L, (valueA, valueB) -> valueA + valueB); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example 9
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 10
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Stores the state value in each key-value pair in the project's datastore. * @param sessionId Request from which to extract session. * @param varName the name of the desired session variable * @param varValue the value of the desired session variable */ protected void setSessionVariables(String sessionId, Map<String, String> setMap) { if (sessionId.equals("")) { return; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); DateTime dt = DateTime.now(DateTimeZone.UTC); dt.toString(dtf); try { Entity stateEntity = transaction.get(key); Entity.Builder seBuilder; if (stateEntity == null) { seBuilder = Entity.newBuilder(key); } else { seBuilder = Entity.newBuilder(stateEntity); } for (String varName : setMap.keySet()) { seBuilder.set(varName, setMap.get(varName)); } transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build()); transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 11
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Take an HttpServletRequest, and copy all of the current session variables over to it * * @param req Request from which to extract session. * @return a map of strings containing all the session variables loaded or an empty map. */ protected Map<String, String> loadSessionVariables(HttpServletRequest req) throws ServletException { Map<String, String> datastoreMap = new HashMap<>(); String sessionId = getCookieValue(req, "bookshelfSessionId"); if (sessionId.equals("")) { return datastoreMap; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); StringBuilder logNames = new StringBuilder(); if (stateEntity != null) { for (String varName : stateEntity.getNames()) { req.getSession().setAttribute(varName, stateEntity.getString(varName)); datastoreMap.put(varName, stateEntity.getString(varName)); logNames.append(varName + " "); } } } finally { if (transaction.isActive()) { transaction.rollback(); } } return datastoreMap; }
Example 12
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Take an HttpServletRequest, and copy all of the current session variables over to it * * @param req Request from which to extract session. * @return a map of strings containing all the session variables loaded or an empty map. */ protected Map<String, String> loadSessionVariables(HttpServletRequest req) throws ServletException { Map<String, String> datastoreMap = new HashMap<>(); String sessionId = getCookieValue(req, "bookshelfSessionId"); if (sessionId.equals("")) { return datastoreMap; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); StringBuilder logNames = new StringBuilder(); if (stateEntity != null) { for (String varName : stateEntity.getNames()) { req.getSession().setAttribute(varName, stateEntity.getString(varName)); datastoreMap.put(varName, stateEntity.getString(varName)); logNames.append(varName + " "); } } } finally { if (transaction.isActive()) { transaction.rollback(); } } return datastoreMap; }
Example 13
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Stores the state value in each key-value pair in the project's datastore. * * @param sessionId Request from which to extract session. * @param varName the name of the desired session variable * @param varValue the value of the desired session variable */ protected void setSessionVariables(String sessionId, Map<String, String> setMap) { if (sessionId.equals("")) { return; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); DateTime dt = DateTime.now(DateTimeZone.UTC); dt.toString(dtf); try { Entity stateEntity = transaction.get(key); Entity.Builder seBuilder; if (stateEntity == null) { seBuilder = Entity.newBuilder(key); } else { seBuilder = Entity.newBuilder(stateEntity); } for (String varName : setMap.keySet()) { seBuilder.set(varName, setMap.get(varName)); } transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build()); transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 14
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Stores the state value in each key-value pair in the project's datastore. * * @param sessionId Request from which to extract session. * @param varName the name of the desired session variable * @param varValue the value of the desired session variable */ protected void setSessionVariables(String sessionId, Map<String, String> setMap) { if (sessionId.equals("")) { return; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); DateTime dt = DateTime.now(DateTimeZone.UTC); dt.toString(dtf); try { Entity stateEntity = transaction.get(key); Entity.Builder seBuilder; if (stateEntity == null) { seBuilder = Entity.newBuilder(key); } else { seBuilder = Entity.newBuilder(stateEntity); } for (String varName : setMap.keySet()) { seBuilder.set(varName, setMap.get(varName)); } transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build()); transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example 15
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Take an HttpServletRequest, and copy all of the current session variables over to it * @param req Request from which to extract session. * @return a map of strings containing all the session variables loaded or an empty map. */ protected Map<String, String> loadSessionVariables(HttpServletRequest req) throws ServletException { Map<String, String> datastoreMap = new HashMap<>(); String sessionId = getCookieValue(req, "bookshelfSessionId"); if (sessionId.equals("")) { return datastoreMap; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); StringBuilder logNames = new StringBuilder(); if (stateEntity != null) { for (String varName : stateEntity.getNames()) { req.getSession().setAttribute(varName, stateEntity.getString(varName)); datastoreMap.put(varName, stateEntity.getString(varName)); logNames.append(varName + " "); } } } finally { if (transaction.isActive()) { transaction.rollback(); } } return datastoreMap; }
Example 16
Source File: InstrumentedDatastoreTest.java From styx with Apache License 2.0 | 5 votes |
private void testTransaction(Transaction instrumented) { testReaderWriter(instrumented, transaction); // void putWithDeferredIdAllocation(FullEntity<?>... entities); instrumented.putWithDeferredIdAllocation(TEST_ENTITY_1, TEST_ENTITY_2); verify(transaction).putWithDeferredIdAllocation(TEST_ENTITY_1, TEST_ENTITY_2); verify(stats).recordDatastoreEntityWrites(TEST_KIND_1, 1); verify(stats).recordDatastoreEntityWrites(TEST_KIND_2, 1); verifyNoMoreInteractions(stats); reset(stats); // boolean isActive(); when(transaction.isActive()).thenReturn(true); assertThat(instrumented.isActive(), is(true)); verify(transaction).isActive(); // void rollback(); instrumented.rollback(); verify(transaction).rollback(); // Response commit(); instrumented.commit(); verify(transaction).commit(); // Datastore getDatastore(); when(transaction.getDatastore()).thenReturn(datastore); var instrumentedDatastore = instrumented.getDatastore(); assertThat(instrumentedDatastore, instanceOf(InstrumentedDatastore.class)); assertThat(((InstrumentedDatastore) instrumentedDatastore).delegate(), is(datastore)); verify(transaction).getDatastore(); verifyNoMoreInteractions(transaction); reset(transaction); }
Example 17
Source File: DatastoreUtils.java From catatumbo with Apache License 2.0 | 5 votes |
/** * Rolls back the given transaction, if it is still active. * * @param transaction * the transaction to roll back. */ static void rollbackIfActive(Transaction transaction) { try { if (transaction != null && transaction.isActive()) { transaction.rollback(); } } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 18
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testTransactionalSingleEntityGroupReadOnly() { setUpQueryTests(); Key taskListKey = datastore.newKeyFactory().setKind("TaskList").newKey("default"); Entity taskListEntity = Entity.newBuilder(taskListKey).build(); datastore.put(taskListEntity); // [START datastore_transactional_single_entity_group_read_only] Entity taskList; QueryResults<Entity> tasks; Transaction txn = datastore.newTransaction( TransactionOptions.newBuilder() .setReadOnly(ReadOnly.newBuilder().build()) .build() ); try { taskList = txn.get(taskListKey); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter(PropertyFilter.hasAncestor(taskListKey)) .build(); tasks = txn.run(query); txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } // [END datastore_transactional_single_entity_group_read_only] assertEquals(taskListEntity, taskList); assertNotNull(tasks.next()); assertFalse(tasks.hasNext()); }
Example 19
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 5 votes |
/** * Take an HttpServletRequest, and copy all of the current session variables over to it * * @param req Request from which to extract session. * @return a map of strings containing all the session variables loaded or an empty map. */ protected Map<String, String> loadSessionVariables(HttpServletRequest req) throws ServletException { Map<String, String> datastoreMap = new HashMap<>(); String sessionId = getCookieValue(req, "bookshelfSessionId"); if (sessionId.equals("")) { return datastoreMap; } Key key = keyFactory.newKey(sessionId); Transaction transaction = datastore.newTransaction(); try { Entity stateEntity = transaction.get(key); StringBuilder logNames = new StringBuilder(); if (stateEntity != null) { for (String varName : stateEntity.getNames()) { req.getSession().setAttribute(varName, stateEntity.getString(varName)); datastoreMap.put(varName, stateEntity.getString(varName)); logNames.append(varName + " "); } } } finally { if (transaction.isActive()) { transaction.rollback(); } } return datastoreMap; }
Example 20
Source File: ShardedCounterMetricsStore.java From nexus-blobstore-google-cloud with Eclipse Public License 1.0 | 4 votes |
void flush() { if (!pending.isEmpty()) { log.debug("flush started, attempting to acquire permit"); double wait = rateLimiter.acquire(); log.debug("permit acquired after {} seconds", wait); Multimap<String, Mutation> toWrite = ArrayListMultimap.create(); // drain the queue into a Map<shard, list<mutations_for_the_shard>> Mutation queued = pending.poll(); while(queued != null) { toWrite.put(queued.getShard(), queued); queued = pending.poll(); } // merge multimap of mutations into a list of single entities per shard to write as a batch List<FullEntity> list = new ArrayList<>(); for (String shard: toWrite.keySet()) { Collection<Mutation> deltas = toWrite.get(shard); deltas.stream().reduce((deltaA, deltaB) -> new Mutation(shard, deltaA.getSizeDelta() + deltaB.getSizeDelta(), deltaA.getCountDelta() + deltaB.getCountDelta()) ).ifPresent(merged -> { Entity shardCounter = getShardCounter(merged.getShard()); FullEntity<Key> entity = FullEntity.newBuilder(shardCounter.getKey()) .set(SIZE, shardCounter.getLong(SIZE) + merged.getSizeDelta()) .set(COUNT, shardCounter.getLong(COUNT) + merged.getCountDelta()) .build(); list.add(entity); }); } log.debug("sending {} mutations to datastore", list.size()); // write the batch off to datastore if (!list.isEmpty()) { Transaction txn = datastore.newTransaction(); try { txn.put(list.toArray(new FullEntity[list.size()])); txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } log.debug("drained {} mutations to datastore", list.size()); } } }