1

I am working on a spring batch which runs only once in a day. Please note that its not a spring boot Project rather its just Spring core along with Spring batch libraries.

I have a question, this jar will be placed in a server where IBM Websphere Was 6 server is also running. In my Jar, Would I be able to use the JNDI datasource connection available in the WAS server.

I thought even though the Jar resides on the same physical server as IBM WAS, JNDI can only be used by the apps deployed in the WAS Server . Please let me know if my understanding is wrong.

Also if you can guide me in the right direction on ways to efficently connecting to Database would be very helpful. I am just looking for reference as a starter.

    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "ABPBatchInfrastructure.xml",
            "AgencyBillPayFileUploadApp.xml"
    );

DataSource ds = (DataSource) ctx.getBean("dbDataSource"); 

Connection connection = ds.getConnection();

Statement st = connection.createStatement();

ResultSet rs = st.executeQuery("select name from notify.product");

XML Below

<jee:jndi-lookup id="dbDataSource"
jndi-name="jdbc/MyLocalDB"
expected-type="javax.sql.DataSource" />

Stack Trace below

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbDataSource': Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
Saimuga
  • 255
  • 1
  • 5
  • 16
  • Does this answer your question? [How to use JNDI DataSource provided by Tomcat in Spring?](https://stackoverflow.com/questions/9183321/how-to-use-jndi-datasource-provided-by-tomcat-in-spring) – Mahmoud Ben Hassine May 13 '20 at 08:38
  • The link to the duplicate question is about Tomcat, but it should work with WAS as well (how to get a jndi datasource in Spring is the same). – Mahmoud Ben Hassine May 13 '20 at 08:40
  • Hi Ben, I have gone through the post and tried implementing the code in my program but it throws below error. Also there is no clairity on that thread for my question below. Does the app needs to be deployed in Tomcat inorder to get connection. In my case it is not deployed in Tomcat, it is just a simple Jar which is present in the same physical machine where tomcat is running. Does that work ? I have shared my updated code snippet in the question. – Saimuga May 14 '20 at 03:53

0 Answers0