2014-01-14 50 views
1

我正在写一个非托管扩展,并且在使用JAVA API访问索引时遇到了问题。Neo4j索引不能使用Java API

代码:

package org.neo4j.parent.parentextension; 

import org.codehaus.jackson.map.ObjectMapper; 
import org.neo4j.cypher.javacompat.ExecutionEngine; 
import org.neo4j.cypher.javacompat.ExecutionResult; 
import org.neo4j.graphdb.GraphDatabaseService; 
import org.neo4j.graphdb.index.Index; 
import org.neo4j.graphdb.index.IndexManager; 
import org.neo4j.graphdb.Node; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.Response; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.util.Map; 

@Path("/parent") 
public class ParentDistance { 

@GET 
@Path("/helloworld") 
public String helloWorld() { 
    return "Hello World!"; 
} 

@GET 
@Path("/common/{acc1}/{acc2}") 
public String getCommon(@PathParam("acc1") String acc1, @PathParam("acc2") String acc2, @Context GraphDatabaseService db) throws IOException { 

    return db.index().nodeIndexNames().toString(); 

} 



} 

的HelloWorld电话不工作以及其他的是执行的Cypher查询方法。但是,只要有任何方法调用任何IndexManager或索引,下面的任何内容都将停止工作。任何暗示要寻找什么?

谢谢!

回答

1

你是什么意思的“停止工作”。

日志中的任何异常?

使用Neo4j 2.0,您必须在事务中包装读取操作。

像这样:

@GET 
@Path("/common/{acc1}/{acc2}") 
public String getCommon(@PathParam("acc1") String acc1, @PathParam("acc2") String acc2, @Context GraphDatabaseService db) throws IOException { 
    try (Transaction tx = db.beginTx()) { 
     String result = db.index().nodeIndexNames().toString(); 
     tx.success(); 
    } 
} 
+0

它的工作原理!非常感谢! –