3

Now before anyone starts jumping to conclusions and looks for other posts to mark this as a duplicate, let me just say this is a last ditch effort for me too. I tried whatever I could find and still the problem persists.

So, getting to the problem. I'm trying to connect to two different Databases from my Spring-Hiberate application. Both are Oracle 11G Databases. But am getting the following error:

No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [sessionFactory01, sessionFactory02]

My spring-servlet.xml, where I've declared the bean is as:

    <bean id="sessionFactory01"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="DB01dataSource" />
    <property name="configLocation">
        <value>classpath:DB01.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.db_stage.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<bean id="sessionFactory02"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="DB02dataSource" />
    <property name="configLocation">
        <value>classpath:DB02.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.db_prod.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
    <bean id="transactionManager01"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory01" />
</bean>
    <bean id="transactionManager02"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory02" />
</bean>

I have two configuration files for this named DB01.cfg.xml and DB02.cfg.xml. I thought it wasn't required here, and hence didn't post it.

This is how I create the sessionFactory object in my DAOImpl:

@Autowired
@Qualifier("sessionFactory01") SessionFactory sessionFactory01 = new Configuration().configure("DB01.cfg.xml").buildSessionFactory();

@Autowired
@Qualifier("sessionFactory02") SessionFactory sessionFactory02 = new Configuration().configure("DB02.cfg.xml").buildSessionFactory();

NOTE: I've tried using @Resource in place of @Autowired in my DAOImpl, still gives me the same error.

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Abhineet Kumar
  • 306
  • 2
  • 20
  • 1
    @Jim Sir, please read the post carefully before marking it as a duplicate. Please do take a look at what I HAVE done and what is being ASKED to do in the post which you've suggested I refer to. – Abhineet Kumar Apr 20 '16 at 06:52
  • The question is properly written and explanations are OK. From my point of view, you made an error with the title. Everybody is intended to have a look at the question itself before voting it to close, but looks like not everyone does it. It's not your fault of course, but I bet that with a more concrete title this wouldn't happen. – Aritz Apr 20 '16 at 07:26
  • How do I remove the [duplicate] tag now? – Abhineet Kumar Apr 20 '16 at 07:29
  • I've voted for reopening, need more people doing the same, though. – Aritz Apr 20 '16 at 07:32

0 Answers0