Spring multiple SessionFactory config?

3.4k Views Asked by At

I am trying to config two sessionFactories using spring. My config looks similar to the one listed here

Here's my config.xml

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url">
        <value>${hibernate.connection.url}</value>
    </property>
    <property name="driverClassName">
        <value>${hibernate.connection.driver_class}</value>
    </property>
    <property name="username">
        <value>${hibernate.connection.username}</value>
    </property>
    <property name="password">
        <value>${hibernate.connection.password}</value>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="mappingResources">
        <list>
            ...Mappings
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.dialect}
            </prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
            <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
            <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
            <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
            <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
        </props>
    </property>
</bean>

<bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url">
        <value>${hibernate.connection.mirror_url}</value>
    </property>
    <property name="driverClassName">
        <value>${hibernate.connection.driver_class}</value>
    </property>
    <property name="username">
        <value>${hibernate.connection.mirror_username}</value>
    </property>
    <property name="password">
        <value>${hibernate.connection.mirror_password}</value>
    </property>
</bean>

<bean id="sessionFactory2"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource2" />
    </property>
    <property name="mappingResources">
        <list>
            ...Mappings
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.dialect}
            </prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
            <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
            <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
            <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
            <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
        </props>
    </property>
</bean>

Then each dao gets a different sessionFactory assigned

<bean id="productDao"
    class="test.dao.ProductDaoHibernate">
    <property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>

<bean id="currencyDao"
    class="test.dao.CurrencyDaoHibernate">
    <property name="sessionFactory"><ref bean="sessionFactory2" /></property>
</bean>

This config gets loaded when its added to the context

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/test-data.xml /WEB-INF/classes/test-services.xml ... </param-value>
</context-param>

The problem shows whenever I start the server each sessionFactory built, but at the end of the second one this shows up:

[INFO] [org.springframework.beans.factory.support.DefaultListableBeanFactory]:? - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@97aaa6: defining beans [... Many elements...]; root of factory hierarchy
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing

Any help, or lead would be appreciated, if you need more info please ask

2

There are 2 best solutions below

2
On

Actually this is supposed to be a comment, but don't have enough reputation (requires 50 points)

It seems you are trying to create 2 different bean id's which are of exactly same configuration. One way is to figure out whether 2 different session objects pointing to same configuration is required. Other way is to try adding the configurations in different files and load separately. Please add how you r trying to use these configurations in code.

0
On

spring4.2 & hibernate5.1.5

@Bean
public LocalSessionFactoryBean aaaSessionFactory() {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(smsDataSource());
    localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
    localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
    localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
    localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
    return localSessionFactoryBean;
}

@Bean
public LocalSessionFactoryBean bbbSessionFactoryMultiple() {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(smsDataSource());
    localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
    localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
    localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
    localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
    return localSessionFactoryBean;
}


public class xxx extends HibernateDaoSupport{***
    @Autowired
    public void anyMethodName(@Qualifier("aaaSessionFactory")     SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }