$npx -y skills add neo4j-contrib/neo4j-skills --skill neo4j-driver-java-skillNeo4j Java Driver v6 — driver lifecycle, Maven/Gradle setup, executableQuery,
| 1 | ## When to Use |
| 2 | - Java/Kotlin code connecting to Neo4j (Aura or self-managed) |
| 3 | - Setting up driver, sessions, transactions in Maven/Gradle projects |
| 4 | - Debugging result handling, error recovery, connection pool issues |
| 5 | - Async (`CompletableFuture`) or reactive (Project Reactor / RxJava) Neo4j access |
| 6 | |
| 7 | ## When NOT to Use |
| 8 | - **Cypher query authoring/optimization** → `neo4j-cypher-skill` |
| 9 | - **Driver version upgrades** → `neo4j-migration-skill` |
| 10 | - **Spring Data Neo4j** (`@Node`, `@Relationship`, `Neo4jRepository`) → `neo4j-spring-data-skill` |
| 11 | |
| 12 | --- |
| 13 | |
| 14 | ## Dependency |
| 15 | |
| 16 | ### Maven |
| 17 | ```xml |
| 18 | <dependency> |
| 19 | <groupId>org.neo4j.driver</groupId> |
| 20 | <artifactId>neo4j-java-driver</artifactId> |
| 21 | <version>6.2.0</version> |
| 22 | </dependency> |
| 23 | ``` |
| 24 | |
| 25 | ### Gradle |
| 26 | ```groovy |
| 27 | implementation 'org.neo4j.driver:neo4j-java-driver:6.2.0' |
| 28 | ``` |
| 29 | |
| 30 | Check latest: https://central.sonatype.com/artifact/org.neo4j.driver/neo4j-java-driver |
| 31 | |
| 32 | 6.2.0 [2026-06]: Neo4j `UUID` type + Bolt 6.1 support; `QueryProfile` in result summary. |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## Environment Variables |
| 37 | |
| 38 | Standard pattern for connection config — never hardcode credentials: |
| 39 | |
| 40 | ```java |
| 41 | String uri = System.getenv().getOrDefault("NEO4J_URI", "neo4j://localhost:7687"); |
| 42 | String user = System.getenv().getOrDefault("NEO4J_USERNAME", "neo4j"); |
| 43 | String password = System.getenv().getOrDefault("NEO4J_PASSWORD", ""); |
| 44 | String database = System.getenv().getOrDefault("NEO4J_DATABASE", "neo4j"); |
| 45 | ``` |
| 46 | |
| 47 | Spring Boot: inject via `@Value("${spring.neo4j.uri}")` or `application.properties`: |
| 48 | ```properties |
| 49 | spring.neo4j.uri=neo4j+s://xxx.databases.neo4j.io |
| 50 | spring.neo4j.authentication.username=neo4j |
| 51 | spring.neo4j.authentication.password=secret |
| 52 | ``` |
| 53 | |
| 54 | --- |
| 55 | |
| 56 | ## Driver Lifecycle |
| 57 | |
| 58 | One `Driver` per application — thread-safe, expensive to create. Implement `AutoCloseable` or use try-with-resources. |
| 59 | |
| 60 | ```java |
| 61 | // Long-lived singleton |
| 62 | var driver = GraphDatabase.driver( |
| 63 | "neo4j+s://xxx.databases.neo4j.io", // Aura TLS+routing |
| 64 | AuthTokens.basic(user, password)); |
| 65 | driver.verifyConnectivity(); // fail fast |
| 66 | |
| 67 | // Short-lived (tests / CLI) |
| 68 | try (var driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password))) { |
| 69 | driver.verifyConnectivity(); |
| 70 | // ... |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | URI schemes: |
| 75 | |
| 76 | | URI | Use | |
| 77 | |---|---| |
| 78 | | `neo4j://localhost` | Unencrypted, cluster routing | |
| 79 | | `neo4j+s://xxx.databases.neo4j.io` | TLS + cluster routing (Aura) | |
| 80 | | `bolt://localhost:7687` | Unencrypted, single instance | |
| 81 | | `bolt+s://localhost:7687` | TLS, single instance | |
| 82 | |
| 83 | Auth options: `AuthTokens.basic(u,p)` · `AuthTokens.bearer(token)` · `AuthTokens.kerberos(b64)` · `AuthTokens.none()` |
| 84 | |
| 85 | --- |
| 86 | |
| 87 | ## Choosing the Right API |
| 88 | |
| 89 | | API | When | Auto-retry | Streaming | |
| 90 | |---|---|:---:|:---:| |
| 91 | | `driver.executableQuery()` | Default for most queries | ✅ | ❌ eager | |
| 92 | | `session.executeRead/Write()` | Large results, callback control | ✅ | ✅ | |
| 93 | | `session.beginTransaction()` | Multi-method, external coordination | ❌ | ✅ | |
| 94 | | `session.run()` | Self-managing queries (`CALL IN TRANSACTIONS`) | ⚠️ one-shot [6.1+] | ✅ | |
| 95 | | `driver.asyncSession()` | Non-blocking `CompletableFuture` | ✅ | ✅ | |
| 96 | | `driver.rxSession()` | Reactor/RxJava backpressure | ✅ | ✅ | |
| 97 | |
| 98 | `CALL { … } IN TRANSACTIONS` and `USING PERIODIC COMMIT` self-manage their transaction — use `session.run()` only. `executableQuery` and `executeRead/Write` will fail for these queries. |
| 99 | |
| 100 | `session.run()` retry [6.1+]: single immediate retry on idempotent errors only (enabled by default). Disable per driver or per session: |
| 101 | |
| 102 | ```java |
| 103 | // Driver-level — disable for all sessions |
| 104 | var config = Config.builder().withAutoCommitRetriesDisabled(true).build(); |
| 105 | |
| 106 | // Session-level — overrides driver |
| 107 | var sessionConfig = SessionConfig.builder() |
| 108 | .withAutoCommitRetriesMode(AutoCommitRetriesMode.DISABLED) // DEFAULT = follow driver |
| 109 | .build(); |
| 110 | ``` |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ## `executableQuery` — Default |
| 115 | |
| 116 | ```java |
| 117 | // Read — route to replicas |
| 118 | var result = driver.executableQuery(""" |
| 119 | MATCH (p:Person {name: $name})-[:KNOWS]->(friend) |
| 120 | RETURN friend.name AS name |
| 121 | """) |
| 122 | .withParameters(Map.of("name", "Alice")) |
| 123 | .withConfig(QueryConfig.builder() |
| 124 | .withDatabase("neo4j") // always specify — avoids home-db round-trip |
| 125 | .withRouting(RoutingControl.READ) |
| 126 | .build()) |
| 127 | .execute(); |