为什么neo4j的GraphDatabaseService.index()在事务外调用时会引发异常?

时间:2013-10-17 13:25:31

标签: scala neo4j

我对Neo4j很新。我正在使用版本2.0.0-M05

如果GraphDatabaseService.index()在事务之外被调用,为什么会抛出异常?我可以在没有交易的情况下使其工作吗?

这是我的示例代码

val graphDb = new GraphDatabaseFactory().
  newEmbeddedDatabaseBuilder("test_db").
  setConfig(GraphDatabaseSettings.node_keys_indexable, "nodeProp1,nodeProp2").
  setConfig(GraphDatabaseSettings.relationship_keys_indexable, "relProp1,relProp2").
  setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
  setConfig(GraphDatabaseSettings.relationship_auto_indexing, "true").
  newGraphDatabase()

val userIndex = graphDb.index().forNodes("user")

导致异常

Exception in thread "main" org.neo4j.graphdb.NotInTransactionException
    at org.neo4j.kernel.impl.transaction.AbstractTransactionManager.assertInTransaction(AbstractTransactionManager.java:108)
    at org.neo4j.kernel.IndexManagerImpl.assertInTransaction(IndexManagerImpl.java:465)
    at org.neo4j.kernel.IndexManagerImpl.forNodes(IndexManagerImpl.java:300)
    at org.neo4j.kernel.IndexManagerImpl.forNodes(IndexManagerImpl.java:293)
    at Test$.main(Test.scala:77)
    at Test.main(Test.scala)

PS。它不会发生在1.9.RC1

1 个答案:

答案 0 :(得分:3)

看起来像是设计的。

Previously it was considered good practice to wrap read operations inside a transaction. The 2.0.0-M04 release makes this recommendation mandatory. Any attempt to execute operations on the database outside of a transaction will now throw a NotInTransactionException. Enforcing this idiom allows Neo4j to reclaim resources much more efficiently leading to a database that will handle much more load. Cypher and the REST-API open their own transactions, so this will only affect users of the embedded Java-API.