Results 1 to 6 of 6

Thread: DataSource lookup through JNDI

  1. #1
    Join Date
    Jul 2007
    Posts
    3

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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()?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2007
    Posts
    3

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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 at 06:14 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2007
    Posts
    3

    Default

    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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •