Results 1 to 6 of 6

Thread: Hibernate using multiple databases

  1. #1
    Join Date
    Apr 2011
    Posts
    4

    Lightbulb Hibernate using multiple databases

    Hello everybody

    Someone know how to add a another datasource in hibernate configuration and how to configure Spring to that datasource its autoinject in my respective DAO?


    This is my code with one datasource, that run perfectly, but i don't know how add another datasource.


    HIBERNATE CONFIGURACION

    Code:
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url" value="jdbc:mysql://localhost/personal"/>
            <property name="username" value="root"/>
            <property name="password" value="mysql"/>
        </bean>
    
       
    
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref local="dataSource"/>
            </property>
            <property name="packagesToScan">
                <list>
                    <value>com.app.personal.model</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                </props>
            </property>
        </bean>
    
        <tx:annotation-driven/>
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>


    DAO EXAMPLE


    Code:
    @Repository
    public class ModuloDAOHibernate extends HibernateTemplate implements ModuloDAO {
    
        @Autowired
        public ModuloDAOHibernate(SessionFactory sessionFactory) {
            super(sessionFactory); 
        }
    
        public List<Modulo> getAllGrupoModuloDAO() {
            Criteria criteriaList = this.getSession().createCriteria(Modulo.class);
            criteriaList.addOrder(Order.asc("orden"));
            return criteriaList.list();
        }



    Thanks

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello Jose

    Someone know how to add a another datasource in hibernate configuration and how to configure Spring to that datasource its autoinject in my respective DAO?
    Do you want for your unique Hibernate SessionFactory work with two datasources? I think is not possible

    What is your specific requirement to want do this?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Apr 2011
    Posts
    4

    Default

    Well. I've a aplication that should access to two databases. At this time my configuration only can access to one database ( like example: personal) but I've access to another database and I want to access too to that.

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello

    Did you reseach about this in the forum? I recall this was covered some times


    Perhaps you could create two different SessionFactory's with dependency to two different dataSource (for each DB configuration) and inject carefully in your DAO classes

    I assume each DB is totally independt of each other

    Try google too, here a result Load Two DataSources
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    Apr 2011
    Posts
    4

    Default

    Well. Finally I resolved my problem with this way:

    First: add another dataSource and sessionFactory.

    Second: add in the end of conf of hibernate a bean to every DAO that use another sessionFactory (implicit another dataSource). Like this:


    Code:
    <bean id="courseDAO" class="com.app.CourseDAOHibernate">
            <property name="sessionFactory" ref="sessionFactory2"/>
        </bean>

    Every DAO that use a "sessionFactory" is inyected automatically, but in case of others DAO that use a others datasource you should inyect explicitely like the example.

    In the end I don't know if that is a successful solution but that is working for me.

  6. #6
    Join Date
    Apr 2011
    Posts
    4

    Default

    OK. When one Light turn on, another Lights turn on too. I 've looking for another solutions, and find this:

    Use the @Qualifier Annotation on the method that inject the sessionFactory:

    Code:
     @Autowired
        public ProgramaDAOHibernate(@Qualifier("sessionFactory2") SessionFactory sessionFactory) {
            super(sessionFactory);
        }


    WOW. Thank to everyone that helpme.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •