javax.jcr.Session.refresh()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(217)

本文整理了Java中javax.jcr.Session.refresh()方法的一些代码示例,展示了Session.refresh()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Session.refresh()方法的具体详情如下:
包路径:javax.jcr.Session
类名称:Session
方法名:refresh

Session.refresh介绍

[英]If keepChanges is false, this method discards all pending changes currently recorded in this Session and returns all items to reflect the current saved state. Outside a transaction this state is simply the current state of persistent storage. Within a transaction, this state will reflect persistent storage as modified by changes that have been saved but not yet committed.

If keepChanges is true then pending change are not discarded but items that do not have changes pending have their state refreshed to reflect the current saved state, thus revealing changes made by other sessions.
[中]如果keepChangesfalse,此方法将丢弃当前在此Session中记录的所有挂起更改,并返回所有项目以反映当前保存的状态。在事务外部,该状态只是持久存储的当前状态。在事务中,此状态将反映已保存但尚未提交的更改所修改的持久存储。
如果keepChanges为真,则不会放弃挂起的更改,但没有挂起更改的项目会刷新其状态以反映当前保存的状态,从而显示其他会话所做的更改。

代码示例

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

/**
 * Set the autoexport:enabled property to false in the repository. This method should only be called in an exception
 * handler, as it assumes the current session state is garbage and aggressively clears it.
 * @throws RepositoryException
 */
private void disableAutoExportJcrProperty() throws RepositoryException {
  // this method is almost certainly being called while current session state is somehow unreliable
  // therefore, before attempting to work with the repo, we should attempt to reset to a good state
  eventProcessorSession.refresh(false);
  final Property autoExportEnableProperty = autoExportConfigNode.getProperty(AutoExportConstants.CONFIG_ENABLED_PROPERTY_NAME);
  autoExportEnableProperty.setValue(false);
  eventProcessorSession.save();
}

代码示例来源:origin: apache/jackrabbit-oak

public void run() {
  try {
    for(int i=0; i<NODES_PER_THREAD; i++) {
      session.refresh(false);
      int level1 = random.nextInt(PARENT_COUNT);
      Node level1Node = session.getNode("/"+ROOT_NODE_NAME+"/testNode/level1_"+level1);
      String randomName = UUID.randomUUID().toString();
      Node newNode = level1Node.addNode(randomName);
      newNode.setProperty("indexedProperty", randomName);
      session.save();
    }
  } catch (RepositoryException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: ModeShape/modeshape

protected void addContentToCustomer( Session session,
                   String pathToCustomer,
                   String childName ) throws Exception {
  // Add a child under the node ...
  session.refresh(false);
  Node customer = session.getNode(pathToCustomer);
  Node deck = customer.addNode(childName, "acme:DeckClassType");
  assertThat(deck, is(notNullValue()));
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public void run() {
  try {
    int numNodes = NODE_COUNT.get();
    long time = System.currentTimeMillis();
    session.refresh(false);
    Node root = session.getNode(path);
    Node node = root.addNode("node" + count++);
    for (int j = 0; j < NODE_COUNT_LEVEL2; j++) {
      node.addNode("node" + j);
      session.save();
      NODE_COUNT.incrementAndGet();
    }
    numNodes = NODE_COUNT.get() - numNodes;
    time = System.currentTimeMillis() - time;
    if (this == writer && VERBOSE) {
      long perSecond = numNodes * 1000 / time;
      System.out.println("Created " + numNodes + " in " + time + " ms. (" + perSecond + " nodes/sec)");
    }
  } catch (RepositoryException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.onehippo.ecm.hst/hst-content-beans

/**
 * Saves all pending changes. 
 * @throws ObjectBeanPersistenceException
 */
public void save() throws ObjectBeanPersistenceException {
  try {
    session.save();
    // also do a refresh, because it is possible that through workflow another jcr session made the changes, and that the current
    // has no changes, hence a session.save() does not trigger a refresh
    session.refresh(false); 
  } catch (Exception e) {
    throw new ObjectBeanPersistenceException(e);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

private void testName(String name) throws RepositoryException {
    Exception exception = null;
    try {
      session.getRootNode().addNode(name);
    } catch (RepositoryException e) {
      exception = e;
    } finally {
      session.refresh(false);
    }

    session.setNamespacePrefix("foo", "http://foo.bar");
    try {
      session.getRootNode().addNode(name);
      assertNull("name = " + name, exception);
    } catch (RepositoryException e) {
      assertNotNull("name = " + name, exception);
    }
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * Move a node in a shared set.
 */
public void testMoveShareableNode() throws Exception {
  // setup parent nodes and first children
  Node a1 = testRootNode.addNode("a1");
  Node a2 = testRootNode.addNode("a2");
  Node b = a1.addNode("b");
  testRootNode.getSession().save();
  // add mixin
  ensureMixinType(b, mixShareable);
  b.getSession().save();
  // move
  Workspace workspace = b.getSession().getWorkspace();
  // move shareable node
  String newPath = a2.getPath() + "/b";
  workspace.move(b.getPath(), newPath);
  // move was performed using the workspace, so refresh the session
  b.getSession().refresh(false);
  assertEquals(newPath, b.getPath());
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public void run() {
  try {
    parent.getSession().refresh(false);
    Node file = JcrUtils.putFile(
        parent, "file-" + counter++,
        "application/octet-stream",
        new TestInputStream(FILE_SIZE * 1024));
    parent.getSession().save();
    addPath(file.getPath());
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: apache/jackrabbit-oak

/**
 * @since oak
 */
@Test
public void testRegisterPrivilegeWithPendingChanges() throws RepositoryException {
  try {
    session.getRootNode().addNode("test");
    assertTrue(session.hasPendingChanges());
    privilegeManager.registerPrivilege("new", true, new String[0]);
    fail("Privileges may not be registered while there are pending changes.");
  } catch (InvalidItemStateException e) {
    // success
  } finally {
    superuser.refresh(false);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public void beforeSuite() throws RepositoryException {
  session = getRepository().login(getCredentials());
  try {
    ensurePropertyIndex();
  } catch (InvalidItemStateException e) {
    // some other oak instance probably created the same
    // index definition concurrently. refresh and try again
    // do not catch exception if it fails again.
    session.refresh(false);
    ensurePropertyIndex();
  }
  root = session.getRootNode().addNode("testroot" + TEST_ID, "nt:unstructured");
  for (int i = 0; i < NODE_COUNT; i++) {
    Node node = root.addNode("node" + i, "nt:unstructured");
    for (int j = 0; j < NODE_COUNT; j++) {
      Node child = node.addNode("node" + j, "nt:unstructured");
      child.setProperty("testcount", j);
    }
    session.save();
  }
}

代码示例来源:origin: com.cognifide.qa.bb/bb-aem-common

/**
 * Removes node property.
 *
 * @param nodePath     Absolute path to node.
 * @param propertyName Property name.
 * @throws RepositoryException if problem with jcr repository occurred
 */
public void removeNodeProperty(String nodePath, String propertyName) throws RepositoryException {
 LOG.debug("Removing property '{}' from node '{}'", propertyName, nodePath);
 session.refresh(true);
 session.getNode(nodePath).getProperty(propertyName).setValue((String) null);
 session.save();
}

代码示例来源:origin: ModeShape/modeshape

private void testAddNode( Session session ) throws Exception {
  session.refresh(false);
  Node root = getTestRoot(session);
  root.addNode(nodeName1, testNodeType);
  session.save();
}

代码示例来源:origin: apache/jackrabbit

public void testRemoveItem4() throws RepositoryException {
  try {
    readOnlySession.refresh(false); // see JCR-3302
    readOnlySession.removeItem(nPath);
    readOnlySession.save();
    fail("A read-only session must not be allowed to remove an item");
  } catch (AccessDeniedException e) {
    // success
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * Tries to register a namespace.
 */
public void testRegisterNamespace() throws RepositoryException {
  nsp.registerNamespace(namespacePrefix, namespaceUri);
  assertEquals("Namespace prefix was not registered.", namespacePrefix, nsp.getPrefix(namespaceUri));
  assertEquals("Namespace URI was not registered.", namespaceUri, nsp.getURI(namespacePrefix));
  Item created;
  try {
    created = testRootNode.addNode(namespacePrefix + ":root");
    testRootNode.getSession().save();
  }
  catch (RepositoryException ex) {
    // that didn't work; maybe the repository allows a property here?
    testRootNode.getSession().refresh(false);
    created = testRootNode.setProperty(namespacePrefix + ":root", "test");
    testRootNode.getSession().save();
  }
  // Need to remove it here, otherwise teardown can't unregister the NS.
  testRootNode.getSession().getItem(created.getPath()).remove();
  testRootNode.getSession().save();
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

private void moveToHistory(final Node node) {
  try {
    // the updater node was modified externally by the executor
    session.refresh(false);
    final String srcPath = node.getPath();
    final Node history = session.getNode(UPDATE_HISTORY_PATH);
    String name = node.getName();
    int count = 2;
    while (history.hasNode(name)) {
      name = node.getName() + "-" + count++;
    }
    final String destPath = UPDATE_HISTORY_PATH + "/" + name;
    session.move(srcPath, destPath);
    session.save();
  } catch (RepositoryException e) {
    log.error("Failed to remove updater from queue", e);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public void beforeSuite() throws RepositoryException {
  session = getRepository().login(getCredentials());
  try {
    ensurePropertyIndexes();
  } catch (InvalidItemStateException e) {
    // some other oak instance probably created the same
    // index definition concurrently. refresh and try again
    // do not catch exception if it fails again.
    session.refresh(false);
    ensurePropertyIndexes();
  }
  root = session.getRootNode().addNode("testroot" + TEST_ID, "nt:unstructured");
  for (int i = 0; i < NODE_COUNT; i++) {
    Node node = root.addNode("node" + i, "nt:unstructured");
    node.setProperty("jcr:uuid", createUUID(i));
    session.save();
  }
  String lookupMode = lookupByQuery ? "query" : "Session#getNodeByIdentifier";
  System.out.printf("No of indexes (%s) %d, Lookup by (%s)[%s] %n",noOfIndex, noOfIndex, "lookupByQuery", lookupMode);
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

private void markExecuted(final ModuleRegistration registration) {
    final String moduleName = registration.getModuleName();
    final String nodePath = MODULES_PATH + "/" + moduleName;
    try {
      final Node node = session.getNode(nodePath);
      node.setProperty(HippoNodeType.HIPPO_EXECUTED, Calendar.getInstance());
      session.save();
    } catch (RepositoryException e) {
      log.error("Failed to mark module {} as executed", moduleName);
      try {
        session.refresh(false);
      } catch (RepositoryException e1) {
        log.error("Failed to refresh session", e1);
      }
    }
  }
}

代码示例来源:origin: apache/jackrabbit

public void testSetRetentionPolicyBelow() throws RepositoryException, NotExecutableException {
  Node childN = testRootNode.addNode(nodeName2);
  superuser.save();
  try {
    retentionMgr.setRetentionPolicy(testNodePath, getApplicableRetentionPolicy());
    retentionMgr.setRetentionPolicy(childN.getPath(), getApplicableRetentionPolicy());
    superuser.save();
  } finally {
    superuser.refresh(false);
    if (retentionMgr.getRetentionPolicy(testNodePath) != null) {
      retentionMgr.removeRetentionPolicy(testNodePath);
    }
    if (retentionMgr.getRetentionPolicy(childN.getPath()) != null) {
      retentionMgr.removeRetentionPolicy(childN.getPath());
    }
    superuser.save();
  }
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-testutils

@Override
protected Node doExecute(Node node) throws Exception {
  GalleryWorkflow workflow = getGalleryWorkflow(node);
  String nodeName = "asset";
  do {
    nodeName +=  random.nextInt(10);
  } while (node.hasNode(nodeName));
  Document document = workflow.createGalleryItem(nodeName, "hippogallery:exampleAssetSet");
  node.getSession().refresh(false);
  Node assetNode = document.getNode(node.getSession());
  InputStream istream = getClass().getClassLoader().getResourceAsStream("org/onehippo/repository/concurrent/action/Hippo.pdf");
  makeImage(assetNode, istream, "application/pdf");
  node.getSession().save();
  return assetNode;
}

代码示例来源:origin: apache/jackrabbit-oak

private static void ensureIndex(Node root, String propertyName)
    throws RepositoryException {
  Node indexDef = root.getNode(IndexConstants.INDEX_DEFINITIONS_NAME);
  if (indexDef.hasNode(propertyName)) {
    return;
  }
  Node index = indexDef.addNode(propertyName, IndexConstants.INDEX_DEFINITIONS_NODE_TYPE);
  index.setProperty(IndexConstants.TYPE_PROPERTY_NAME,
      PropertyIndexEditorProvider.TYPE);
  index.setProperty(IndexConstants.REINDEX_PROPERTY_NAME,
      true);
  index.setProperty(IndexConstants.PROPERTY_NAMES,
      new String[] { propertyName }, PropertyType.NAME);
  try {
    root.getSession().save();
  } catch (RepositoryException e) {
    // created by other thread -> ignore
    root.getSession().refresh(false);
  }
}

相关文章

微信公众号

最新文章

更多