0

我对 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.

4

1 回答 1

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.

于 2013-10-17T14:44:10.423 回答