Results 1 to 5 of 5

Thread: Help datasource not defined

  1. #1
    Join Date
    Aug 2004
    Location
    Thousand Oaks, CA
    Posts
    4

    Default Help datasource not defined

    I'm having a hard time getting started. I am trying to use the spring jdbc in a existing program. I am getting java.lang.IllegalArgumentException: dataSource is required error. I know if I can just get started, using spring's jdbc support will correct the problems we are having with leaked connections.


    <web-app id="WebApp">
    <display-name>SanctionedProviderWeb</display-name>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <beans>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location"><value>/WEB-INF/jdbc.properties</value></property>
    </bean>

    <!-- Message source for this context, loaded from localized "messages_xx" files -->
    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename"><value>messages</value></property>
    </bean>
    <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName"><value>jdbc/xep</value></property>
    </bean>
    <bean id="letterDao" class="com.wellpoint.xep.dao.LetterDaoImpl">
    <property name="dataSource"><ref local="myDataSource"/></property>
    </bean>
    <bean id="providerDao" class="com.wellpoint.xep.dao.ProviderDaoImpl">
    <property name="dataSource"><ref local="myDataSource"/></property>
    </bean>
    <bean id="sanctionDao" class="com.wellpoint.xep.dao.SanctionDaoImpl">
    <property name="dataSource"><ref local="myDataSource"/></property>
    </bean>

    </beans>

    public class ProviderDaoImpl implements ProviderDao {


    private DataSource dataSource;

    private List List;

    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;

    }
    public List getMonths() throws DataAccessException {

    final List months = new ArrayList();

    setDataSource(dataSource);

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    jdbcTemplate.queryForList("Select Distinct Month(STPR_MTCH_RPT_DT) RptMonth, Year(STPR_MTCH_RPT_DT) RptYear "
    + "from XEP.Stpr_Snctn_Prov");

    return months;
    }
    Thanks,
    jlm

  2. #2
    Join Date
    Aug 2004
    Posts
    1,110

    Default

    It does not look like you are using Spring's MVC so you don't automatically have access to the application context. How are you creating your DAO instances? If you are creating them using new then they would not have the datasource set. If you are not using Spring's MVC you can access the application context using
    Code:
            webAppCtx = WebApplicationContextUtils.getWebApplicationContext&#40;servletContext&#41;;
            dao = webAppCtx.getBean&#40;"ProviderDao"&#41;;
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    From org.springframework.jndi.AbstractJndiLocator source
    /**
    * Convenient superclass for JNDI-based service locators. Subclasses are
    * JavaBeans, exposing a jndiName property. This may or may not include
    * the "java:comp/env/" prefix expected by J2EE applications when accessing
    * a locally mapped (ENC - Environmental Naming Context) resource. If it
    * doesn't, the "java:comp/env/" prefix will be prepended if the "resourceRef"
    * property is true (the default is <strong>false</strong>) and no other scheme
    * like "java:" is given.
    *
    * ...
    */
    You should either use
    Code:
      <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName"><value>java&#58;comp/env/jdbc/xep</value></property> 
      </bean>
    or
    Code:
      <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName"><value>jdbc/xep</value></property>
        <property name="resourceRef"><value>true</value></property>
      </bean>
    An other issue: why are you calling
    Code:
      setDataSource&#40;dataSource&#41;;
    inside getMonths()
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  4. #4
    Join Date
    Aug 2004
    Location
    Thousand Oaks, CA
    Posts
    4

    Default

    I am still having trouble. I tried the code from Spring Team for the WebApplicationContext below and the servletContext cannot be resolved. Should the WebApplicationContext be used only in a Servlet or can it be used in a helper class like the code below? Also do I need a listener-class in the web.xml file? As always I appreciate any help.

    import com.wellpoint.xep.beans.guibeans.*;
    import com.wellpoint.xep.beans.compositebeans.*;
    import com.wellpoint.xep.dao.ProviderDaoImpl;
    import org.springframework.web.context.*;
    import org.springframework.web.context.support.WebApplica tionContextUtils;
    import org.springframework.web.*;

    public class ProviderSanctionsHelper extends com.wellpoint.xep.helpers.BaseHelper {


    /** Creates new ProviderSanctionsBean */
    public ProviderSanctionsHelper() {
    }

    public void readReportMonths(BaseHashtableDataBean monthList)
    throws java.sql.SQLException, java.lang.ClassNotFoundException {

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContex t(servletContext);
    providerDao = webAppCtx.getBean("providerDao");

    //ProviderDaoImpl providerDao = new ProviderDaoImpl();

    providerDao.getMonths();
    Thanks,
    jlm

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    I am still having trouble. I tried the code from Spring Team for the WebApplicationContext below and the servletContext cannot be resolved.
    servletContext is not declared in you helper class. If it is not declared in the parent class (BaseHelper), then you need to pass a valid ServletContext as parameter when calling readReportMonths.

    Should the WebApplicationContext be used only in a Servlet or can it be used in a helper class like the code below?
    WebApplicationContext can be used anywhere you want, however, in order to access your WebApplicationContext, you need to provide a valid ServletContext.

    Also do I need a listener-class in the web.xml file?
    you can configue a WebApplicationContext either by using org.springframework.web.context.ContextLoaderServl et or org.springframework.web.context.ContextLoaderListe ner although ContextLoaderListener is recommended.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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