How to define multiple sessionfactory instances in Spring?

I would like to have several Hibernate SessionFactories in the spring application, all of which have identical configurations except the DataSource. Ideally, I would acquire a specific SessionFactory by name. I need to be able to do this based on the state of the runtime, and it is not possible to determine which session factories I will need during application startup. Basically, I need a SessionFactoryTemplate or something like that.

Is it possible? How can I do it?

+3
source share
4 answers

You can define an abstract bean and use bean inheritance. This means that you will have a bean definition that works like a template, and you can have multiple beans by simply copying the attributes set by the parent bean.

Here is an example:

<bean id="abstractSessionFactory" abstract="true"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingResources">
      <list>
        <value>product.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
</bean>

<bean id="mySessionFactory" parent="abstractSessionFactory">
    <property name="dataSource" ref="myDataSource"/>
    ...
</bean>

<bean id="mySessionFactory2" parent="abstractSessionFactory">
    <property name="dataSource" ref="myDataSource2"/>
    ...
</bean>

Using the 'abstract' attribute, you guarantee that the bean will not be created and will be used as a template.

Read more here: link text

+3
source

, SessionFactories? / , (, ?), , SessionFactory, DataSource, ?

. :

Spring.

+1

, bean, SessionFactory?

<bean id="mySessionFactory1" 
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource1"/>
    <property name="mappingResources">
      <list>
        <value>product.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
</bean>

<bean id="mySessionFactory2"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource2"/>
    ...
</bean>

DAO sessionFactory :

<bean id="myProductDao" class="product.ProductDaoImpl">
  <property name="sessionFactory" ref="mySessionFactory1"/>
</bean>

<bean id="myCompanyDao" class="product.ProductDaoImpl">
  <property name="sessionFactory" ref="mySessionFactory2"/>
</bean>
0

, , Spring.

Hibernate Interceptors, , / master/admin. , , , SQL, Hibernate, , . , .

Alternatively, you can try writing your own TransactionManager, using HibernateTransactionManageras a starting point, adding support for working with multiple session factories. However, this would mean that you really need to dive into Spring's internal ORM support, and this is what I tried, but then abandoned the first approach. I am sure that this can be done with moderate efforts, but the previous solution already performed this work for us.

0
source

Source: https://habr.com/ru/post/1707202/


All Articles