Neo4j "Tried to execute Write query after executing Schema modification" 错误

Neo4j "Tried to execute Write query after executing Schema modification" Error

我正在使用最新版本的 Neo4j 和 Java 驱动程序,但我的代码不断出现以下错误:

Exception in thread "main" org.neo4j.driver.exceptions.ClientException: Tried to execute Write query after executing Schema modification

这是我的代码:

import org.neo4j.driver.*;

public class HelloWorldExample implements AutoCloseable
{
    private final Driver driver;

    public HelloWorldExample( String uri, String user, String password )
    {
        driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
    }

    @Override
    public void close() throws Exception
    {
        driver.close();
    }

    public void printGreeting( final String message )
    {
        try ( Session session = driver.session() )
        {
            String greeting = session.writeTransaction( new TransactionWork<String>()
            {
                @Override
                public String execute( Transaction tx )
                {
                  //ISSUE HERE
                    String test = "CREATE INDEX qwert IF NOT EXISTS FOR (m:Pode) ON (m.made)";
                    Result hello = tx.run(test);
                    String test2 = "CREATE(n:Node)";
                    Result hello2 = tx.run(test2);
                 //ISSUE ABOVE
                    return "";
              //      return result.single().get( 0 ).asString();
                }
            } );
            System.out.println( greeting );
        }
    }

    public static void main( String... args ) throws Exception
    {
        try ( HelloWorldExample greeter = new HelloWorldExample( "bolt://localhost:7687", "neo4j", "Neo4J" ) )
        {
            greeter.printGreeting( "hello, world" );
        }
    }
}

我知道错误发生在“ISSUE HERE”部分,如果我删除任何“CREATE”行,它就会消失,但我需要这两行,但不确定如何继续。感谢您的帮助!

该错误表明您不能在单个事务中组合架构和写入查询。

将两个查询拆分为两个事务。 在第一个事务中定义索引,然后在第二个事务中创建一个节点。