Setting up the Connection
Learn how to set up a connection from Oracle NoSQL Database SDK for Spring Data to the Oracle NoSQL Database.
To expose the connection and security parameters to the Oracle NoSQL Database SDK for Spring Data, you must create a class that extends the AbstractNosqlConfiguration
class. You can customize this code as required. 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 must be loaded before running the program. The @EnableNoSQLRepositories
annotation tells the Spring Data Framework that it must 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 can 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 previously-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 about 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.Example 1-1 Setting up the connection in a nonsecure data store
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 must provide the URL of the Oracle NoSQL Database Proxy with nonsecure 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
must 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);
}
}
Example 1-2 Setting up the connection in a secure data store
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
must be registered as a bean in the Spring application.*/
@Bean
public NosqlDbConfig nosqlDbConfig() {
AuthorizationProvider authorizationProvider;
/* Provide the user name 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);
}
}
StoreAccessTokenProvider
parameterized constructor takes the following arguments.
user
is the user name of the kvstore.password
is the password of the kvstore user.
For more details on the security configuration, see Obtaining a NoSQL Handle.
Example 1-3 Setting up the connection in Oracle NoSQL Database Cloud Service
You can use different methods to connect to the Oracle NoSQL Database Cloud Service. For more details, see Connecting your Application to NDCS.
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.
You require tenancy id, user id, and fingerprint information which can be found on the user profile page of the cloud account under the User Information
tab on View Configuration File
. You can also add the passphrase to your private key. For more details, see Authentication to connect to Oracle NoSQL Database.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import oracle.nosql.driver.kv.StoreAccessTokenProvider;
import oracle.nosql.driver.Region;
import oracle.nosql.driver.iam.SignatureProvider;
import java.io.File;
public class AppConfig extends AbstractNosqlConfiguration
{
/* Annotation to tell that the returned object must be registered as a bean in the Spring application.*/
@Bean
public NosqlDbConfig nosqlDbConfig()
{
SignatureProvider signatureProvider;
char passphrase[] = < Pass phrase > ; // Optional. A passphrase for the key, if it is encrypted.
/* Details that are required to authenticate and authorize access to the Oracle NDCS 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.
new File( < privateKeyFile > ), // Full path to the key file.
passphrase //Optional.
);
/* Provide the service URL of the Oracle NoSQL Database Cloud Service */
/* Update the Region.<Region name>.endpoint() with the appropriate value. */
/* For example, Region.US_ASHBURN_1.endpoint() .*/
return new NosqlDbConfig(Region. < Region name > .endpoint(), signatureProvider);
}