5
votes

I am working on spring boot on both command line runner and web application. Both applications require to be implemented with oracle wallet, so I implemented the oracle wallet. The command line runner is able to connect to database using spring jdbc template using oracle datasource but same configuration is not been able to create the bean for datasource object. When same has been implemented with database user name and password, I am able to connect.

I am taking help from this post - [Connect to Oracle DB from Spring-jdbc with Oracle Wallet authentification

with code similar to,

System.setProperty("oracle.net.tns_admin", "path/to/your/tnsnames");

OracleDataSource ds = new OracleDataSource();

Properties props = new Properties();
props.put("oracle.net.wallet_location", "(source=(method=file)(method_data=(directory=path/to/your/wallet)))");
ds.setConnectionProperties( props );
ds.setURL("jdbc:oracle:thin:/@dbAlias"); //dbAlias should match what's in your tnsnames

return ds;

I have all my properties set from application.properties of boot application and I am getting null pointer exception on creating the datasource.

Any pointer or help in this regard will be much appreciated.

3

3 Answers

5
votes

After trying, I could figure out what we need to do when we need to include oracle wallet in spring boot.

1. In application.properties put two properties,
   A> spring.datasource.url=jdbc:oracle:thin:/@<DB_ALIAS_NAME>
   B> spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

2. On boot runner/configuration class, 
   A> Define the dataSource bean like this,


 @Bean
   public DataSource dataSource() {
       OracleDataSource dataSource = null;
       try {
           dataSource = new OracleDataSource();
           Properties props = new Properties();
           String oracle_net_wallet_location = 
           System.getProperty("oracle.net.wallet_location");
           props.put("oracle.net.wallet_location", "(source=(method=file)(method_data=(directory="+oracle_net_wallet_location+")))");
           dataSource.setConnectionProperties(props);
           dataSource.setURL(url);
       } catch(Exception e) {
           e.printStackTrace();
       }
       return dataSource;
   }

   B> Define the jdbcTemplate bean as follows,


@Bean
   public JdbcTemplate jdbcTemplate(DataSource dataSource) {
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
       jdbcTemplate.setFetchSize(20000);
       return jdbcTemplate;
   }

3. Now we need to set jvm arguments in boot runner class like as follows,
   -Doracle.net.wallet_location=<PATH_TO_WALLET_DIR> -Doracle.net.tns_admin=<PATH_TO_WALLET_DIR>
   Note - <WALLET_DIR> should contain .sso, .p12 and .ora files. On external 
   server like tomcat, set above two variables on catalina.sh or catalina.bat 
   depending on your environment OS.

I hope this helps.
Thanks,
Sandip  
0
votes
add below in application properties 

spring.datasource.url=jdbc:oracle:thin:@db202007181319_medium?TNS_ADMIN=C:/wallet/Wallet_Name
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=ADMIN
spring.datasource.password=yourpassword


Also pom entry as follows : - 

       <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>19.6.0.0</version>
        </dependency>
0
votes

In addition to the steps above, make sure to add these dependencies at the correct version level:

    <dependency>
        <groupId>com.oracle.database.security</groupId>
        <artifactId>osdt_cert</artifactId>
        <version>21.1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.security</groupId>
        <artifactId>osdt_core</artifactId>
        <version>21.1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.security</groupId>
        <artifactId>oraclepki</artifactId>
        <version>21.1.0.0</version>
    </dependency>