Configure Neo4j Connection

You do not need to manage a driver object, but you still need to put the database connection details somewhere for the application. In Spring applications, this is typically handled in the application.properties file. In this lesson, you will learn how to configure the connection details for your Neo4j database and test the connection.

Configure the Connection

The library contains some namespaced properties to use for connecting to a database.

properties
spring.neo4j.uri=neo4j+s://{sandbox_ip}:{sandbox_boltPort}
spring.neo4j.authentication.username={sandbox_username}
spring.neo4j.authentication.password={sandbox_password}
spring.data.neo4j.database=neo4j

Each of the above properties are as follows:

  1. spring.neo4j.uri - the connection URI string for our database. For example, neo4j://localhost:7687 or neo4j+s://<dbhash>.databases.neo4j.io.

  2. spring.neo4j.authentication.username and spring.neo4j.authentication.password - properties to authenticate with the database.

  3. spring.data.neo4j.database - name of the database we want to connect to. This is optional and defaults to neo4j.

Authentication Types

Neo4j supports four authentication methods, basic authentication (e.g. username and password or LDAP), a base64 encoded Bearer token, a Kerberos token, or a custom authentication token. For more information on these options, visit Authentication and Authorization.

Set environment variables

In your project, open the src/main/resources/application.properties file and update the required values.

Open the properties file
spring.neo4j.uri=neo4j+s://{sandbox_host}:{sandbox_boltPort}
spring.neo4j.authentication.username={sandbox_username}
spring.neo4j.authentication.password={sandbox_password}
spring.data.neo4j.database=neo4j

Update the file with your Neo4j sandbox connection details:

NEO4J_URI

neo4j://{sandbox-ip}:{sandbox-boltPort}

NEO4J_USERNAME

{sandbox-username}

NEO4J_PASSWORD

{sandbox-password}

Verifying Connectivity

Run the tests to confirm everything is ok. Running as-is will only result in an error if general syntax is incorrect. However, it will not test whether you can actually connect to the instance or not.

To take it one step further and fully test the connection, open the src/main/test/AppSpringDataApplicationTests file and add the code shown below.

  1. Import org.neo4j.driver.Driver and org.springframework.beans.factory.annotation.Autowired classes.

    java
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.neo4j.driver.Driver;
    import org.springframework.beans.factory.annotation.Autowired;
  2. Inject the driver into the class.

    java
        // Driver injection
        final Driver driver;
  3. Add a new test method that calls the driver’s verifyConnectivity() method.

    java
        // Test connection
        @Test
        final void testConnection() {
            driver.verifyConnectivity();
        }

Completed code is available below to cross-check.

Click to reveal completed AppSpringDataApplicationTests code
java
package com.example.appspringdata;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.neo4j.driver.Driver;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootTest
class AppSpringDataApplicationTests {
	@Test
	void contextLoads() {
	}

	final Driver driver;

	public AppSpringDataApplicationTests(@Autowired Driver driver) {
		this.driver = driver;
	}

	@Test
	final void testConnection() {
		driver.verifyConnectivity();
	}
}

Run the test class in the IDE to verify that the test passes by clicking on the Test Results tab at the bottom of the IDE. If the test doesn’t pass, then the database credentials or connection details are incorrect.

VerifyConnectivity Test Passed

Lesson Summary

In this lesson, you added connection details to your application and tested the connection to the Neo4j database. This lesson completes Module 1 of the course.

In the next module, you will learn about mapping the graph data model to the application domain model.