Announcement Announcement Module
Collapse
No announcement yet.
Spring Hibernate Multiple sessionFactories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Hibernate Multiple sessionFactories

    Hi there, newbie to jsf-spring-hibernate... new to this forum and this is my first posting. I have a very elementary question. Please dont reply back with "search old posts". I did search and could not figure out the answer...
    I have an application where I am moving data from a couple of tables in one database to another table in another database. I am not sure how to wire up spring to use multiple session factories. The spring config file i am using is :

    ================================================== ====
    <beans>
    <bean id="DataSource1" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>jdbc/ds1</value>
    </property>
    </bean>
    <bean id="DataSource2" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>jdbc/ds2</value>
    </property>
    </bean>
    <bean id="DataSource3" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>jdbc/ds3</value>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean" autowire="byName">
    <property name="dataSource"><ref local="DataSource1"/></property>
    <property name="mappingResources">
    <list>
    <value>/com/xxx/yyy/bo/User.hbm.xml</value>
    <value>/com/xxx/yyy/bo/Menu.hbm.xml</value>
    </list>
    </property>

    <property name="hibernateProperties">
    <props >
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.S QLServerDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">fal se</prop>
    </props>
    </property>
    </bean>

    <bean id="userDAOTarget"
    class="com.bcbsa.ldsweb.dao.UserDAOHibernateImpl" autowire="byName"/>

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate.Hibernate TransactionManager" autowire="byName"/>

    <bean id="usrDAO"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">

    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="userDAOTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>

    </bean>

    <bean id="model"
    class="com.bcbsa.ldsweb.delegate.UserBusinessDeleg ateDAOSupportImpl">
    <property name="userDao"><ref bean="usrDAO"/></property>
    </bean>

    </beans>

    ================================================== ====

    Now my specific question is, if I have multiple sessionfactories defined, where and how should I inject that into my DAO's ? Do I need to define a property within my UserDAOHibernateImpl class called "sessionFactory" and inject it from within the file? Any code example will be useful.

    Thanks

  • #2
    where and how should I inject that into my DAO's
    If you want to use HibernateDaoSupport, try limiting yourslf to one DAO per sessionFactory. You can effectively combine these DAOs into a higher level service layer.

    Do I need to define a property within my UserDAOHibernateImpl class called "sessionFactory"
    Not if you're using HibernateDaoSupport (it defines it for you) - but this is designed for one sessionFactory, thus the suggestion above. Have a look at the samples to see how it's used (specifically PetClinic's HibernateClinic.java) or the HibernateDaoSupport JavaDoc.

    Comment


    • #3
      I agree with the solution katentim suggested. It's far easier to have a stack of DAOs per sessionFactory (not to metion that by definition DAOs are just about data access). The switching between the sessionFactories should be done by a service/facade level that will move data between the two SFs through the DAOs.
      That being said, dynamically SF injection/switching should happen in my opinion only when you have a large number of SFs and/or the DAO stacks would be just duplicated - this was one scenario that was discussed several times on the forum.

      Comment

      Working...
      X