san*_*mik 5 spring-jdbc spring-boot
我正在使用命令行运行程序和 Web 应用程序进行 Spring Boot。两个应用都需要用oracle钱包实现,所以我实现了oracle钱包。命令行运行程序能够使用使用 oracle 数据源的 spring jdbc 模板连接到数据库,但相同的配置无法为数据源对象创建 bean。当使用数据库用户名和密码实现相同时,我就可以连接了。
我正在从这篇文章中获得帮助 - [使用 Oracle 钱包身份验证从 Spring-jdbc 连接到 Oracle DB
代码类似于,
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;
Run Code Online (Sandbox Code Playgroud)
我从启动应用程序的 application.properties 设置了我的所有属性,并且在创建数据源时出现空指针异常。
在这方面的任何指示或帮助将不胜感激。
经过尝试,我可以弄清楚当我们需要在 spring boot 中包含 oracle 钱包时我们需要做什么。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4822 次 |
| 最近记录: |