Spring Data Neo4j在访问已检索对象时会出现NotInTransactionException

时间:2014-12-03 17:10:34

标签: neo4j spring-data-neo4j

我刚刚在Spring环境中将项目从SDN2更新到SDN3,并进行了必要的更改,例如强制传统的简单索引和Indexed TypeRepresentationStrategy。

上下文启动很好但是给出了以下代码......

@RequestMapping({"/customer/browse"})
public String customerBrowse(final ModelMap model) {
    populateBaseModel(model);
    Result<Customer> result = this.customerService.findAll();
    for(Customer c : result) {
        LOG.info("Customer: {}", c.getName());
    }
    model.addAttribute("allCustomer", result);
    return "pages/browse";
}

... findAll()存储库调用运行完美,而仅仅访问检索到的Customer对象for(Customer c : result)会触发NotInTransactionException(见下文)。 Customer类有几个旧的索引注释,但没有传出引用。对我来说,不明白为什么对成功检索到的对象的简单访问会遇到NotInTransactionException。我正在以简单模式运行SDN,并假设foreach正在分离的Customer对象上运行。

org.neo4j.graphdb.NotInTransactionException
at org.neo4j.index.impl.lucene.LuceneIndex.assertInTransaction(LuceneIndex.java:631)
at org.neo4j.index.impl.lucene.LuceneIndex.access$000(LuceneIndex.java:58)
at org.neo4j.index.impl.lucene.LuceneIndex$1.hasNext(LuceneIndex.java:359)
at org.neo4j.helpers.collection.IteratorWrapper.hasNext(IteratorWrapper.java:42)
at org.neo4j.helpers.collection.IteratorWrapper.hasNext(IteratorWrapper.java:42)
at com.myapp.controller.CustomerController.customerBrowse(CustomerController.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1448)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:160)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:75)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at com.github.dandelion.core.web.DandelionFilter.doFilter(DandelionFilter.java:138)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:198)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:176)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1075)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:368)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:488)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:932)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:994)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:744)

异常前的跟踪输出:

| org.springframework.web.method.HandlerMethod            | Invoking [CustomerController.customerBrowse] method with arguments [{}] 
| o.s.d.r.c.s.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource | Adding transactional method 'findAll' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 
| o.s.beans.factory.support.DefaultListableBeanFactory    | Returning cached instance of singleton bean 'neo4jTransactionManager' 
| o.springframework.transaction.jta.JtaTransactionManager | Creating new transaction with name [org.springframework.data.neo4j.repository.NodeGraphRepositoryImpl.findAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 
| o.s.t.support.TransactionSynchronizationManager         | Initializing transaction synchronization 
| o.s.transaction.interceptor.TransactionInterceptor      | Getting transaction for [org.springframework.data.neo4j.repository.NodeGraphRepositoryImpl.findAll] 
| o.s.transaction.interceptor.TransactionInterceptor      | Completing transaction for [org.springframework.data.neo4j.repository.NodeGraphRepositoryImpl.findAll] 
| o.springframework.transaction.jta.JtaTransactionManager | Triggering beforeCommit synchronization 
| o.springframework.transaction.jta.JtaTransactionManager | Triggering beforeCompletion synchronization 
| o.springframework.transaction.jta.JtaTransactionManager | Initiating transaction commit 
| o.springframework.transaction.jta.JtaTransactionManager | Triggering afterCommit synchronization 
| o.springframework.transaction.jta.JtaTransactionManager | Triggering afterCompletion synchronization 
| o.s.t.support.TransactionSynchronizationManager         | Clearing transaction synchronization

交易范围由下面的服务层处理:

  @Service
  @Transactional
  public class CustomerService implements AssetService, ApplicationContextAware {

      @Autowired
      private CustomerRepository customerRepository;

          ...

          public Result<Customer> findAll() {
            return this.customerRepository.findAll();
          }

          ...
  }

SDN手册讨论了一些自动交易的方法。从日志中可以看出,这与findAll()调用一起正常工作,但是这仍然保留了为什么检索对象的访问失败的原因。

如果有人能指出我在这里进行故障排除的正确方向,我将非常感激。

1 个答案:

答案 0 :(得分:2)

在迭代结果时我得到了相同的Exception,在阅读了几条评论之后我就这样排序了:

@Repository
public class OrderDAO implements DaoOperations<Order> {

@Autowired
private Neo4jOperations neo4j;
@Autowired
private GraphDatabase gd;

public List<Order> findAll() {

    List<Order> list = new ArrayList<Order>();

    try (Transaction tx = gd.beginTx()) { 
        Result<Order>res = neo4j.findAll(Order.class);
        list =IteratorUtil.asList(res);
    } 

    return list;
  }
}

请注意,方法neo4j.findAll()最初返回一个Result,我将其转换为事务中的List 并解决了问题,现在我可以遍历List而不会抛出任何异常在我的服务/控制器中。希望这会有所帮助。