1

我正在使用 JCR 1.0,并且在关闭 JCR 连接后重新打开它们时遇到问题。

这是我的两个辅助方法:

private void openConnection() throws Exception {
    loadDbProperties();

    repository = new TransientRepository(repositoryConfig,repositoryHome);
    session = repository.login(new SimpleCredentials("testUserName","testPassword".toCharArray()));

    ws = session.getWorkspace();

    rootNode = session.getRootNode();
}

private void closeConnection() throws Exception {
    session.save();
    session.logout();
    session = null;
    repository = null;
}

我有两个方法叫做 addProperty() 和 getProperty(),它们都需要连接到 JCR 存储库。我将 openConnection() 和 closeConnection() 方法放在 add 和 get 方法的开头,但似乎第二个运行的方法都无法重新打开已经关闭的连接。

这是我得到的例外:

javax.faces.el.EvaluationException: javax.jcr.RepositoryException: this session has been closed
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: javax.jcr.RepositoryException: this session has been closed
    at org.apache.jackrabbit.core.SessionImpl.sanityCheck(SessionImpl.java:340)
    at org.apache.jackrabbit.core.ItemImpl.sanityCheck(ItemImpl.java:154)
    at org.apache.jackrabbit.core.NodeImpl.addNode(NodeImpl.java:1751)
    at pgts.trueMiner.core.dao.TenureDAOImpl.addTenure(TenureDAOImpl.java:63)
    at pgts.trueMiner.core.biz.TenureServiceImpl.addTenure(TenureServiceImpl.java:63)
    at pgts.trueMiner.core.ui.TenureForm.addTenure(TenureForm.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 23 more

你如何重新打开相同的连接?我不想总是保持连接打开,因为锁会阻止其他应用程序使用存储库。

4

1 回答 1

1

看起来您在会话关闭后尝试添加一个节点。我可以使用以下测试用例重现异常:

Repository repository = new TransientRepository();
Session session = repository.login(
        new SimpleCredentials("admin", "admin".toCharArray()));
session.getWorkspace();
Node rootNode = session.getRootNode();
session.save();
session.logout();
rootNode.addNode("test");

使用最新版本的 Jackrabbit(我使用 Jackrabbit 主干),您将获得会话关闭位置的堆栈跟踪:

Exception in thread "main" javax.jcr.RepositoryException: This session has been closed. See the chained exception for a trace of where the session was closed.
  at org.apache.jackrabbit.core.session.SessionState.checkAlive(SessionState.java:121)
  at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:171)
  at org.apache.jackrabbit.core.ItemImpl.perform(ItemImpl.java:91)
  at org.apache.jackrabbit.core.NodeImpl.addNodeWithUuid(NodeImpl.java:1790)
  at org.apache.jackrabbit.core.NodeImpl.addNode(NodeImpl.java:1742)
  at org.apache.jackrabbit.api.Test.main(Test.java:21)
Caused by: java.lang.Exception: Stack trace of  where session-admin-4 was originally closed
  at org.apache.jackrabbit.core.session.SessionState.close(SessionState.java:246)
  at org.apache.jackrabbit.core.SessionImpl.logout(SessionImpl.java:888)
  at org.apache.jackrabbit.core.XASessionImpl.logout(XASessionImpl.java:389)
  at org.apache.jackrabbit.api.Test.main(Test.java:20)
于 2011-02-09T10:42:25.517 回答