使用Result时Spring Data Neo4j NotInTransactionException

时间:2015-07-03 08:43:46

标签: spring-data-neo4j spring-transactions

我正在使用spring数据neo4j为iOS应用程序开发服务器端应用程序。

在处理从存储库的查询方法返回的Result时,我一直收到NotInTransactionException。这是代码:

@SuppressWarnings({ "unchecked", "rawtypes" })
@Path("/{startIndex}/{pageSize}/{mood}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response fetchByMood(@PathParam("startIndex") int startIndex,
        @PathParam("pageSize") int pageSize, @PathParam("mood") String mood)
        throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("startIndex", startIndex);
    params.put("pageSize", pageSize);
    params.put("mood", mood);

    String query = "";
    if (mood.equals("all"))
        query = "match (d: Diary) return d skip {startIndex} limit {pageSize}";
    else
        query = "match (d: Diary {mood: {mood}}) return d skip {startIndex} limit {pageSize}";

    Result<Diary> diaries = repository.query(query, params);
    Result<String> jsonReps = diaries.to(String.class,
            new ResultConverter<Diary, String>() {

                @Override
                public String convert(Diary diary, Class<String> type) {
                    // TODO Auto-generated method stub
                    return diary.toJsonRepre();
                }

                @Override
                public String convert(Diary diary, Class<String> type,
                        MappingPolicy policy) {
                    // TODO Auto-generated method stub
                    return diary.toJsonRepre();
                }

            });
    List results = jsonReps.as(List.class);
    return Response.ok(new GenericEntity<List>(results) {
    }).build();
}

以下是异常消息:

org.neo4j.graphdb.NotInTransactionException
at org.neo4j.kernel.impl.persistence.PersistenceManager.getCurrentTransaction(PersistenceManager.java:297)
......
at com.bond.pamela.service.DiaryService.fetchByMood(DiaryService.java:117)
......
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:403)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
......

当我使用Result对象的as方法时发生错误。更奇怪的是,当我使用Page而不是Result时,不会出现任何异常。以下是使用Page:

的代码
@Path("/{page}/{size}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response fetch(@PathParam("page") int page,
        @PathParam("size") int size) throws Exception {
    Pageable pageable = new PageRequest(page, size);
    Page<Diary> diaries = repository.findAll(pageable);
    Page<String> jsonReps = diaries.map(new Converter<Diary, String>() {

        @Override
        public String convert(Diary diary) {
            // TODO Auto-generated method stub
            return diary.toJsonRepre();
        }

    });
    return Response.ok(
            new GenericEntity<List<String>>(jsonReps.getContent()) {
            }).build();
}

这是我的Spring XML配置文件:

enter image description here

有人知道如何修复它吗?

1 个答案:

答案 0 :(得分:0)

灵感来自@MichaelHunger在这篇文章中的回答Can not explore database created by an embedded neo4j 我手动控制事务而不是使用Spring Transaction。像这样:

try (Transaction tx = db.beginTx()) {
        Diary diary = (Diary) DiaryFactory.getInstance().create(parameters);
        repository.save(diary);
        tx.success();
        return Response.ok().build();
    }

通过这样做,没有异常发生,并且一切正常。我仍然不知道为什么Spring Transaction不起作用。由于我是春天的新手,我不知道答案,但如果我弄明白,我会更新我的答案。