0

I'm using OpenEJB 4.0.0, Spring 3.1.0.RELEASE, and JUnit 4.8.1. I'm trying to use OpenEJB to setup a datasource, which I do like so …

    final Properties props = loadMyProjectProps();
    final String dsJndiName = props.getProperty("DATASOURCE_JNDI_PREFIX") + "MySqlDS";
    System.out.println("ds jndi name:" + dsJndiName);
    p.put(dsJndiName, "new://Resource?type=DataSource");
    p.put(dsJndiName + ".JdbcDriver", "com.mysql.jdbc.Driver");
    final String url = "jdbc:mysql://" + props.getProperty("DB_SERVER") + ":" + props.getProperty("DB_PORT") + "/" + props.getProperty("DB_NAME");
    p.put(dsJndiName + ".JdbcUrl", url);
    p.put(dsJndiName + ".Username", props.getProperty("DB_USER"));
    p.put(dsJndiName + ".Password", props.getProperty("DB_PASSWORD"));
    final InitialContext initialContext = new InitialContext(p);

but upon initializing my Spring text application, which sets up the session factory and datasource like so …

    <bean id="myprojSessionFactory" lazy-init="true" scope="singleton"
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="mappingResources">
            <ref bean="_hibernate_config_list"/>
            </property>
            <property name="hibernateProperties">
                    <props>
                            <prop key="hibernate.connection.release_mode">auto</prop>
                            <prop key="hibernate.dialect">${HIBERNATE_DIALECT}</prop>
                            <prop key="hibernate.transaction.factory.class">${HIBERNATE_TRANSACTION_FACTORY}</prop>
                            <prop key="hibernate.current_session_context_class">jta</prop>
                            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
                            <prop key="hibernate.show.sql">true</prop>
                            <prop key="hibernate.transaction.flush_before_completion">true</prop>
                            <prop key="hibernate.transaction.auto_close_session">true</prop>
                    </props>
            </property>
            <property name="dataSource">
                    <ref bean="myproj.ds.${DS_PREFIX}${DB_DATASOURCE}"/>
            </property>
    </bean>

    …

    <bean id="myproj.ds.jndi-MySqlDS" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
            <property name="jndiName"><value>${DATASOURCE_JNDI_PREFIX}MySqlDS</value></property>
    </bean>

I'm getting the exception that follows. Any idea how I fix this annoying "Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource'" exception?

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myprojSessionFactory' defined in class path resource [myproj/spring/config/db/hibernate/Hibernate-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.openejb.core.ivm.naming.IvmContext' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 113 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.openejb.core.ivm.naming.IvmContext' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 119 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
    ... 125 more
Dave
  • 15,639
  • 133
  • 442
  • 830
  • It looks as though the wrong `jndiName` is being used in the `JndiObjectFactoryBean`. Can you add some of the startup log output from OpenEJB? Specifically, the part that shows the DataSource being configured and created. – David Blevins Sep 05 '12 at 20:11

1 Answers1

2

the datasource property is looking for a datasource and you've passed it a String (because you used value=). You need to pass it the reference to your bean instead: