我对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)
附言。它不会发生在1.9.RC1
.
看起来这是设计使然。
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.