Announcement Announcement Module
Collapse
No announcement yet.
JndiObjectFactoryBean cast error with tomcat data source Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JndiObjectFactoryBean cast error with tomcat data source

    Hi,
    We are trying to configure a datasource in tomcat. The datasource is configured properly as we are able to lookup using simple code in JSP as below :
    --------------------------------------------------------------------------------------------------------------------------------------------
    Code:
    <%@ page import="java.util.*,java.sql.*,javax.naming.*,javax.sql.*" %>
    
    <%
    Hashtable env = new Hashtable();
    Context ctx = new InitialContext(env);
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/cmpPubPoolTest");		
    out.print("data source is "+ ds);
    
    %>
    --------------------------------------------------------------------------------------------------------------------------------------------
    This prints the data source object.

    Now the problem is to lookup this datasource from spring.
    The configuration in spring is :
    --------------------------------------------------------------------------------------------------------------------------------------------
    Code:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    	<property name="jndiEnvironment">
    		<props>
    			<prop key="java.naming.factory.initial">org.apache.naming.java.javaURLContextFactory</prop>
    			<prop key="java.naming.factory.url.pkgs">org.apache.naming</prop>
    			
    		</props>
    				
    	</property>
    	
    	<property name="jndiName"><value>java:comp/env/jdbc/cmpPubPool</value></property>
    </bean>
    --------------------------------------------------------------------------------------------------------------------------------------------

    Tried with and without specifying the jndiEnvironment , but we get the below error.

    I ended up with the below error.

    --------------------------------------------------------------------------------------------------------------------------------------------
    Code:
    applicationContext-hibernate.xml]: Error setting property values; nested exception is PropertyAccessExceptionsException (1 errors)
    Caused by:
    PropertyAccessExceptionsException (1 errors)
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.apache.tomcat.dbcp.dbcp.BasicDataSource] to required type [org.springframework.jdbc.datasource.DriverManagerDataSource] for property 'dataSource'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
    Caused by:
    java.lang.IllegalArgumentException: No matching editors or conversion strategy found    at org.springframework.beans.PropertyTypeConverter.convertIfNecessary(Pr
    opertyTypeConverter.java:209)
            at org.springframework.beans.PropertyTypeConverter.convertIfNecessary(Pr
    opertyTypeConverter.java:108)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrappe
    rImpl.java:723)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrappe
    rImpl.java:575)
            at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(A
    bstractPropertyAccessor.java:49)
            at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(
    AbstractPropertyAccessor.java:68)
            at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(
    AbstractPropertyAccessor.java:57)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:792)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.populateBean(AbstractAutowireCapableBeanFactory.java:572)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.createBean(AbstractAutowireCapableBeanFactory.java:415)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
    (AbstractBeanFactory.java:241)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
    (AbstractBeanFactory.java:152)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.
    preInstantiateSingletons(DefaultListableBeanFactory.java:253)
            at org.springframework.context.support.AbstractApplicationContext.refres
    h(AbstractApplicationContext.java:331)
            at org.springframework.web.context.support.AbstractRefreshableWebApplica
    tionContext.refresh(AbstractRefreshableWebApplicationContext.java:155)
            at org.springframework.web.context.ContextLoader.createWebApplicationCon
    text(ContextLoader.java:246)
            at org.springframework.web.context.ContextLoader.initWebApplicationConte
    xt(ContextLoader.java:184)
            at org.springframework.web.context.ContextLoaderListener.contextInitiali
    zed(ContextLoaderListener.java:49)
            at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
    t.java:3729)
            at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
    187)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
    .java:759)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:73
    9)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
    
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.jav
    a:904)
            at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
    ava:867)
            at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474
    )
            at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
            at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
    :310)
            at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
    eSupport.java:119)
            at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
    
            at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
            at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
    
            at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442
    )
            at org.apache.catalina.core.StandardService.start(StandardService.java:4
    50)
            at org.apache.catalina.core.StandardServer.start(StandardServer.java:709
    )
            at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
            at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
    
    Jul 17, 2007 11:58:50 AM org.apache.catalina.core.StandardContext start
    SEVERE: Error listenerStart
    Jul 17, 2007 11:58:50 AM org.apache.catalina.core.StandardContext start
    SEVERE: Context [/cmpWeb2] startup failed due to previous errors
    --------------------------------------------------------------------------------------------------------------------------------------------

    The context.xml is kept in webapp/myApp/META-INF with the below :
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context
        reloadable="true">
      <Resource
        auth="Container"
        name="jdbc/cmpPubPool"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        password="cmppub"
        maxIdle="2"
        maxWait="5000"
        username="cmppub"
        url="jdbc:oracle:thin:@172.20.51.125:1521:cmp"
        maxActive="4"
    />
    
    <ResourceLink name="jdbc/cmpPubPool" type="javax.sql.DataSource" global="jdbc/cmpPubPool"/>
        <Logger name="tc_log"
                path="logs/mytomcat.log"
                verbosityLevel = "INFORMATION" />
    </Context>
    --------------------------------------------------------------------------------------------------------------------------------------------
    The web.xml is configured as below:

    --------------------------------------------------------------------------------------------------------------------------------------------
    Code:
      <resource-ref>
    	     <res-ref-name>jdbc/cmpPubPool</res-ref-name>
    	     <res-type>javax.sql.DataSource</res-type>
    	     <res-auth>Container</res-auth>
       </resource-ref>
    --------------------------------------------------------------------------------------------------------------------------------------------

    Am i doing something wrong or Is it a bug in spring ?? Kindly help me.

    Regards
    Sandeep
    Last edited by sandeep_maloth; Jul 17th, 2007, 08:39 AM. Reason: formatted

  • #2
    Failed to convert property value of type [org.apache.tomcat.dbcp.dbcp.BasicDataSource] to required type [org.springframework.jdbc.datasource.DriverManagerD ataSource] for property 'dataSource';

    You didn't show where is the datasource injected, but from the error message I'd say that the property is of the type org.springframework.jdbc.datasource.DriverManagerD ataSource and it should be of the type javax.sql.DataSource

    Comment


    • #3
      Thanks for the quick reply dejan.

      The spring configuration resides in the web app. The datasource is looked up using JNDI with JndiObjectFactoryBean

      The complete spring configuration is as below
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
      
      <beans>
      
        <bean id="propertyConfigurer" 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location"><value>/WEB-INF/hibernate.properties</value></property>
        </bean>
        
      <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      	
      	<property name="jndiName"><value>java:comp/env/jdbc/cmpPubPool</value></property>
      	<property name="expectedType"><value>org.apache.tomcat.dbcp.dbcp.BasicDataSource</value></property>
      	
      </bean>
      
      <!--
      <bean id="ref" class="java.util.Properties">
      	<constructor-arg>
      
      		 <props>
      			<prop key="java.naming.factory.initial">org.apache.naming.factory.DbcpDataSourceFactory</prop>			
      		</props> 		
      		
      	</constructor-arg>
      </bean>
      -->
      <!-- All table mapping hbm files are specified here -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource"><ref bean="dataSource"/></property>
          <property name="mappingResources">
            <list>
            
              <value>com/cmp/web2/dao/hbm/BlogThreadMap.hbm.xml</value>
      
           </list>
          </property>
          <property name="hibernateProperties">
            <props>
              <prop key="hibernate.dialect">${hibernate.dialect}</prop>
              <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            </props>
          </property>
        </bean>
        
            
      </beans>
      Regards
      Sandeep
      Last edited by sandeep_maloth; Jul 17th, 2007, 08:38 AM.

      Comment


      • #4
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
        <property name="jndiName"><value>java:comp/env/jdbc/cmpPubPool</value></property>
        <property name="expectedType"><value>org.apache.tomcat.dbcp. dbcp.BasicDataSource</value></property>
        </bean>

        I don't think you need the expectedType property in the DataSource definition.

        Comment


        • #5
          Please use [ code][/code ] tags, makes it more readable.

          The interface could be a problem I would always use the most generic interface in this case javax.sql.DataSource and not some implementation (makes it easier to swap or switch when testing for instance).

          Comment


          • #6
            Thanks again dejan and mdeinum.
            I tried with javax.sql.DataSource as below :
            Code:
            <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            	<property name="proxyInterface" value="javax.sql.DataSource"/>
            	    
            	<property name="jndiName"><value>java:comp/env/jdbc/cmpPubPool</value></property>
            		
            </bean>
            But this time gettting the below exception:
            Code:
            <Context initialization failed>
            org.springframework.beans.factory.BeanCreationException: Error creating bean wit
            h name 'articleDAO' defined in ServletContext resource [/WEB-INF/context/applica
            tionContext-hibernate.xml]: Error setting property values; nested exception is P
            ropertyAccessExceptionsException (1 errors)
            Caused by:
            PropertyAccessExceptionsException (1 errors)
            org.springframework.beans.TypeMismatchException: Failed to convert property valu
            e of type [$Proxy17] to required type [org.springframework.jdbc.datasource.Drive
            rManagerDataSource] for property 'dataSource'; nested exception is java.lang.Ill
            egalArgumentException: No matching editors or conversion strategy found
            Caused by:
            java.lang.IllegalArgumentException: No matching editors or conversion strategy f
            ound
                    at org.springframework.beans.PropertyTypeConverter.convertIfNecessary(Pr
            opertyTypeConverter.java:209)
                    at org.springframework.beans.PropertyTypeConverter.convertIfNecessary(Pr
            opertyTypeConverter.java:108)
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrappe
            rImpl.java:723)
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrappe
            rImpl.java:575)
            I am using Spring Framework Version: 2.0.3
            Regards
            Sandeep

            Comment


            • #7
              Can you show us the class that implements articleDao bean?

              Comment


              • #8
                I wonder why Spring is trying to a DrivermanagerDataSource? Did you base your Dao on that? Always program to interfaces so in this case to javax.sql.DataSource...

                Comment


                • #9
                  Thanks,
                  The problem was with our code .It was due to bad code (not programming to interface) ..The DAO class was having member org.springframework.jdbc.datasource.DriverManagerD ataSource;
                  Thanks a lot. You saved my day.
                  Regards
                  Sandeep

                  Comment

                  Working...
                  X