1

I am using NodeJS and the neo4j-driver package. On server startup I need to ensure that a specific database exists in my neo4j cluster. I therefore implemented this method:

export const ensureAdminDatabaseExists = async () => {
  const session = neo4j
    .driver(
      NEO4J_CONNECTION_URL,
      neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
    )
    .session();
  await session.writeTransaction((tx) =>
    tx.run('CREATE DATABASE $dbName IF NOT EXISTS', { dbName: 'admin' })
  );
  await session.close();
};

If no such database exists in Neo4j and I call this method several times in parallel, then at least one of the transactions will fail with the following error message:

Neo4jError: Failed to create the specified database 'admin': Database already exists.

However, if the database exists on startup then no transactions fail. I, therefore, question if the CREATE DATABASE query is transactional.

  • Should be transactional following to [this](https://github.com/neo4j/neo4j/blob/4cd5556a5356c8d0d9efe9c8fb6a8b87865c48ed/community/kernel/src/main/java/org/neo4j/dbms/database/DatabaseManagementServiceImpl.java#L141) – Timur Samkharadze Feb 15 '21 at 12:38

0 Answers0