Spring Error loading JNDI file to search for data source - name comp / env / jdbc not found in context "java:"

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

The environment that is trying to deploy: WebSphere Application Server 8.5.5.4

The problem I encountered was a crash when starting the application when trying to find the JNDI data source ( jdbc / fileUploadDS ), as shown below in the dependent data service module.

@Configuration @Profile("prod") public class JndiDataConfig implements DataConfig { @Bean public DataSource dataSource() throws NamingException { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:comp/env/jdbc/fileUploadDS"); } } 

My spring boot configuration:

 @Configuration @ComponentScan(basePackages = { "au.com.aiaa.fileupload.data.*", "demo" }) @EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class }) public class SampleApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(applicationClass, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass); } private static Class<SampleApplication> applicationClass = SampleApplication.class; @Bean public static Properties fileUploadJndiProperties() throws NamingException { JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean(); jndiFactoryBean.setJndiName("props/FileUploadProperties"); jndiFactoryBean.setExpectedType(Properties.class); jndiFactoryBean.setLookupOnStartup(true); jndiFactoryBean.afterPropertiesSet(); return (Properties) jndiFactoryBean.getObject(); } } 

Please note that I can successfully find the details / FileUploadProperties. But the inability to do the same for the data source.

I doubt that he is trying to load an EmbeddedWebApplicationContext , which is not what I want.

Stack trace:

 Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".** at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at **org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)** at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:142) at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89) at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175) .................. Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".** at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586) ... 132 common frames omitted Caused by: javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:". at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970) at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377) at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220) at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142) at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436) at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477) at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485) at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370) at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161) at javax.naming.InitialContext.lookup(InitialContext.java:436) at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource(JndiDataConfig.java:41) at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.CGLIB$dataSource$0(<generated>) at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe$$FastClassBySpringCGLIB$$3c9e0518.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.dataSource(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) at java.lang.reflect.Method.invoke(Method.java:611) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) 

What am I missing here? Even when I try to explicitly define the dataSource bean method in SampleApplication.java as shown below, it fails with the same error.

 @Bean public static DataSource dataSource() throws NamingException { JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean(); jndiFactoryBean.setJndiName("java:comp/env/jdbc/fileUploadDS"); jndiFactoryBean.setExpectedType(DataSource.class); jndiFactoryBean.setLookupOnStartup(true); jndiFactoryBean.setResourceRef(true); jndiFactoryBean.afterPropertiesSet(); return (DataSource) jndiFactoryBean.getObject(); } 

I called this and says we need to set enableNaming () in the servlet container? Can I do something similar for the non-embedded context of web applications? Or is it just a WAS 8.5 question?

+5
source share
4 answers

You need to have a resource link named jdbc/fileUploadDS in web.xml . And make sure that it is bound to the actual name of the data source during installation or through the ibm-web-bnd.xml .

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 do not want to use web.xml , then in a regular Java EE application, you can simply add the following class annotation to the web component (servlet, filter):

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

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

+7
source

I ran into the same problem. I don’t know how to define a tag in spring boot, since there is no web.xml file in spring file.

So far, I have learned that we must define it in the application file, from where we start our spring application. I think we need to use this method to set the data source:

  @Bean(destroyMethod="") @ConfigurationProperties(prefix="datasource.mine") public DataSource dataSource() throws Exception { JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); return dataSourceLookup.getDataSource("java:jdbc/configurationFile"); } 
+1
source

I can connect the Spring-Boot application (deployed to WebSphere Application Server 9) to the WAS data source. The following code worked for me to connect to the 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 DSNAME data source name is the name that appears in the user interface of the Websphere console. You can see that through β†’ Select Resources> JDBC> Data Sources.

Then I created the jdbc template:

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

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

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

0
source

I just configured spring boot using my own data source as follows:

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

and inside the application.properties file, I defined all the parameters of the data source as usual

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

It works great with @SpringBootApplication with all other default settings

thanks

0
source

Source: https://habr.com/ru/post/1209062/


All Articles