Announcement Announcement Module
Collapse
No announcement yet.
Help datasource not defined Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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;
    }

  • #2
    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;;

    Comment


    • #3
      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()

      Comment


      • #4
        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();

        Comment


        • #5
          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.

          Comment

          Working...
          X