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.
Spring Boot Starter Web provides all the dependencies and the auto configuration needed to develop web applications. We should use the first dependency. We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat.
Open the sqlnet. ora file, and use the ENCRYPTION_WALLET_LOCATION parameter to specify the Oracle Wallet Location. CAUTION: The wallet location directory must have an absolute path and end with a right parenthesis. Verify that there are no invisible characters at the end of the directory path.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With