Spring启动Web应用程序无法使用oracle钱包

时间:2017-05-16 05:43:47

标签: spring-boot spring-jdbc

我正在命令行运行器和Web应用程序上进行spring boot。这两个应用程序都需要用oracle钱包来实现,所以我实现了oracle钱包。命令行运行器能够使用oracle数据源使用spring jdbc模板连接到数据库,但是相同的配置无法为数据源对象创建bean。当使用数据库用户名和密码实现相同时,我能够连接。

我正在接受这篇文章的帮助 - [Connect to Oracle DB from Spring-jdbc with Oracle Wallet authentification

代码类似于

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;

我从启动应用程序的application.properties设置了所有属性,并且在创建数据源时出现空指针异常。

非常感谢在这方面的任何指针或帮助。

1 个答案:

答案 0 :(得分:2)

尝试之后,当我们需要在春季启动中加入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