This is your first challenge of this course. Your challenge here is to modify the code to create a new instance of the Driver that can be used across the application.
As we discussed in the About the Driver lesson, it is best practice to create a single instance of the driver in our application per Neo4j cluster or DBMS.
Inside AppUtils.java
, you will see an initDriver()
function.
static Driver initDriver() {
// TODO: Create and assign an instance of the driver here
return null;
}
This function should use the getNeo4jUri()
, getNeo4jUsername()
and getNeo4jPassword()
methods from AppUtils
to create an instance of the Neo4j Java Driver, verify connectivity and return it.
Challenge: Implement the initDriver function.
Your first challenge is to modify the initDriver()
method in src/AppUtils.java
to create an instance of the driver and return it.
To do this, we will need to:
-
Add the
org.neo4j.driver:neo4j-java-driver
dependency to yourpom.xml
. -
Import the package into
AppUtil.java
. -
Create the driver instance using the credentials from
getNeo4jUri()
,getNeo4jUsername()
andgetNeo4jPassword()
and then use thedriver.verifyConnectivity()
to assert that the credentials are correct.
1. Add the neo4j-java-driver Dependency
First, you will need to add the neo4j-java-driver
dependency to your pom.xml
.
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>5.1.0</version>
</dependency>
Verifying the Dependency
You can use themvn verify
to download the driver library and its dependencies.2. Importing the Dependency
To include the Driver dependency in our module, you will add an import
directive to the top of the file.
Copy and paste the following code into the top of src/main/java/neoflix/AppUtils.java
.
// Import all relevant classes from neo4j-java-driver dependency
import org.neo4j.driver.*;
3. Creating the Driver Instance
Create the driver instance by calling the GraphDatabase.driver()
method.
The first argument will be the uri
from getNeo4jUri()
.
The second argument will be an authentication token which can be created using the AuthTokens.basic()
function, this takes two arguments; the username returned by the getNeo4jUsername()
method and the password returned by the getNeo4jPassword()
method.
Replace the initDriver
placeholder function with the following code:
static Driver initDriver() {
AuthToken auth = AuthTokens.basic(getNeo4jUsername(), getNeo4jPassword());
Driver driver = GraphDatabase.driver(getNeo4jUri(), auth);
driver.verifyConnectivity();
return driver;
}
In the last line of the function above, the verifyConnectivity()
method will verify that the connection details are correct.
If the connection cannot be made for any reason, call will fail with an Exception. If this occurs, your application will be unable to communicate with Neo4j. Manual investigation will be required to diagnose the issue.
If the connection has been successfully verified, it will return an instance of the driver.
Testing
To test that this functionality has been correctly implemented, run the following code in a new terminal session:
mvn test -Dtest=neoflix._01_ConnectToNeo4jTest#createDriverAndConnectToServer
The test file is located at src/test/java/neoflix/_01_ConnectToNeo4jTest.java
.
Are you stuck? Click here for help
If you get stuck, you can see a working solution by checking out the 01-connect-to-neo4j
branch by running:
git checkout 01-connect-to-neo4j
You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.
Check Your Understanding
1. How many tests are run in the test you ran?
-
✓ 1
-
❏ 2
-
❏ 3
-
❏ 4
Hint
You can run the src/test/java/neoflix/_01_ConnectToNeo4jTest.java
test suite by running the following command:
mvn test -Dtest=neoflix._01_ConnectToNeo4jTest#createDriverAndConnectToServer
Solution
There is 1 test in the test suite.
Lesson Summary
You have implemented the code to create a new driver instance with environment variables read from the application.properties
file which verifies that the driver can connect to the DBMS before the application starts.
In the next module we will look at how to use the driver to query the DBMS.