Spring boot JNDI datasource lookup failure - Name comp/env/jdbc not found in context “java:”

前端 未结 4 1053
北海茫月
北海茫月 2020-12-10 16:28

I have setup a spring boot (v 1.1.9) application to deploy as a WAR file. And I\'m trying to integrate this web application with an existing data service module (added as a

相关标签:
4条回答
  • 2020-12-10 16:37

    I am able to connect my Spring-Boot application (deployed in Websphere Application Server 9) to WAS datasource. The following code worked for me, for connecting to DataSource:

    @Bean(name = "WASDataSource")
    public DataSource WASDataSource() throws Exception {        
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    return dataSourceLookup.getDataSource("DSNAME");
    }   
    
    @Bean(name = "WASDataSourceJdbcTemplate")
    public JdbcTemplate jdbcTemplate_WASDS(@Qualifier("WASDataSource")
    DataSource dsDB2) {
    return new JdbcTemplate(dsDB2);
    }
    

    Note: The name of Datasource "DSNAME" is the name which appears on the UI of Websphere console. You can see that via -> Select Resources > JDBC > Data Sources.

    Then I created jdbc template:

    @Autowired
    @Qualifier("WASDataSourceJdbcTemplate")
    private JdbcTemplate db2WASTemplate;`
    

    And running query using the query method works fine :
    db2WASTemplate.query()

    I did not create any Web.xml or ibm-web-bnd.xml files

    0 讨论(0)
  • 2020-12-10 16:40

    You need to have resource reference with jdbc/fileUploadDS name in your web.xml. And make sure it is bound to actual datasource name during installation or via ibm-web-bnd.xml file.

    Definition in web.xml:

    <resource-ref>
        <description />
        <res-ref-name>jdbc/fileUploadDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    If you dont want to use web.xml, then in normal Java EE app you could just add in web component (servlet, filter) the following class annotation:

    @Resource(name="jdbc/fileUploadDS", type=javax.sql.DataSource.class, lookup="jdbc/fileUploadDS")
    

    but I'm not Spring-boot expert, so don't know, if it will work or is possible there.

    0 讨论(0)
  • 2020-12-10 16:47

    I am facing the same problem. I don't know how to define tag in spring boot since there is no web.xml file in the spring boot.

    So far what I came to know that we have to define it in the application file from where we start our spring application. I think we need to use this method to set the Datasource:

      @Bean(destroyMethod="")
      @ConfigurationProperties(prefix="datasource.mine")
      public DataSource dataSource() throws Exception {
          JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
          return dataSourceLookup.getDataSource("java:jdbc/configurationFile");
      }
    
    0 讨论(0)
  • 2020-12-10 16:55

    I just configured spring boot with my custom datasource as follows:

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    

    and inside the application.properties file I defined all datasource settings as usual

    spring.datasource.driver-class-name= ***
    spring.datasource.url= ***
    spring.datasource.username= ***
    spring.datasource.password= ***
    spring.datasource.jndi-name=jdbc/myDB
    

    It works no nicely with @SpringBootApplication with all other default settings

    Thanks

    0 讨论(0)
提交回复
热议问题