本文介绍了多个数据源的通用解决方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有在我的春季应用程序中,我需要将休眠与两个不同的数据库(PostgreSQL和MySQL)一起使用,并且我的配置不太好,因此我需要一些有关此操作的指南

Greetings allIn my spring application I will need to use hibernate with two different databases (PostgreSQL & MySQL) and I am not pretty good with configuration, so I need some guide about how to do so

我在hibernate-postgresql中使用以下配置

I am using the following configuration for hibernate-postgresql

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.project.domain.myDomain</value>
            </list>

        </property>

        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
            </value>
        </property>


    </bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.postgresql.Driver" />

        <property name="url">
        <value>${db.url}</value>
        </property>

        <property name="username" >
        <value>${db.username}</value>
        </property>

        <property name="password">
        <value>${db.password}</value>
        </property>

    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

在DAO中,我为sessionFactory制作了自动连线.

and in the DAO I make an autowire for the sessionFactory.

推荐答案

唯一的方法是必须将数据源与它们的一个堆栈(SessionFactory,Hibernate Template等)一起使用.然后,您可以注入要在业务类中使用的HIbernate Tempolate(如果要同时访问两个DB,则可以注入两者).

The only way is to have to Data Sources with their one stack (SessionFactory, Hibernate Template, ect.). Then you can inject the HIbernate Tempolate you want to use in you business classes (or both if you want to access both DB at same time).

这是带有显式配置的示例DAO ...

here is an example DAO with explicit config ...

<bean id="db2SessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
</bean>

<bean id="db1Dao" class="ch.sbb.uno.dao.hibernate.UnoHibernateDaoSupport" scope="prototype">
    <property name="sessionFactory" ref="db1SessionFactory" />
</bean>

<bean id="db2Dao" class="ch.sbb.uno.dao.hibernate.UnoHibernateDaoSupport" scope="prototype">
    <property name="sessionFactory" ref="db2SessionFactory" />
</bean>

这篇关于多个数据源的通用解决方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:22
查看更多