Setting up the Connection

To expose the connection and security parameters to the Oracle NoSQL Database SDK for Spring Data, you need to create a class that extends the AbstractNosqlConfiguration class. You could customize this code as per your requirement. Perform the following steps to set up a connection to the Oracle NoSQL Database.

Step 1: In your application, create the NosqlDbConfig class. This class will have the connection details to the Oracle NoSQL Database Proxy. Provide the @Configuration and @EnableNoSQLRepositories annotations to this NosqlDbConfig class. The @Configuration annotation tells the Spring Data Framework that the @Configuration annotated class is a configuration class that should be loaded before running the program. The @EnableNoSQLRepositories annotation tells the Spring Data Framework that it needs to load the program and lookup for the repositories that extends the NosqlRepository interface. The @Bean annotation is required for the repositories to be instantiated.

Step 2: Create an @Bean annotated method to return an instance of the NosqlDBConfig class. The NosqlDBConfig class will also be used by the Spring Data Framework to authenticate the Oracle NoSQL Database.

Step 3: Instantiate the NosqlDbConfig class. Instantiating the NosqlDbConfig class will cause the Spring Data Framework to internally instantiate an Oracle NoSQL Database handle by authenticating with the Oracle NoSQL Database.

Note:

You could add an exception code block to catch any connection error that might be thrown upon authentication failure.

Note:

Creating an Oracle NoSQL Database handle using the above-mentioned steps has a limitation. The limitation is that the application will not be able to connect to two or more different clusters at the same time. This is a Spring Data Framework limitation. For more information on Spring Data Framework, see Spring Core.

Note:

If you have trouble connecting to Oracle NoSQL Database from your Spring application, you can add an exception block and print the message for debugging.

As given in the following example, you can use the StoreAccessTokenProvider class to configure the Spring Data Framework to connect and authenticate with an Oracle NoSQL Database. You need to provide the URL of the Oracle NoSQL Database Proxy with non-secure access.

/*Annotation to specify that this class can be used by the 
  Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {

    /*Annotation to tell the Spring Data Framework that the returned object 
      should be registered as a bean in the Spring application.*/
    @Bean
    public NosqlDbConfig nosqlDbConfig() {
        AuthorizationProvider authorizationProvider;
        authorizationProvider = new StoreAccessTokenProvider();
        //Provide the host name and port number of the NoSQL cluster.
        return new NosqlDbConfig("http://<host:port>", authorizationProvider);
    }
}

The following example modifies the previous example to connect to a secure Oracle NoSQL Database store. For more details on StoreAccessTokenProvider class, see StoreAccessTokenProvider in the Java SDK API Reference.

/*Annotation to specify that this class can be used by the 
  Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {

    /*Annotation to tell the Spring Data Framework that the returned object 
      should be registered as a bean in the Spring application.*/
    @Bean
    public NosqlDbConfig nosqlDbConfig() {
        AuthorizationProvider authorizationProvider;
        //Provide the username and password of the NoSQL cluster.
        authorizationProvider = new StoreAccessTokenProvider(user, password);
        //Provide the host name and port number of the NoSQL cluster.
        return new NosqlDbConfig("http://<host:port>", authorizationProvider);
    }
}
For secure access, the StoreAccessTokenProvider parameterized constructor takes the following arguments.
  • username is the username of the kvstore.
  • password is the password of the kvstore user.

For more details on the security configuration, see Creating NoSQL Handle in the Administrator's Guide.

As given in the following example, you can use the SignatureProvider class to configure the Spring Data Framework to connect and authenticate with the Oracle NoSQL Database Cloud Service. See SignatureProvider in the Java SDK API Reference.

/*Annotation to specify that this class can be used by the 
  Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {

    /*Annotation to tell the Spring Data Framework that the returned object 
      should be registered as a bean in the Spring application.*/
    @Bean
    public NosqlDbConfig nosqlDbConfig() {
        SignatureProvider signatureProvider;

        /*Details that are required to authenticate and authorize access to 
          the Oracle NoSQL Database Cloud Service are provided.*/
        signatureProvider = new SignatureProvider(
            <tenantId>,  //The Oracle Cloud Identifier (OCID) of the tenancy.
            <userId>,  //The Oracle Cloud Identifier (OCID) of a user in the tenancy.
            <fingerprint>,  //The fingerprint of the key pair used for signing.
            <privateKeyFile>,  //Full path to the key file.
            <passphrase>  //Optional. A pass phrase for the key, if it is encrypted.
        );
        /*Provide the service URL of the Oracle NoSQL Database Cloud Service and 
          update the 'Region.US_PHOENIX_1' with an appropriate value.*/
        return new NosqlDbConfig(Region.US_PHOENIX_1,signatureProvider);
    }
}