2016-06-15 73 views
0

我正在嘗試爲neo4j 3.0.0編寫一個插件。爲了測試水,我寫了這個非常簡單的方法來公開即將離任的程度。當我嘗試打電話給我的插件時,neo4j說我需要在交易中包裝我的電話

public class Utils extends ServerPlugin { 
    @PluginTarget(value = Node.class) 
    public int outgoingDegree(@Source Node node) { 
     return node.getDegree(Direction.OUTGOING); 
    } 
} 

註冊插件完美地工作:

curl -v http://localhost:7474/db/data/ext/

{ 
    "Utils" : "http://localhost:7474/db/data/ext/Utils" 
} 

我的小方法有:

curl -v http://localhost:7474/db/data/ext/Utils

{ 
    "node" : { 
    "outgoingDegree" : { 
     "extends" : "node", 
     "name" : "outgoingDegree", 
     "description" : "", 
     "parameters" : [ ] 
    } 
    } 
} 

令人驚歎。現在,當我嘗試調用此方法,一切都順心:

url -XPOST -v http://localhost:7474/db/data/ext/Utils/node/20/outgoingDegree -H "Accept: application/json" -H "Content-type: application/json" -d "{ }"

{ 
    "message" : "The requested operation cannot be performed, because it has to be performed in a transaction. Ensure you are wrapping your operation in the appropriate transaction boilerplate and try again.", 
    "exception" : "BridgeNotInTransactionException", 
    "fullname" : "org.neo4j.kernel.impl.core.ThreadToStatementContextBridge$BridgeNotInTransactionException", 
    "stackTrace" : [ "org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.assertInUnterminatedTransaction(ThreadToStatementContextBridge.java:72)", "org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.getTopLevelTransactionBoundToThisThread(ThreadToStatementContextBridge.java:105)", "org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.getKernelTransactionBoundToThisThread(ThreadToStatementContextBridge.java:113)", "org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.get(ThreadToStatementContextBridge.java:65)", "org.neo4j.kernel.impl.factory.ClassicCoreSPI.currentStatement(ClassicCoreSPI.java:205)", "org.neo4j.kernel.impl.factory.GraphDatabaseFacade$$Lambda$76/46581122.get(Unknown Source)", "org.neo4j.kernel.impl.coreapi.StandardNodeActions.statement(StandardNodeActions.java:56)", "org.neo4j.kernel.impl.core.NodeProxy.getDegree(NodeProxy.java:702)", "com.github.denizco.datascience.neo4jplugin.Utils.outgoingDegree(Utils.java:12)", "java.lang.reflect.Method.invoke(Method.java:483)", "org.neo4j.server.plugins.PluginMethod.invoke(PluginMethod.java:61)", "org.neo4j.server.plugins.PluginManager.invoke(PluginManager.java:158)", "org.neo4j.server.rest.web.ExtensionService.invokeNodeExtension(ExtensionService.java:325)", "org.neo4j.server.rest.web.ExtensionService.invokeNodeExtension(ExtensionService.java:184)", "java.lang.reflect.Method.invoke(Method.java:483)", "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:144)", "org.neo4j.server.rest.dbms.AuthorizationFilter.doFilter(AuthorizationFilter.java:121)", "org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)", "java.lang.Thread.run(Thread.java:745)" ], 
    "errors" : [ { 
    "code" : "Neo.ClientError.Request.TransactionRequired", 
    "message" : "The requested operation cannot be performed, because it has to be performed in a transaction. Ensure you are wrapping your operation in the appropriate transaction boilerplate and try again." 
    } ] 
} 

這是怎麼回事?爲什麼我需要在交易中包裝我的電話,我該怎麼做?有沒有解決的辦法?

回答

0

Neo4j中的任何操作(讀取或寫入)都需要一個事務。如果沒有明確管理

所以包裹你的代碼它創建罩下一個交易的唯一例外graphDb.execute(cypherString)

try (Transaction tx=graphDb.beginTx()) { 
    // do your stuff 
    tx.success(); 
} 

但是使用「服務器插件」是相當多的老技術。在Neo4j 3.0中,黃金標準正在使用程序並使用CALL從Cypher訪問它們。

+0

不夠公平,我會使用程序。 – Tillerino

相關問題