【问题标题】:Spring boot web application not working with oracle walletSpring Boot Web 应用程序无法与 Oracle 钱包一起使用
【发布时间】:2021-11-26 22:34:48
【问题描述】:

我正在为命令行运行程序和 Web 应用程序开发 Spring Boot。这两个应用都需要用预言机钱包实现,所以我实现了预言机钱包。命令行运行程序能够使用 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 设置了所有属性,并且在创建数据源时遇到空指针异常。

在这方面的任何指针或帮助将不胜感激。

【问题讨论】:

    标签: spring-boot spring-jdbc


    【解决方案1】:

    经过尝试,我可以弄清楚当我们需要在 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  
    

    【讨论】:

    • 对于任何多线程负载测试,请始终使用 Oracle 11.2.0.4 或更高版本的 ojdbc 驱动程序,因为 11.2.0.3 ojdbc 驱动程序不支持多线程访问 Oracle 钱包。如果您在多线程上使用 10.2.0.3 版本的 ojdbc,将会看到一些日志,例如 PKIX 错误、02002 和无法打开 cwallet.sso 文件。
    【解决方案2】:
    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>
    

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    【解决方案3】:

    除了上述步骤之外,请确保在正确的版本级别添加这些依赖项:

        <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>
    

    【讨论】:

      【解决方案4】:

      这个解决方案对我有用。

      build.gradle 文件中你需要这些依赖:

      dependencies {
              // Oracle database
          runtimeOnly('com.oracle.database.jdbc:ojdbc8:21.1.0.0') {
              exclude group: 'com.oracle.database.ha', module: 'simplefan'
              exclude group: 'com.oracle.database.ha', module: 'ons' 
          }
          runtimeOnly 'com.oracle.database.jdbc:ucp:21.1.0.0'
          runtimeOnly 'com.oracle.database.security:oraclepki:21.1.0.0'   
          runtimeOnly 'com.oracle.database.security:osdt_cert:21.1.0.0'   
          runtimeOnly 'com.oracle.database.security:osdt_core:21.1.0.0'   
      }
      

      我已经在 application.properties 文件中定义了数据源信息:

      spring.datasource.url=jdbc:oracle:thin:@name_in_tnsnames?TNS_ADMIN=C:/path/to/oracle/wallet
      spring.datasource.username=username
      spring.datasource.password=password
      

      Oracle 钱包的路径必须是包含文件tnsnames.ora 和密钥和其他文件的目录。通常 Oracle 钱包以 ZIP 文件的形式分发,在这种情况下,您需要从该 ZIP 存档中提取文件到您将在数据源定义中引用的目录中。

      【讨论】:

        猜你喜欢
        • 2022-11-11
        • 2015-01-28
        • 2016-11-17
        • 1970-01-01
        • 2019-01-10
        • 2017-08-31
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多