Question

for example can I use catalog attribute or schema for it?
Now I use several databases and several sessionfactories but I would like to create relationships between these databases by foreign keys, so I need to use one SessionFactory as far as I understand. Maybe some sample configuration, how can I achieve it?

Thanks in advance

Was it helpful?

Solution

In general, trying to keep FKs in synch across more than 1 database is going to require you manage your FK values using your own sequence generator. You will need to use an XADataSource to ensure that any transactions are able to span both databases and rollback appropriately.

You will need to use one SessionFactory for each database since each SessionFactory is tied to a single DataSource. JOTM is the Open Java Transaction Manager and is supported by Spring.

An example Spring context would look like this:

<?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/password@server:port:DB1"/>
</bean>

<bean id="oracle2DataSourceTarget" class="oracle.jdbc.xa.client.OracleXADataSource">
 <property name="URL" value="jdbc:oracle:thin:user/password@server: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>

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top