Adding the Driver

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.

java
AppUtils.java
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/src/main/java/neoflix/AppUtils.java[tag=initDriver]

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:

  1. Add the org.neo4j.driver:neo4j-java-driver dependency to your pom.xml.

  2. Import the package into AppUtil.java.

  3. Create the driver instance using the credentials from getNeo4jUri(), getNeo4jUsername() and getNeo4jPassword() and then use the driver.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.

xml
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/pom.xml[tag=driver]

Verifying the Dependency

You can use the mvn 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.

java
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/src/main/java/example/Index.java[tag=import]

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:

java
AppUtils.java
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/01-connect-to-neo4j/src/main/java/neoflix/AppUtils.java[tag=initDriver]

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:

sh
Running the test
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:

sh
Check out the 01-connect-to-neo4j branch
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:

sh
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.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?