Announcement Announcement Module
Collapse
No announcement yet.
Spring+Hibernate Multi DataSource Problems!!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring+Hibernate Multi DataSource Problems!!!

    Hi. I need to define 2 datasources in my application with differents DAO´s to access data from 2 differents DB. The problem is when i try to test the connections in a JUnit TestCase, when the context is load the application throws the next exception.

    org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'cl.indra.dominio.TestOperacionesBasicas' defined in null: Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been 1 to be able to autowire property 'dataSource' of bean 'cl.indra.dominio.TestOperacionesBasicas'.
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireByType(Abstract AutowireCapableBeanFactory.java:804)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:724)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:192)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:123)
    at junit.framework.TestCase.runBare(TestCase.java:125 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)

    <--------------Here is My Codes ------------------------------->

    testContext.xml

    <?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="genericDAOTarget"
    class="cl.indra.variables.soporte.comun.dao.DAOSup portHibernateImpl">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    <bean id="genericDAO"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="HibernatetransactionManager" />
    </property>
    <property name="target">
    <ref bean="genericDAOTarget"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/test_db</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>admin</value>
    </property>
    </bean>

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="mappingResources">
    <list>
    <value>cl/indra/dominio/Usuario.hbm.xml</value>
    <value>cl/indra/dominio/Empleados.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    <bean id="HibernatetransactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    </beans>

    testContext2.xml

    <?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="genericDAOTarget2"
    class="cl.indra.variables.soporte.comun.dao.DAOSup portHibernateImpl2">
    <property name="sessionFactory">
    <ref bean="sessionFactory2" />
    </property>
    </bean>

    <bean id="genericDAO2"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="HibernatetransactionManager2" />
    </property>
    <property name="target">
    <ref bean="genericDAOTarget2"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/test2_db</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>admin</value>
    </property>
    </bean>

    <bean id="sessionFactory2"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource2" />
    </property>
    <property name="mappingResources">
    <list>
    <value>cl/indra/dominio/Usuario.hbm.xml</value>
    <value>cl/indra/dominio/Empleados.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    <bean id="HibernatetransactionManager2"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory2" />
    </property>
    </bean>
    </beans>

    testCaseContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <import resource="classpath:testContext.xml"/>
    <import resource="classpath:testContext2.xml"/>
    </beans>

    anyone can help?

    Thanx a Lot

  • #2
    You can change the default autowire type of the test.
    http://www.springframework.org/docs/...owireMode(int)
    Last edited by karldmoore; Aug 27th, 2007, 03:40 PM.

    Comment


    • #3
      Works fine, i change the testcase superclass to AbstractDependencyInjectionSpringContextTests and set the property super.setAutowireMode(super.AUTOWIRE_BY_TYPE);

      Thanx a lot karldmoore

      Comment


      • #4
        Not a problem, glad to help! The Spring JavaDoc is very good so it's well worth a look. Presumably you changed it from AUTOWIRE_BY_TYPE to AUTOWIRE_BY_NAME.
        Last edited by karldmoore; Aug 27th, 2007, 03:39 PM.

        Comment

        Working...
        X