你應該有單獨的會話工廠和數據源配置爲每個DBS的,你可以通過數據源名稱指的是正確的e.g。
這意味着如果你有3個遠程數據庫,那麼理想情況下你應該有3個sessionFacories和數據源。
例子:
休眠-first.cfg.xml - 你一樣有
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
<!-- Here commes the entities mapped to tables in the FIRST database -->
<mapping class="com.yourpackage.EntityOftheFirstDatabase" />
</session-factory>
休眠-second.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
<!-- Here commes the entities mapped to tables in the SECOND database -->
<mapping class="com.yourpackage.EntityOftheSecondDatabase" />
</session-factory>
現在讓我們來配置會話工廠春天:
<!-- Hibernate SessionFactory for the FIRST database -->
<bean id="sessionFactoryForTheFirstDatabase" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceForTheFirstDatabase" />
<property name="configLocation" value="classpath:hibernate-first.cfg.xml" />
</bean>
<!-- Hibernate SessionFactory for the SECOND database -->
<bean id="sessionFactoryForTheSecondDatabase" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceForTheSecondDatabase" />
<property name="configLocation" value="classpath:hibernate-second.cfg.xml" />
</bean>
數據源
<bean id="dataSourceForTheFirstDatabase" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="your specific" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
第二個數據源是非常相同的只需將它指向第二個數據庫。如果你想配置TX管理比你需要兩個事務管理器爲您databses:
<bean id="firstDbServiceProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<description>Transaction boundaries are mapped to service layer</description>
<property name="proxyTargetClass" value="false" />
<property name="beanNames">
<list>
<value>ServicesMangingoperationsAboveYourFirstDatabase1</value>
<value>ServicesMangingoperationsAboveYourFirstDatabase2</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>FirstDbTxInterceptor</value>
</list>
</property>
</bean>
如果你想有一個交易是跨越多個數據庫的也有可能,只是不知道你是否需要它!希望這對你來說已經足夠清晰。我知道有更多奇特的方法來定義TX經理和春天的東西,主要是如果你使用的是最新的,我建議你試一試,就像我描述的那樣,之後你可以調整它 - 谷歌是你的朋友:)
ü可以上傳你 – Vikki
編輯我的消息,任何有關link..thank,希望我是足夠描述:) – shippi
非常感謝。 – Vikki