/** * Attempt to reverse changes of last commit since not all transactions successfully committed. * @param restoreList List of database managers to reverse the last commit * @param cause cause to add any suppressed exceptions */ private void reverseTransactions(ArrayList<DataStore> restoreList, Throwable cause, RequestScope requestScope) { for (DataStore dataStore : restoreList) { try (DataStoreTransaction transaction = dataStore.beginTransaction()) { List<Object> list = dirtyObjects.get(dataStore); for (Object dirtyObject : list == null ? Collections.emptyList() : list) { Object cloned = clonedObjects.get(dirtyObject); if (cloned == NEWLY_CREATED_OBJECT) { transaction.delete(dirtyObject, requestScope); } else { transaction.save(cloned, requestScope); } } transaction.commit(requestScope); } catch (RuntimeException | IOException e) { cause.addSuppressed(e); } } }
@Override public User accessUser(Object opaqueUser) { User user = new User(opaqueUser); for (DataStore dataStore : multiplexManager.dataStores) { DataStoreTransaction transaction = transactions.get(dataStore); user = transaction.accessUser(user.getOpaqueUser()); } return user; }
public void saveOrCreateObjects() { dirtyResources.removeAll(newPersistentResources); // Delete has already been called on these objects dirtyResources.removeAll(deletedResources); newPersistentResources .stream() .map(PersistentResource::getObject) .forEach(s -> transaction.createObject(s, this)); dirtyResources.stream().map(PersistentResource::getObject).forEach(obj -> transaction.save(obj, this)); }
boolean isVerbose = false; try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) { final User user = tx.accessUser(getUser.apply(securityContext)); GraphQLRequestScope requestScope = new GraphQLRequestScope(tx, user, elide.getElideSettings()); isVerbose = requestScope.getPermissionExecutor().isVerbose(); tx.preCommit(); requestScope.runQueuedPreSecurityTriggers(); requestScope.getPermissionExecutor().executeCommitChecks(); tx.flush(requestScope); tx.commit(requestScope); requestScope.runQueuedPostCommitTriggers();
@Test public void accessParentBean() throws IOException { DataStoreTransaction tx = dataStore.beginTransaction(); Parent parent = new Parent(); parent.setChildren(new HashSet<>()); parent.setSpouses(new HashSet<>()); tx.createObject(parent, null); tx.commit(null); tx.close(); } }
@Test (dataProvider = "like_queries_hql") public void testSpecialCharacterLikeQueryHQL(FilterPredicate filterPredicate, int noOfRecords) throws Exception { DataStoreTransaction tx = dataStore.beginReadTransaction(); RequestScope scope = mock(RequestScope.class); EntityDictionary dictionary = new EntityDictionary(new HashMap<>()); dictionary.bindEntity(Book.class); when(scope.getDictionary()).thenReturn(dictionary); Pagination pagination = mock(Pagination.class); when(pagination.isGenerateTotals()).thenReturn(true); tx.loadObjects(Book.class, Optional.of(filterPredicate), Optional.empty(), Optional.of(pagination), scope); tx.commit(scope); tx.close(); verify(pagination).setPageTotals(noOfRecords); }
@Override public void commit(RequestScope scope) { // flush all before commit flush(scope); transactions.values().forEach(dataStoreTransaction -> dataStoreTransaction.commit(scope)); }
@Override public Iterable<Object> loadObjects( Class<?> entityClass, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { return getTransaction(entityClass).loadObjects(entityClass, filterExpression, sorting, pagination, scope); }
@Override public void flush(RequestScope requestScope) { transactions.values().forEach(dataStoreTransaction -> dataStoreTransaction.flush(requestScope)); }
@SuppressWarnings("resource") @Override public void createObject(Object entity, RequestScope scope) { DataStoreTransaction transaction = getTransaction(entity.getClass()); transaction.createObject(entity, scope); // mark this object as newly created to be deleted on reverse transaction clonedObjects.put(entity, NEWLY_CREATED_OBJECT); }
@Override public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression, RequestScope scope) { return getTransaction(entityClass).loadObject(entityClass, id, filterExpression, scope); }
@Override public void delete(Object entity, RequestScope requestScope) { getTransaction(entity).delete(entity, requestScope); dirtyObjects.add(this.multiplexManager.getSubManager(entity.getClass()), entity); }
val = transaction.getRelation(transaction, obj, relationName, Optional.empty(), sorting, Optional.empty(), requestScope); val = transaction.getRelation(transaction, obj, relationName, computedFilters, sorting, computedPagination, requestScope);
@Override public void close() throws IOException { IOException cause = null; for (DataStoreTransaction transaction : transactions.values()) { try { transaction.close(); } catch (IOException | Error | RuntimeException e) { if (cause != null) { cause.addSuppressed(e); } else if (e instanceof IOException) { cause = (IOException) e; } else { cause = new IOException(e); } } } transactions.clear(); if (cause != null) { throw cause; } }
boolean isVerbose = false; try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) { final User user = tx.accessUser(getUser.apply(securityContext)); GraphQLRequestScope requestScope = new GraphQLRequestScope(tx, user, elide.getElideSettings()); isVerbose = requestScope.getPermissionExecutor().isVerbose(); tx.preCommit(); requestScope.runQueuedPreSecurityTriggers(); requestScope.getPermissionExecutor().executeCommitChecks(); tx.flush(requestScope); tx.commit(requestScope); requestScope.runQueuedPostCommitTriggers();
@BeforeClass public static void setup() throws IOException { DataStoreTransaction tx = dataStore.beginTransaction(); Embedded embedded = new Embedded(); // id 1 embedded.setSegmentIds(ImmutableSet.of(3L, 4L, 5L)); tx.createObject(embedded, null); Left left = new Left(); Right right = new Right(); left.setOne2one(right); right.setOne2one(left); tx.createObject(left, null); tx.createObject(right, null); tx.commit(null); tx.close(); }
@Override public void commit(RequestScope scope) { // flush all before commits flush(scope); ArrayList<DataStore> commitList = new ArrayList<>(); for (Entry<DataStore, DataStoreTransaction> entry : transactions.entrySet()) { try { entry.getValue().commit(scope); commitList.add(entry.getKey()); } catch (HttpStatusException | WebApplicationException e) { reverseTransactions(commitList, e, scope); throw e; } catch (Error | RuntimeException e) { TransactionException transactionException = new TransactionException(e); reverseTransactions(commitList, transactionException, scope); throw transactionException; } } }
@Override public Iterable<Object> loadObjects( Class<?> entityClass, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { DataStoreTransaction transaction = getTransaction(entityClass); return hold(transaction, transaction.loadObjects(entityClass, filterExpression, sorting, pagination, scope)); }
requestScope.getTransaction().flush(requestScope);
@Override public void createObject(Object entity, RequestScope scope) { getTransaction(entity).createObject(entity, scope); }