Announcement Announcement Module
Collapse
No announcement yet.
DataSource lookup through JNDI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DataSource lookup through JNDI

    Am new to Spring. Trying to do a JNDI lookup to obtain a reference to DataSource object.

    The following is the config file that i use

    <beans>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="jdbc/OBUSV8" />
    </bean>

    <bean id="springJDBC" class="<className>">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    </beans>


    When i try to access the dataSouce through JdbcTemplate class, i get the following exception

    [7/6/07 8:57:57:718 GMT] 00000057 SystemOut O Setting dataSource com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource@4b23364 4
    [7/6/07 8:57:57:718 GMT] 00000057 SystemOut O <classname> ->getInstance Obtained dataSource com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource@4b23364 4
    [7/6/07 8:57:57:718 GMT] 00000057 SystemOut O <classname> ->getFiles Calling JdbcTemplate
    [7/6/07 8:57:57:768 GMT] 00000057 SystemOut O <classname> ->getFiles Exception obtained
    [7/6/07 8:57:57:798 GMT] 00000057 SystemErr R java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.jdbc.support.JdbcAccessor.afte rPropertiesSet(JdbcAccessor.java:134)
    at org.springframework.jdbc.core.JdbcTemplate.<init>( JdbcTemplate.java:143)


    This occurs when i use JdbcTemplate
    JdbcTemplate jdbc = new JdbcTemplate(getDataSource());

    The DAO class has a property dataSource. What is the property 'dataSource' that is being referred to above in the exception log. The dataSource has been properly configured as i am able to obtain a connection through a initialContext.lookup method.

    Would appreciate if someone could guide me on this.

  • #2
    Apparently your getDataSource() returns null.

    Also I wonder why are you creating a springJDBC thing in your configuration and next try to create a new JdbcTemplate()?

    Comment


    • #3
      Thanks for the response

      Apparently your getDataSource() returns null - yes it does. but if you could see the log messages posted above, i get a non-null value for the dataSource.

      "Also I wonder why are you creating a springJDBC thing in your configuration and next try to create a new JdbcTemplate()?" - Is it that we dont need to use JdbcTemplate here ? If so, how do we go about our DB operations ? I thought JdbcTemplate was one of the standard ways to access DB.

      Comment


      • #4
        If your springJdbc thing is a JdbcTemplate inject it into the beans who need it. That is the whole idea of DI. Also when you are creating object with new those aren't going to be injected and managed by Spring so all the configuration you have done isn't going to be used for those new instances.

        Can you post your code and full stracktrace, please use [ code][/code ] tags to post that.
        Last edited by Marten Deinum; Jul 6th, 2007, 06:14 AM.

        Comment


        • #5
          You were correct. Thanks - i modified my code to make it work.

          The following is the piece of code
          Code:
          public class DAOImpl {
          
              static {
                  // Loads the context
                  String location = "<>/dataSourceConfig.xml";
                  context = new FileSystemXmlApplicationContext(location);        
              }
              
              
              public DAOImpl getInstance() throws Exception {
                  return (DAOImpl) context.getBean("springJDBC");   
              }  
              
              
              public DTO getFiles(DTO dto) {
                  String methodName = "getFiles ";
                  try {
                      // TODO Auto-generated method stub
                      DAOImpl dao = getInstance();
          
                      JdbcTemplate template = new JdbcTemplate(dao.getDataSource());
          
                      int count = template.queryForInt("select count(*) from obt_manreq");    
          
                  } catch (DataAccessException daEx) {
                      daEx.printStackTrace();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }    
              
          }
          Now, i would like to know if this is indeed a proper way of using JDBC in Spring.

          Comment


          • #6
            Well ehr almost.

            You don't want to create a new application context each time, and you want to inject the JdbcTemplate into your dao. Also let spring create a DaoImpl for you and inject that one into your beans.

            I assume that this isn't production code but merely a test?

            Comment

            Working...
            X