1

I'm new to hibernate and I'm trying to connect to multiple DB's. I know that we can create a new cfg file separate for each DB and then create an factory of it like

factory1 = new Configuration().configure(cfg1.xml).buildSessionFactory();
factory2 = new Configuration().configure(cfg2.xml).buildSessionFactory();

But wanted to know what is the meaning of having a name like session-factory name="SESS1" in hibernate-configuration and can I use that to define multiple DB sessions there instead of defining in a new cfg file. please let me know.

1 Answer 1

1

if you have another database you should define the relevant configuration in hibernate.hbm.xml to create a separate SessionFactory for that database too.

Yes it is possible to do, what you need to do is change the names inside your cfg.xml file.

For example:

<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <!-- ... -->
</bean>

<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1"/>
    <!-- ... -->
</bean>


<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource2"/>
    <!-- ... -->
</bean>

<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory2"/>
    <!-- ... -->
</bean>

You can also check this topic here: Hibernate using multiple databases

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.