2010-11-17 64 views

回答

1

一般情況下,試圖讓FKS在同步跨越超過1個數據庫將要求您使用自己的序列發生器管理您的FK值。您將需要使用XADataSource來確保任何事務都能夠跨兩個數據庫並進行適當的回滾。

因爲每個SessionFactory綁定到一個DataSource,所以您將需要爲每個數據庫使用一個SessionFactory。 JOTM是Open Java Transaction Manager,並受Spring支持。

一個例子Spring上下文是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
<property name="userTransaction" ref="jotm"/> 
</bean> 

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/> 

<bean id="oracle1DataSource" class="oracle.jdbc.xa.client.OracleXADataSource"> 
<property name="xaDataSource" ref="oracle1DataSourceTarget"/> 
<property name="transactionManager" ref="jotm"/> 
</bean> 

<bean id="oracle2DataSource" class="oracle.jdbc.xa.client.OracleXADataSource"> 
<property name="xaDataSource" ref="oracle2DataSourceTarget"/> 
<property name="transactionManager" ref="jotm"/> 
</bean> 

<beans> 
<bean id="oracle1DataSourceTarget" class="oracle.jdbc.xa.client.OracleXADataSource"> 
<property name="URL" value="jdbc:oracle:thin:user/[email protected]:port:DB1"/> 
</bean> 

<bean id="oracle2DataSourceTarget" class="oracle.jdbc.xa.client.OracleXADataSource"> 
<property name="URL" value="jdbc:oracle:thin:user/[email protected]:port:DB2"/> 
</bean> 

<bean id="oracle1SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
<property name="dataSource" ref="oracle1DataSource"/> 
<property name="jtaTransactionManager" ref="jotm"/> 
<property name="useTransactionAwareDataSource" value="true"/> 
<property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> 
    </props> 
</property> 
<property name="mappingResources"> 
    <list> 
    <value>org/example/Entity.hbm.xml</value> 
    </list> 
</property> 
</bean> 

<bean id="oracle2SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
<property name="dataSource" ref="oracle2DataSource"/> 
<property name="jtaTransactionManager" ref="jotm"/> 
<property name="useTransactionAwareDataSource" value="true"/> 
<property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> 
    </props> 
</property> 
<property name="mappingResources"> 
    <list> 
    <value>org/example/Entity.hbm.xml</value> 
    </list> 
</property> 
</bean> 

希望這有助於。

相關問題