Search code examples
javaspring-bootwebspherejndiear

JNDI configuration with spring boot with WebSphere 9.0.0.7


I have added spring.datasource.jndi-name=java:comp/env/jdbc/DB_Name property in application.properties file, datasource not created.

I want to use that datasoure with spring boot 2.0.3, spring boot data jpa(hibernate),websphere. Need to use the jparepository for CURD operations.

I have seen the similar issue. But we don't have web.xml in spring boot to add resource reference. So can't follow that answer. How can I achieve the jparepository.


Solution

  • Need to add the below property in application.properties

    spring.datasource.jndi-name=jdbc/yourjndiname

    In SpringApplication.java class where is our spring boot main method exist.Below code generates the datasource for us.

    @Autowired
        private Environment env;
    @Bean
        public DataSource dataSource() throws NamingException {
            return (DataSource) new JndiTemplate().lookup(env.getProperty("spring.datasource.jndi-name"));
        }
    

    And one more thing If you want access to your table in schema level mention the schema name in @Table(schema = "DB_SCHEMA",name = "DB_TABLE")