Search code examples
javadatabaseneo4jdriver

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


I'm using the latest version of Neo4j and the Java driver and keep getting the following error with my code:

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

And here's my code:

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" );
        }
    }
}

I know the error is happening at the "ISSUE HERE" part, and if I remove either of the "CREATE" lines, it goes away, but I need both lines, and not sure how to proceed. Any help is appreciated!


Solution

  • The error indicated that you can't combine a schema and a write query in a single transaction.

    Split the two queries into two transactions. In the first transaction define the index, and then in the second transaction create a node.