Skip to content

Commit

Permalink
Update SessionConfig.withDatabase documentation (#1518)
Browse files Browse the repository at this point in the history
* Update SessionConfig.withDatabase documentation (#1334)

* Update SessionConfig.withDatabase documentation (addition) (#1336)

* Further update to SessionConfig.withDatabase documentation (#1337)
  • Loading branch information
injectives authored Dec 19, 2023
1 parent 4770155 commit 757de68
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions driver/src/main/java/org/neo4j/driver/SessionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,40 @@ public Builder withDefaultAccessMode(AccessMode mode) {
}

/**
* Set the database that the newly created session is going to connect to.
* Sets target database name for queries executed within session.
* <p>
* For connecting to servers that support multi-databases,
* it is highly recommended to always set the database explicitly in the {@link SessionConfig} for each session.
* If the database name is not set, then session defaults to connecting to the default database configured in server configuration.
* This option has no explicit value by default, as such it is recommended to set a value if the target database
* is known in advance. This has the benefit of ensuring a consistent target database name throughout the
* session in a straightforward way and potentially simplifies driver logic, which reduces network communication
* and might result in better performance.
* <p>
* For servers that do not support multi-databases, leave this database value unset. The only database will be used instead.
* Cypher clauses such as USE are not a replacement for this option as Cypher is handled by the server and not
* the driver.
* <p>
* When no explicit name is set, the driver behavior depends on the connection URI scheme supplied to the driver
* on instantiation and Bolt protocol version.
* <p>
* Specifically, the following applies:
* <ul>
* <li><b>bolt schemes</b> - queries are dispatched to the server for execution without explicit database name
* supplied, meaning that the target database name for query execution is determined by the server. It is
* important to note that the target database may change (even within the same session), for instance if the
* user's home database is changed on the server.</li>
* <li><b>neo4j schemes</b> - providing that Bolt protocol version 4.4, which was introduced with Neo4j server
* 4.4, or above is available, the driver fetches the user's home database name from the server on first query
* execution within the session and uses the fetched database name explicitly for all queries executed within
* the session. This ensures that the database name remains consistent within the given session. For instance,
* if the user's home database name is 'movies' and the server supplies it to the driver upon database name
* fetching for the session, all queries within that session are executed with the explicit database name
* 'movies' supplied. Any change to the user’s home database is reflected only in sessions created after such
* change takes effect. This behavior requires additional network communication. In clustered environments, it
* is strongly recommended to avoid a single point of failure. For instance, by ensuring that the connection URI
* resolves to multiple endpoints. For older Bolt protocol versions the behavior is the same as described for
* the <b>bolt</b> schemes above.</li>
* </ul>
*
* @param database the database the session going to connect to. Provided value should not be {@code null}.
* @return this builder.
* @param database the target database name, must not be {@code null}
* @return this builder
*/
public Builder withDatabase(String database) {
requireNonNull(database, "Database name should not be null.");
Expand Down

0 comments on commit 757de68

Please sign in to comment.